英文:
How can I enumerate all constraints using constraint satisfaction problem
问题
我有一个产品属性,比如颜色。我有绿色和蓝色两种颜色。绿色被认为比蓝色好。我有三个产品,A是绿色,B是蓝色,C是绿色。问题是如何使用约束满足问题算法列举出产品之间的所有可能的序数约束?
如何使用CSP算法建模并解决这个问题?
英文:
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?
答案1
得分: 3
在MiniZinc中,你可以以以下方式编写决策变量和约束条件:
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];
% 决策变量:每种产品的数量
array[Products] of var int: volume;
% 可用于购买产品的金额
int: Budget = 100;
% 金钱是我们的限制因素
constraint
Budget > sum([volume[p] * price[p] | p in Products]);
% 数量不能为负
constraint
forall(p in Products) ( volume[p] >= 0 );
% 在我们的预算内获取尽可能多的价值
solve maximize sum([volume[p] * merit[p] | p in Products]);
output
[ "\n\(p): \(volume) x \(color
) "
++ "cost:\(volume*price
) merit:\(volume
*merit
)"
| p in Products ];
这对于大多数约束求解器来说都是类似的。给定的示例也可以使用线性规划求解器来解决。
英文:
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 * price
| p in Products]);
% volumes mustn't be negative
constraint
forall(p in Products) ( volume
>= 0 );
% as much merit as fits into our budget
solve maximize sum([volume
* merit
| p in Products]);
output
[ "\n\(p): \(volume
) x \(color
) "
++ "cost:\(volume
*price
) merit:\(volume
*merit
)"
| p in Products ];
This is somehow similar for most constraint solvers. The given example could also be solved with linear programming solvers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论