-1

How do I express a quadratic objective function in Constraint Programming? In Cplex, I write it as follows:

IloLQNumExpr objfn = model.lqNumExpr()

but it is not the same in CP.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Ridvan
  • 13
  • 1

1 Answers1

1

very simple with CP Optimizer.

For example:

cp2.add(cp2.le(cp2.prod(Germany,Germany),4));

in

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));
                }

if I change a bit the very simple color.java example

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thank you for the answer. I want to define an expression for the objective function. Lets call the objective function as W=x^2+2x, and i want to maximize W. How to declare W? – Ridvan Nov 21 '19 at 10:16
  • Hi, cp2.add(cp2.eq(w,cp2.sum(cp2.prod(x,x),cp2.prod(2,x)))); will do that – Alex Fleischer Nov 21 '19 at 12:00