FCO / ProblemSolver

A constraint programming tool for perl6
6 stars 0 forks source link

Weird behavior in simple problem #4

Open markjreed opened 2 years ago

markjreed commented 2 years ago

Tried to implement the municipal department numbers problem. Obviously this is a trivial problem that is amenable to a brute-force solution, but I thought it would be a good toy exercise for the constraint solver. However, I'm getting inconsistent results from run to run; it only produces the correct result about half the time, so I wonder if I'm doing something incorrectly.

This is my attempt:

use ProblemSolver;

my ProblemSolver $p .= new;
my @vars = «police sanitation fire»;
for @vars -> $dept {
  $p.add-variable($dept, 1..7);
}
$p.unique-vars(@vars);
$p.add-constraint: -> :$police! { $police %% 2 };
$p.add-constraint: -> :$police!, :$sanitation!, :$fire! { $police + $sanitation + $fire == 12 };

.say for $p.solve;

One run will output the correct 14 solutions, while the next will include 16 additional solutions in which the value for police is odd, ignoring the middle constraint above.

FCO commented 1 year ago

Thanks for your message. Sorry for taking so long to respond. I’ll take a look at it as soon as possible, when I’m back home from vacation. Thanks