ILOG CPLEX 11.0 User's Manual > Continuous Optimization > Solving Problems with a Quadratic Objective (QP) > Examples: Creating a QP, Optimizing, Finding a Solution > Example: QPex1.java

This example is almost identical to LPex1.java using only the function populatebyrow to create the model. Also, this function differs only in the creation of the objective from its LPex1.java counterpart. Here the objective function is created and added to the model like this:

 // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 )
      IloNumExpr x00 = model.prod( 33, x[0], x[0]);
      IloNumExpr x11 = model.prod( 22, x[1], x[1]);
      IloNumExpr x22 = model.prod( 11, x[2], x[2]);
      IloNumExpr x01 = model.prod(-12, x[0], x[1]);
      IloNumExpr x12 = model.prod(-23, x[1], x[2]);
      IloNumExpr Q   = model.prod(0.5, model.sum(x00, x11, x22, x01, x12));

      double[] objvals = {1.0, 2.0, 3.0};
      model.add(model.maximize(model.diff(model.scalProd(x, objvals), Q)));

A quadratic objective may be built with square, prod or sum methods. Note that inclusion of IloPiecewiseLinear will change the model from a QP to a MIQP.