| blib/lib/Algorithm/SAT/Backtracking/Ordered.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 38 | 38 | 100.0 |
| branch | 10 | 10 | 100.0 |
| condition | 1 | 2 | 50.0 |
| subroutine | 8 | 8 | 100.0 |
| pod | 3 | 3 | 100.0 |
| total | 60 | 61 | 98.3 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Algorithm::SAT::Backtracking::Ordered; | ||||||
| 2 | 3 | 3 | 31853 | use base 'Algorithm::SAT::Backtracking'; | |||
| 3 | 6 | ||||||
| 3 | 883 | ||||||
| 3 | 3 | 3 | 15 | use strict; | |||
| 3 | 8 | ||||||
| 3 | 70 | ||||||
| 4 | 3 | 3 | 10 | use warnings; | |||
| 3 | 1 | ||||||
| 3 | 56 | ||||||
| 5 | 3 | 3 | 1507 | use Hash::Ordered; | |||
| 3 | 8548 | ||||||
| 3 | 658 | ||||||
| 6 | ##Ordered implementation, of course has its costs | ||||||
| 7 | our $VERSION = "0.12"; | ||||||
| 8 | |||||||
| 9 | sub _choice { | ||||||
| 10 | 8 | 8 | 8 | my $self = shift; | |||
| 11 | 8 | 8 | my $variables = shift; | ||||
| 12 | 8 | 6 | my $model = shift; | ||||
| 13 | 8 | 5 | my $choice; | ||||
| 14 | 8 | 7 | foreach my $variable ( @{$variables} ) { | ||||
| 8 | 11 | ||||||
| 15 | 21 | 100 | 50 | 67 | $choice = $variable and last if ( !$model->exists($variable) ); | ||
| 16 | } | ||||||
| 17 | 8 | 46 | return $choice; | ||||
| 18 | } | ||||||
| 19 | |||||||
| 20 | sub solve { | ||||||
| 21 | 10 | 10 | 1 | 11 | my $self = shift; | ||
| 22 | 10 | 12 | my $variables = shift; | ||||
| 23 | 10 | 8 | my $clauses = shift; | ||||
| 24 | 10 | 100 | 30 | my $model = defined $_[0] ? shift : Hash::Ordered->new; | |||
| 25 | 10 | 49 | return $self->SUPER::solve( $variables, $clauses, $model ); | ||||
| 26 | } | ||||||
| 27 | |||||||
| 28 | # ### update | ||||||
| 29 | # Copies the model, then sets `choice` = `value` in the model, and returns it, keeping the order of keys. | ||||||
| 30 | sub update { | ||||||
| 31 | 22 | 22 | 1 | 20 | my $self = shift; | ||
| 32 | 22 | 44 | my $copy = shift->clone; | ||||
| 33 | 22 | 234 | my $choice = shift; | ||||
| 34 | 22 | 21 | my $value = shift; | ||||
| 35 | 22 | 42 | $copy->set( $choice => $value ); | ||||
| 36 | 22 | 181 | return $copy; | ||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | # ### resolve | ||||||
| 40 | # Resolve some variable to its actual value, or undefined. | ||||||
| 41 | sub resolve { | ||||||
| 42 | 2944 | 2944 | 1 | 4216 | my $self = shift; | ||
| 43 | 2944 | 2112 | my $var = shift; | ||||
| 44 | 2944 | 2124 | my $model = shift; | ||||
| 45 | 2944 | 100 | 3843 | if ( substr( $var, 0, 1 ) eq "-" ) { | |||
| 46 | 1180 | 1964 | my $value = $model->get( substr( $var, 1 ) ); | ||||
| 47 | 1180 | 100 | 7787 | return !defined $value ? undef : $value == 0 ? 1 : 0; | |||
| 100 | |||||||
| 48 | } | ||||||
| 49 | else { | ||||||
| 50 | 1764 | 2476 | return $model->get($var); | ||||
| 51 | } | ||||||
| 52 | } | ||||||
| 53 | |||||||
| 54 | 1; | ||||||
| 55 | |||||||
| 56 | |||||||
| 57 | =encoding utf-8 | ||||||
| 58 | |||||||
| 59 | =head1 NAME | ||||||
| 60 | |||||||
| 61 | Algorithm::SAT::Backtracking::Ordered - A simple Backtracking SAT ordered implementation | ||||||
| 62 | |||||||
| 63 | =head1 SYNOPSIS | ||||||
| 64 | |||||||
| 65 | |||||||
| 66 | # You can use it with Algorithm::SAT::Expression | ||||||
| 67 | use Algorithm::SAT::Expression; | ||||||
| 68 | |||||||
| 69 | my $expr = Algorithm::SAT::Expression->new->with("Algorithm::SAT::Backtracking::Ordered"); | ||||||
| 70 | $expr->or( '-foo@2.1', 'bar@2.2' ); | ||||||
| 71 | $expr->or( '-foo@2.3', 'bar@2.2' ); | ||||||
| 72 | $expr->or( '-baz@2.3', 'bar@2.3' ); | ||||||
| 73 | $expr->or( '-baz@1.2', 'bar@2.2' ); | ||||||
| 74 | my $model = $exp->solve(); | ||||||
| 75 | |||||||
| 76 | # Or you can use it directly: | ||||||
| 77 | use Algorithm::SAT::Backtracking::Ordered; | ||||||
| 78 | my $solver = Algorithm::SAT::Backtracking::Ordered->new; | ||||||
| 79 | my $variables = [ 'blue', 'green', 'yellow', 'pink', 'purple' ]; | ||||||
| 80 | my $clauses = [ | ||||||
| 81 | [ 'blue', 'green', '-yellow' ], | ||||||
| 82 | [ '-blue', '-green', 'yellow' ], | ||||||
| 83 | [ 'pink', 'purple', 'green', 'blue', '-yellow' ] | ||||||
| 84 | ]; | ||||||
| 85 | |||||||
| 86 | my $model = $solver->solve( $variables, $clauses ); | ||||||
| 87 | |||||||
| 88 | |||||||
| 89 | =head1 DESCRIPTION | ||||||
| 90 | |||||||
| 91 | |||||||
| 92 | Algorithm::SAT::Backtracking::Ordered is a pure Perl implementation of a simple SAT Backtracking solver, in this variant of L |
||||||
| 93 | |||||||
| 94 | Look at L |
||||||
| 95 | |||||||
| 96 | Look also at the test file for an example of usage. | ||||||
| 97 | |||||||
| 98 | L |
||||||
| 99 | |||||||
| 100 | =head1 METHODS | ||||||
| 101 | |||||||
| 102 | Inherits all the methods from L |
||||||
| 103 | |||||||
| 104 | =head2 SOLVE | ||||||
| 105 | |||||||
| 106 | $expr->solve(); | ||||||
| 107 | |||||||
| 108 | in this case returns a L |
||||||
| 109 | |||||||
| 110 | =head1 LICENSE | ||||||
| 111 | |||||||
| 112 | Copyright (C) mudler. | ||||||
| 113 | |||||||
| 114 | This library is free software; you can redistribute it and/or modify | ||||||
| 115 | it under the same terms as Perl itself. | ||||||
| 116 | |||||||
| 117 | =head1 AUTHOR | ||||||
| 118 | |||||||
| 119 | mudler E |
||||||
| 120 | |||||||
| 121 | =head1 SEE ALSO | ||||||
| 122 | |||||||
| 123 | L |
||||||
| 124 | |||||||
| 125 | =cut | ||||||
| 126 |