File Coverage

blib/lib/Algorithm/SAT/Backtracking/Ordered/DPLL.pm
Criterion Covered Total %
statement 33 39 84.6
branch 8 14 57.1
condition 1 11 9.0
subroutine 8 9 88.8
pod 1 1 100.0
total 51 74 68.9


line stmt bran cond sub pod time code
1             package Algorithm::SAT::Backtracking::Ordered::DPLL;
2 2     2   1540 use Hash::Ordered;
  2         3  
  2         51  
3 2     2   7 use base "Algorithm::SAT::Backtracking::DPLL";
  2         2  
  2         558  
4             use Algorithm::SAT::Backtracking::DPLL
5 2     2   9 "Algorithm::SAT::Backtracking::Ordered";
  2         4  
  2         14  
6 2     2   20 use strict;
  2         4  
  2         81  
7 2     2   11 use warnings;
  2         3  
  2         874  
8             our $VERSION = "0.13";
9              
10             ##Ordered implementation, of course has its costs
11             sub solve {
12 18     18 1 50 my ( $self, $variables, $clauses, $model ) = @_;
13 18 100       60 $model = Hash::Ordered->new if !defined $model;
14 18         118 return $self->SUPER::solve( $variables, $clauses, $model );
15             }
16              
17             sub _up {
18 15     15   42 my ( $self, $variables, $clauses, $model ) = @_;
19 15 50       30 $model = Hash::Ordered->new if !defined $model;
20              
21             #Finding single clauses that must be true, and updating the model
22 57         119 ( @{$_} != 1 )
23             ? ()
24             : ( substr( $_->[0], 0, 1 ) eq "-" ) ? (
25 15         33 do {
26 7         16 my $literal = substr( $_->[0], 1 );
27 7         23 $model->set( $literal => 0 );
28 7         70 $self->_delete_from_index( $literal, $clauses );
29             }
30              
31             #remove the positive clause form OR's and add it to the model with a false value
32             )
33             : (
34             $self->_add_literal( "-" . $_->[0], $clauses )
35             and $model->set( $_->[0] => 1 )
36              
37             ) # if the literal is present, remove it from SINGLE ARRAYS in $clauses and add it to the model with a true value
38 15 50 0     21 for ( @{$clauses} );
    100          
39 15         42 return $model;
40             }
41              
42             sub _add_literal {
43 0     0   0 my ( $self, $literal, $clauses, $model ) = @_;
44 0 0       0 $literal
45             = ( substr( $literal, 0, 1 ) eq "-" )
46             ? $literal
47             : substr( $literal, 1 );
48             return
49 0 0 0     0 if $model
      0        
50             and $model->exists($literal)
51             and $model->set( $literal, 1 ); #avoid cycle if already set
52             #remove the literal from the model (set to false)
53 0         0 $model->set( $literal, 1 );
54 0         0 $self->_delete_from_index( $literal, $clauses );
55 0         0 return 1;
56             }
57              
58             sub _choice {
59 14     14   22 my ( $self, $variables, $model ) = @_;
60 14         16 my $choice;
61 14         17 foreach my $variable ( @{$variables} ) {
  14         28  
62 33 100 50     161 $choice = $variable and last if ( !$model->exists($variable) );
63             }
64 14         113 return $choice;
65             }
66              
67             1;
68              
69             =encoding utf-8
70              
71             =head1 NAME
72              
73             Algorithm::SAT::Backtracking::Ordered::DPLL - A DPLL Backtracking SAT ordered implementation
74              
75             =head1 SYNOPSIS
76              
77              
78             # You can use it with Algorithm::SAT::Expression
79             use Algorithm::SAT::Expression;
80              
81             my $expr = Algorithm::SAT::Expression->new->with("Algorithm::SAT::Backtracking::Ordered::DPLL");
82             $expr->or( '-foo@2.1', 'bar@2.2' );
83             $expr->or( '-foo@2.3', 'bar@2.2' );
84             $expr->or( '-baz@2.3', 'bar@2.3' );
85             $expr->or( '-baz@1.2', 'bar@2.2' );
86             my $model = $exp->solve();
87              
88             # Or you can use it directly:
89             use Algorithm::SAT::Backtracking::Ordered::DPLL;
90             my $solver = Algorithm::SAT::Backtracking::Ordered::DPLL->new;
91             my $variables = [ 'blue', 'green', 'yellow', 'pink', 'purple' ];
92             my $clauses = [
93             [ 'blue', 'green', '-yellow' ],
94             [ '-blue', '-green', 'yellow' ],
95             [ 'pink', 'purple', 'green', 'blue', '-yellow' ]
96             ];
97              
98             my $model = $solver->solve( $variables, $clauses );
99              
100              
101             =head1 DESCRIPTION
102              
103              
104             Algorithm::SAT::Backtracking::Ordered::DPLL is a pure Perl implementation of a DPLL SAT Backtracking solver, in this variant of L we keep the order of the model updates and return a L as result.
105              
106             Look at L for a theory description.
107              
108             Look also at the test file for an example of usage.
109              
110             L use this module to solve Boolean expressions.
111              
112             =head1 METHODS
113              
114             Inherits all the methods from L and override/implements the following:
115              
116             =head2 SOLVE
117              
118             $expr->solve();
119              
120             in this case returns a L.
121              
122             =head1 LICENSE
123              
124             Copyright (C) mudler.
125              
126             This library is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself.
128              
129             =head1 AUTHOR
130              
131             mudler Emudler@dark-lab.netE
132              
133             =head1 SEE ALSO
134              
135             L, L, L, L
136              
137             =cut
138