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