Java约束规划
Java Constraint Programming
如何在约束规划中表达二次 objective 函数?在Cplex中,我是这样写的:
IloLQNumExpr objfn = model.lqNumExpr()
但CP不一样
使用 CP Optimizer 非常简单。
例如:
cp2.add(cp2.le(cp2.prod(Germany,Germany),4));
在
IloCP cp2 = new IloCP();
IloIntVar Belgium = cp2.intVar(0, 3);
IloIntVar Denmark = cp2.intVar(0, 3);
IloIntVar France = cp2.intVar(0, 3);
IloIntVar Germany = cp2.intVar(0, 3);
IloIntVar Luxembourg = cp2.intVar(0, 3);
IloIntVar Netherlands = cp2.intVar(0, 3);
cp2.add(cp2.neq(Belgium , France));
cp2.add(cp2.neq(Belgium , Germany));
cp2.add(cp2.neq(Belgium , Netherlands));
cp2.add(cp2.neq(Belgium , Luxembourg));
cp2.add(cp2.neq(Denmark , Germany));
cp2.add(cp2.neq(France , Germany));
cp2.add(cp2.neq(France , Luxembourg));
cp2.add(cp2.neq(Germany , Luxembourg));
cp2.add(cp2.neq(Germany , Netherlands));
cp2.add(cp2.le(cp2.prod(Germany,Germany),4));
if (cp2.solve())
{
System.out.println();
System.out.println( "Belgium: " + (int)cp2.getValue(Belgium));
System.out.println( "Denmark: " + (int)cp2.getValue(Denmark));
System.out.println( "France: " + (int)cp2.getValue(France));
System.out.println( "Germany: " + (int)cp2.getValue(Germany));
System.out.println( "Luxembourg: " + (int)cp2.getValue(Luxembourg));
System.out.println( "Netherlands: " + (int)cp2.getValue(Netherlands));
}
如果我稍微改变一下非常简单的 color.java 示例
如何在约束规划中表达二次 objective 函数?在Cplex中,我是这样写的:
IloLQNumExpr objfn = model.lqNumExpr()
但CP不一样
使用 CP Optimizer 非常简单。
例如:
cp2.add(cp2.le(cp2.prod(Germany,Germany),4));
在
IloCP cp2 = new IloCP();
IloIntVar Belgium = cp2.intVar(0, 3);
IloIntVar Denmark = cp2.intVar(0, 3);
IloIntVar France = cp2.intVar(0, 3);
IloIntVar Germany = cp2.intVar(0, 3);
IloIntVar Luxembourg = cp2.intVar(0, 3);
IloIntVar Netherlands = cp2.intVar(0, 3);
cp2.add(cp2.neq(Belgium , France));
cp2.add(cp2.neq(Belgium , Germany));
cp2.add(cp2.neq(Belgium , Netherlands));
cp2.add(cp2.neq(Belgium , Luxembourg));
cp2.add(cp2.neq(Denmark , Germany));
cp2.add(cp2.neq(France , Germany));
cp2.add(cp2.neq(France , Luxembourg));
cp2.add(cp2.neq(Germany , Luxembourg));
cp2.add(cp2.neq(Germany , Netherlands));
cp2.add(cp2.le(cp2.prod(Germany,Germany),4));
if (cp2.solve())
{
System.out.println();
System.out.println( "Belgium: " + (int)cp2.getValue(Belgium));
System.out.println( "Denmark: " + (int)cp2.getValue(Denmark));
System.out.println( "France: " + (int)cp2.getValue(France));
System.out.println( "Germany: " + (int)cp2.getValue(Germany));
System.out.println( "Luxembourg: " + (int)cp2.getValue(Luxembourg));
System.out.println( "Netherlands: " + (int)cp2.getValue(Netherlands));
}
如果我稍微改变一下非常简单的 color.java 示例