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