Leonardo Ribeiro
Leonardo Ribeiro

Reputation: 11

How can I enumerate all constraints using constraint satisfaction problem

I have an attribute of a product, let's say color. I have the color green and blue. Green is considered better than blue. I have three products A that is green, B that is blue, and C that is green. The question is how can I enumerate all possible ordinalities constraints between the products using a constraint satisfaction problem algorithm?

How can I model and solve this problem with a CSP algorithm?

Upvotes: 1

Views: 81

Answers (1)

Axel Kemper
Axel Kemper

Reputation: 11324

In MiniZinc you can write the decision variables and constraints in the following manner:

set of int: Products = 1..3;

array[Products] of string: color = ["red", "green", "blue"];
array[Products] of int: merit = [10, 5, 27];
array[Products] of int: price = [4, 7, 20];

%  decision variables: how much of which product?
array[Products] of var int: volume;

%  the money available to spend on products
int: Budget = 100;

%  money is our limiting factor
constraint
  Budget > sum([volume[p] * price[p] | p in Products]);
  
%  volumes mustn't be negative
constraint
  forall(p in Products) ( volume[p] >= 0 );
  
%  as much merit as fits into our budget
solve maximize sum([volume[p] * merit[p] | p in Products]);

output
[ "\n\(p): \(volume[p]) x \(color[p]) "
  ++ "cost:\(volume[p]*price[p]) merit:\(volume[p]*merit[p])" 
  | p in Products ];

This is somehow similar for most constraint solvers. The given example could also be solved with linear programming solvers.

Upvotes: 3

Related Questions