1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <cmath> #include <string> #include <iostream> #include <ilcplex/ilocplex.h>
ILOSTLBEGIN
#define TESTTOL 1e-9
#define CONVTOL 1e-9
static void createmodel(IloModel& model, IloObjective &obj, IloNumVarArray &x, IloRangeArray &rngs, IloIntArray& cone) { IloEnv env = model.getEnv();
x.add(IloNumVar(env, -IloInfinity, IloInfinity)); x.add(IloNumVar(env, -IloInfinity, IloInfinity)); x.add(IloNumVar(env, -IloInfinity, IloInfinity)); x.add(IloNumVar(env, 0, IloInfinity)); x.add(IloNumVar(env, -IloInfinity, IloInfinity)); x.add(IloNumVar(env, -IloInfinity, IloInfinity)); x.add(IloNumVar(env, 0, IloInfinity));
obj = IloMinimize(env, x[0]+x[1] + x[2] + x[3] + x[4] + x[5]); model.add(x[0] + x[1] + x[4]==8); model.add(x[2] + x[4] + x[5]==10); model.add(x[6] - x[0] - x[1] == 0); model.add(-x[6] * x[6]+ x[0] * x[0] + x[1] * x[1] + x[2] * x[2]<=0);
double a[] = {0,0,0, -1,1 }; IloExpr temp(env); for (IloInt i = 3; i < 5; i++) { temp += a[i] * x[i] * x[i]; } model.add(temp <= 0); temp.end();
model.add(obj); }
int main(void) { IloEnv env; int retval = -1;
try { IloModel model(env); IloCplex cplex(env); IloObjective obj(env); IloNumVarArray vars(env); IloRangeArray rngs(env); IloIntArray cone(env); createmodel(model, obj, vars, rngs, cone);
cplex.extract(model);
cplex.setParam(IloCplex::Param::Barrier::QCPConvergeTol, CONVTOL); if (!cplex.solve() || cplex.getStatus() != IloAlgorithm::Optimal) throw string("Failed to solve problem to optimality");
IloNumArray vals_x(env); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; cplex.getValues(vals_x, vars); env.out() << "Values = " << vals_x << endl; env.end(); } catch (IloException &e) { cerr << "IloException: " << e << endl; if (env.getImpl()) env.end(); ::abort(); } catch (string& e) { cerr << e << endl; if (env.getImpl()) env.end(); ::abort(); } return retval; }
|