// -------------------------------------------------------------- -*- C++ -*-
// File: examples/src/ilolpex3.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.
// --------------------------------------------------------------------------
//
// ilolpex3.cpp, example of adding constraints to solve a problem
//
// Modified example from Chvatal, "Linear Programming", Chapter 26.
// minimize c*x
// subject to Hx = d
// Ax = b
// l <= x <= u
// where
//
// H = ( -1 0 1 0 1 0 0 0 ) d = ( -3 )
// ( 1 -1 0 1 0 0 0 0 ) ( 1 )
// ( 0 1 -1 0 0 1 -1 0 ) ( 4 )
// ( 0 0 0 -1 0 -1 0 1 ) ( 3 )
// ( 0 0 0 0 -1 0 1 -1 ) ( -5 )
//
// A = ( 2 1 -2 -1 2 -1 -2 -3 ) b = ( 4 )
// ( 1 -3 2 3 -1 2 1 1 ) ( -2 )
//
// c = ( -9 1 4 2 -8 2 8 12 )
// l = ( 0 0 0 0 0 0 0 0 )
// u = ( 50 50 50 50 50 50 50 50 )
//
//
//
// Treat the constraints with A as the complicating constraints, and
// the constraints with H as the "simple" problem.
//
// The idea is to solve the simple problem first, and then add the
// constraints for the complicating constraints, and solve with dual.
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
int main()
{
IloEnv env;
try {
IloModel model(env, "chvatal");
IloNumVarArray x(env, 8, 0, 50);
model.add(IloMinimize(env,-9*x[0] + x[1] + 4*x[2] + 2*x[3]
-8*x[4] + 2*x[5] + 8*x[6] + 12*x[7]));
model.add(-x[0] + x[2] + x[4] == -3);
model.add( x[0] - x[1] + x[3] == 1);
model.add( x[1] - x[2] + x[5] - x[6] == 4);
model.add( - x[3] - x[5] + x[7] == 3);
model.add( - x[4] + x[6] - x[7] == -5);
IloCplex cplex(model);
cplex.setParam(IloCplex::SimDisplay, 2);
cplex.setParam(IloCplex::RootAlg, IloCplex::Network);
cplex.solve();
cplex.out() << "After network optimization, objective is "
<< cplex.getObjValue() << endl;
model.add(2*x[0] + 1*x[1] - 2*x[2] - 1*x[3] +
2*x[4] - 1*x[5] - 2*x[6] - 3*x[7] == 4);
model.add(1*x[0] - 3*x[1] + 2*x[2] + 3*x[3] -
1*x[4] + 2*x[5] + 1*x[6] + 1*x[7] == -2);
cplex.setParam(IloCplex::RootAlg, IloCplex::Dual);
cplex.solve();
IloNumArray vals(env);
cplex.getValues(vals, x);
cplex.out() << "Solution status " << cplex.getStatus() << endl;
cplex.out() << "Objective value " << cplex.getObjValue() << endl;
cplex.out() << "Solution is: " << vals << endl;
cplex.exportModel("lpex3.sav");
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
} // END main
|