File Coverage

blib/lib/Algorithm/SAT/Backtracking/Ordered.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 10 100.0
condition 1 2 50.0
subroutine 8 8 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   35167 use base 'Algorithm::SAT::Backtracking';
  3         4  
  3         839  
3 3     3   23 use strict;
  3         4  
  3         105  
4 3     3   67 use warnings;
  3         4  
  3         94  
5 3     3   2303 use Hash::Ordered;
  3         9486  
  3         737  
6             ##Ordered implementation, of course has its costs
7             our $VERSION = "0.13";
8              
9             sub _choice {
10 8     8   11 my ( undef, $variables, $model ) = @_;
11              
12 8         7 my $choice;
13 8         6 foreach my $variable ( @{$variables} ) {
  8         13  
14 21 100 50     74 $choice = $variable and last if ( !$model->exists($variable) );
15             }
16 8         50 return $choice;
17             }
18              
19             sub solve {
20 10     10 1 12 my ( $self, $variables, $clauses, $model ) = @_;
21              
22 10 100       33 $model = Hash::Ordered->new if !defined $model;
23 10         53 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 33 my ( $self, $copy, $choice, $value ) = @_;
30 22         57 $copy = $copy->clone;
31              
32 22         340 $copy->set( $choice => $value );
33 22         220 return $copy;
34             }
35              
36             # ### resolve
37             # Resolve some variable to its actual value, or undefined.
38             sub resolve {
39 2944     2944 1 5982 my ( undef, $var, $model ) = @_;
40              
41 2944 100       4593 if ( substr( $var, 0, 1 ) eq "-" ) {
42 1180         2479 my $value = $model->get( substr( $var, 1 ) );
43 1180 100       9878 return !defined $value ? undef : $value == 0 ? 1 : 0;
    100          
44             }
45             else {
46 1764         3073 return $model->get($var);
47             }
48             }
49              
50             1;
51              
52             =encoding utf-8
53              
54             =head1 NAME
55              
56             Algorithm::SAT::Backtracking::Ordered - A simple Backtracking SAT ordered implementation
57              
58             =head1 SYNOPSIS
59              
60              
61             # You can use it with Algorithm::SAT::Expression
62             use Algorithm::SAT::Expression;
63              
64             my $expr = Algorithm::SAT::Expression->new->with("Algorithm::SAT::Backtracking::Ordered");
65             $expr->or( '-foo@2.1', 'bar@2.2' );
66             $expr->or( '-foo@2.3', 'bar@2.2' );
67             $expr->or( '-baz@2.3', 'bar@2.3' );
68             $expr->or( '-baz@1.2', 'bar@2.2' );
69             my $model = $exp->solve();
70              
71             # Or you can use it directly:
72             use Algorithm::SAT::Backtracking::Ordered;
73             my $solver = Algorithm::SAT::Backtracking::Ordered->new;
74             my $variables = [ 'blue', 'green', 'yellow', 'pink', 'purple' ];
75             my $clauses = [
76             [ 'blue', 'green', '-yellow' ],
77             [ '-blue', '-green', 'yellow' ],
78             [ 'pink', 'purple', 'green', 'blue', '-yellow' ]
79             ];
80              
81             my $model = $solver->solve( $variables, $clauses );
82              
83              
84             =head1 DESCRIPTION
85              
86              
87             Algorithm::SAT::Backtracking::Ordered is a pure Perl implementation of a simple SAT Backtracking solver, in this variant of L we keep the order of the model updates and return a L as result.
88              
89             Look at L for a theory description.
90              
91             Look also at the test file for an example of usage.
92              
93             L use this module to solve Boolean expressions.
94              
95             =head1 METHODS
96              
97             Inherits all the methods from L and override/implements the following:
98              
99             =head2 SOLVE
100              
101             $expr->solve();
102              
103             in this case returns a L.
104              
105             =head1 LICENSE
106              
107             Copyright (C) mudler.
108              
109             This library is free software; you can redistribute it and/or modify
110             it under the same terms as Perl itself.
111              
112             =head1 AUTHOR
113              
114             mudler Emudler@dark-lab.netE
115              
116             =head1 SEE ALSO
117              
118             L, L, L, L
119              
120             =cut
121