Example: iloqpex1.cpp

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

  model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2]
                - 0.5 * (33*x[0]*x[0] + 22*x[1]*x[1] + 11*x[2]*x[2]
                              - 12*x[0]*x[1] - 23*x[1]*x[2]) ));

In general, any expression built of basic operations +, -, *, / constant, and brackets '()' that amounts to a quadratic and optional linear term can be used for building QP objective function. Note that, if the expressions of the objective or any constraint of the model contains IloPiecewiseLinear, then when a quadratic objective is specified the model becomes an MIQP problem.

Complete Program: iloqpex1.cpp

The complete program, iloqpex1.cpp, appears here and online in the standard distribution.

  // -------------------------------------------------------------- -*- C++ -*-
  // File: examples/src/iloqpex1.cpp
  // Version 8.1
  // --------------------------------------------------------------------------
  //  Copyright (C) 1999-2002 by ILOG.
  //  All Rights Reserved.
  //  Permission is expressly granted to use this example in the
  //  course of developing applications that use ILOG products.
  // --------------------------------------------------------------------------
  //
  // iloqpex1.cpp - Entering and optimizing a quadratic problem.
  
  #include <ilcplex/ilocplex.h>
  ILOSTLBEGIN
  
  static void
     populatebyrow     (IloModel model, IloNumVarArray var, IloRangeArray con);
  
  int
  main (int argc, char **argv)
  {
     IloEnv   env;
     try {
        IloModel model(env);
        IloNumVarArray var(env);
        IloRangeArray con(env);
  
        populatebyrow (model, var, con);
  
        IloCplex cplex(model);
  
        // Optimize the problem and obtain solution.
        if ( !cplex.solve() ) {
           env.error() << "Failed to optimize LP" << endl;
           throw(-1);
        }
  
        IloNumArray vals(env);
        env.out() << "Solution status = " << cplex.getStatus() << endl;
        env.out() << "Solution value  = " << cplex.getObjValue() << endl;
        cplex.getValues(vals, var);
        env.out() << "Values        = " << vals << endl;
        cplex.getSlacks(vals, con);
        env.out() << "Slacks        = " << vals << endl;
        cplex.getDuals(vals, con);
        env.out() << "Duals         = " << vals << endl;
        cplex.getReducedCosts(vals, var);
        env.out() << "Reduced Costs = " << vals << endl;
  
        cplex.exportModel("qpex1.lp");
     }
     catch (IloException& e) {
        cerr << "Concert exception caught: " << e << endl;
     }
     catch (...) {
        cerr << "Unknown exception caught" << endl;
     }
  
     env.end();
  
     return 0;
  }  // END main
  
  
  // To populate by row, we first create the variables, and then use them to
  // create the range constraints and objective.  The model we create is:
  //
  //    Maximize
  //     obj: x1 + 2 x2 + 3 x3
  //            - 0.5 ( 33*x1*x1 + 22*x2*x2 + 11*x3*x3
  //                             - 12*x1*x2 - 23*x2*x3 )
  //    Subject To
  //     c1: - x1 + x2 + x3 <= 20
  //     c2: x1 - 3 x2 + x3 <= 30
  //    Bounds
  //     0 <= x1 <= 40
  //    End
  
  static void
  populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
  {
     IloEnv env = model.getEnv();
  
     x.add(IloNumVar(env, 0.0, 40.0));
     x.add(IloNumVar(env));
     x.add(IloNumVar(env));
     model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2]
                              - 0.5 * (33*x[0]*x[0] + 22*x[1]*x[1] +
                                       11*x[2]*x[2] - 12*x[0]*x[1] -
                                       23*x[1]*x[2]                 ) ));
  
     c.add( - x[0] +     x[1] + x[2] <= 20);
     c.add(   x[0] - 3 * x[1] + x[2] <= 30);
     model.add(c);
  }  // END populatebyrow
  


Previous Page: Example: Creating a QP, Optimizing, Finding a Solution  Return to Top Next Page: Example: QPex1.java