void BNC::buildModelNF(){ env = new IloEnv; model = new IloModel(*env); variables = new IloNumVarArray(*env); constraints = new IloRangeArray(*env); //create variables for( int i = 0; i < n; ++i ){ variables->add( IloIntVar( *env, 0, 1 ) ); variables->add( IloIntVar( *env, 0, 1 ) ); } for( int i = 0, k = 0; i < n; ++i ){ for( int j = i; j < n; ++j ){ if( graph[i][j] ){ IloRange newconstraintA( *env, 0, 1 ); IloRange newconstraintB( *env, 0, 1 ); newconstraintA.setLinearCoef( (*variables)[i], 1 ); newconstraintA.setLinearCoef( (*variables)[j], 1 ); newconstraintB.setLinearCoef( (*variables)[n+i], 1 ); newconstraintB.setLinearCoef( (*variables)[n+j], 1 ); constraints->add( newconstraintA ); constraints->add( newconstraintB ); ++k; } } } for( int i = 0; i < n; ++i ){ IloRange newconstraintAB( *env, 0, 1 ); newconstraintAB.setLinearCoef( (*variables)[i], 1 ); newconstraintAB.setLinearCoef( (*variables)[n+i], 1 ); constraints->add( newconstraintAB ); } //objective function IloObjective obj = IloMaximize(*env); //objective for( int i = 0 ; i < n; i++ ){ obj.setLinearCoef((*variables)[i], 1 ); obj.setLinearCoef((*variables)[n+i], 1 ); } model->add( *constraints ); model->add( obj ); cplex = new IloCplex(*model); configureCPLEX(); //write model in file cplexmodel.lp cplex->exportModel("cplexmodel.lp"); }
void kMST_ILP::modelMTZ() { // Miller-Tucker-Zemlin model IloInt u_max = instance.n_nodes; // some u_i for each vertex u = IloIntVarArray(env, instance.n_nodes); for (unsigned int i=0; i<u.getSize(); i++) { u[i] = IloIntVar(env, 0, u_max, Tools::indicesToString("u", i).c_str()); } // 0 vertex has fixed value u[0].setUB(0); // if there is a connection x_ij, the u_j is greater than u_i for (unsigned int edgeId=0; edgeId < edges.getSize(); edgeId++) { Instance::Edge edgeInst = instance.edges[ edgeId % instance.n_edges ]; uint start = edgeInst.v1, end = edgeInst.v2; if (edgeId >= instance.n_edges) { // upper half uint tmp = start; start = end; end = tmp; } //cerr << "edge " << edgeId << " from " << start << " to " << end << endl; // u_start + edge < u_end + (1-edge) * M model.add( (u[start] + edges[edgeId]) - u[end] - ( ( 1 - edges[edgeId]) * u_max ) <= 0 ); } // if there are no incoming edges to a vertex, its u_i is maximal // this prevents any outgoing edges // the condition is not stated for node 0 to allow a start for (unsigned int vertex=1; vertex<instance.n_nodes; vertex++) { IloExpr incomingEdgesSum(env); { vector<u_int> incomingEdgeIds; getIncomingEdgeIds(incomingEdgeIds, vertex); for (unsigned int i=0; i<incomingEdgeIds.size(); i++) { incomingEdgesSum += edges[ incomingEdgeIds[i] ]; } } // if there are no incoming edges, the subtrahend is 0, so u_vertex is forced to be maximal // if there are incoming edges, the lhs is smaller or equal to 0, so the condition doesn't go into effect // NOTE: this is a quite loose model model.add( (u_max - (incomingEdgesSum * u_max)) <= u[vertex]); // this is the same but probably more efficient, maybe test with it: //model.add( IloIfThen(env, incomingEdgesSum == 0, u[vertex] == u_max) ); incomingEdgesSum.end(); } }
int main(int, const char * []) { IloEnv env; try { IloIntVarArray x(env); for (IloInt i = 0; i < 10; i++) { char name[6]; sprintf(name, "X%ld", i); x.add(IloIntVar(env, 0, 100 - 2*(i / 2), name)); } IloModel mdl(env); mdl.add(IloAllDiff(env, x)); mdl.add(x); IloIntVarChooser varChooser = ChooseSmallestCentroid(env); IloIntValueChooser valChooser = ChooseSmallestDistanceFromCentroid(env); IloSearchPhase sp1(env, x, varChooser, valChooser); IloIntVarEval varEval = Centroid(env); IloIntValueEval valEval = DistanceFromCentroid(env); IloSearchPhase sp2(env, x, IloSelectSmallest(varEval), IloSelectSmallest(valEval)); // sp2 can have ties as two variable or values could evaluate // to the same values. sp3 shows how to break these ties // choosing, for equivalent centroid and distance-to-centroid // evaluations, the lowest indexed variable in x and the // lowest value. IloVarSelectorArray selVar(env); selVar.add(IloSelectSmallest(varEval)); selVar.add(IloSelectSmallest(IloVarIndex(env, x))); // break ties on index IloValueSelectorArray selValue(env); selValue.add(IloSelectSmallest(valEval)); selValue.add(IloSelectSmallest(IloValue(env))); // break ties on smallest IloSearchPhase sp3(env, x, selVar, selValue); IloCP cp(mdl); cp.setParameter(IloCP::Workers, 1); cp.setParameter(IloCP::SearchType, IloCP::DepthFirst); cp.setParameter(IloCP::LogPeriod, 1); cp.out() << "Choosers" << std::endl; cp.solve(sp1); cp.out() << cp.domain(x) << std::endl; cp.out() << "Evaluators" << std::endl; cp.solve(sp2); cp.out() << cp.domain(x) << std::endl; cp.out() << "Evaluators (with tie-break)" << std::endl; cp.solve(sp3); cp.out() << cp.domain(x) << std::endl; cp.end(); } catch (IloException & ex) { env.out() << "Caught: " << ex << std::endl; } env.end(); return 0; }
static void populatebyrow (IloModel model, IloIntVarArray x, IloRangeArray c) { IloEnv env = model.getEnv(); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 2.0)); model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2] + x[3])); c.add( - x[0] + x[1] + x[2] + 10 * x[3] <= 20); c.add( x[0] - 3 * x[1] + x[2] <= 30); c.add( x[1] - 3.5* x[3] == 0); model.add(c); } // END populatebyrow
static void populatebynonzero (IloModel model, IloIntVarArray x, IloRangeArray c) { IloEnv env = model.getEnv(); IloObjective obj = IloMaximize(env); int n, a; scanf("%d", &n); scanf("%d", &a); //restrição c.add(IloRange(env, -IloInfinity, a)); //variaveis for(int i=0 ; i<n; i++){ x.add(IloIntVar(env, 0, 1)); } /*x.add(IloIntVar(env, 0.0, 40.0)); x.add(IloIntVar(env)); x.add(IloIntVar(env));*/ /*obj.setLinearCoef(x[0], 1.0); obj.setLinearCoef(x[1], 2.0); obj.setLinearCoef(x[2], 3.0);*/ /*restricoes*/ for(int i=0 ; i<n; i++){ scanf("%d", &a); c[0].setLinearCoef(x[i], a); } //objetivo for(int i=0 ; i<n; i++){ scanf("%d", &a); obj.setLinearCoef(x[i], a); } /*c[0].setLinearCoef(x[1], 1.0); c[0].setLinearCoef(x[2], 1.0); c[1].setLinearCoef(x[0], 1.0); c[1].setLinearCoef(x[1], -3.0); c[1].setLinearCoef(x[2], 1.0);*/ c[0].setName("c1"); for(int i=0; i<n; i++){ char tmp[10]; printf("x%d", i+1); x[i].setName(tmp); } model.add(obj); model.add(c); } // END populatebynonzero
/* $f_{ij} \in [0, k]$ variables denote the number of goods on edge (i, j). */ static IloIntVarArray createVarArrayFs(IloEnv env,vector<Instance::Edge> edges, u_int n_edges) { IloIntVarArray fs = IloIntVarArray(env, n_edges); for (u_int k = 0; k < n_edges; k++) { const u_int i = edges[k].v1; const u_int j = edges[k].v2; fs[k] = IloIntVar(env, 0, k, Tools::indicesToString("f", i, j).c_str()); } return fs; }
static void populatebycolumn (IloModel model, IloIntVarArray x, IloRangeArray c) { IloEnv env = model.getEnv(); IloObjective obj = IloMaximize(env); c.add(IloRange(env, -IloInfinity, 20.0, "c1")); c.add(IloRange(env, -IloInfinity, 30.0, "c2")); x.add(IloIntVar(obj(1.0) + c[0](-1.0) + c[1]( 1.0), 0.0, 40.0)); x.add(IloIntVar(obj(2.0) + c[0]( 1.0) + c[1](-3.0))); x.add(IloIntVar(obj(3.0) + c[0]( 1.0) + c[1]( 1.0))); x[0].setName("x1"); x[1].setName("x2"); x[2].setName("x3"); model.add(obj); model.add(c); } // END populatebycolumn
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int M=n*max_d; UB = M; /* Objective*/ model.add(IloMinimize(env, lambda)); //model.add(IloMinimize(env, IloSum(c))); /*Constrains*/ model.add(lambda - UB <= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } /* for (unsigned i=0; i<sqrt(n)-1; i=i+1) { for (unsigned j=0; j<sqrt(n)-1; j=j+1) { //square lattice model.add (c[i*sqrt(n)+j] +c[i*sqrt(n)+j+1] + c[(i+1)*sqrt(n)+j] +c[(i+1)*sqrt(n)+j+1]>= 16-4*sqrt(2)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // // triangular lattice // model.add (c[i*5+j]+c[i*5+j+1] + // c[(i+1)+j] >= 4); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // model.add (c[i*sqrt(n)+j]+c[i*sqrt(n)+j+1] + // c[(i+1)*sqrt(n)+j]+c[(i+1)*sqrt(n)+j+1] >= 22 - 4*sqrt(2)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 4-x } } */ /* for (unsigned i=0; i<sqrt(n)-2; i+=3) { for (unsigned j=0; j<sqrt(n)-2; j+=3) { // model.add (c[i*sqrt(n)+j] + c[i*sqrt(n)+j+1] + c[i*sqrt(n)+j+2] + // c[(i+1)*sqrt(n)+j]+ c[(i+1)*sqrt(n)+j+1]+ c[(i+1)*sqrt(n)+j+2] + // c[(i+2)*sqrt(n)+j]+ c[(i+2)*sqrt(n)+j+1]+ c[(i+2)*sqrt(n)+j+2] // >= 60-17*sqrt(2)-3*sqrt(5)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // model.add (c[i*sqrt(n)+j] + c[i*sqrt(n)+j+1] + c[i*sqrt(n)+j+2] + // c[(i+1)*sqrt(n)+j]+ c[(i+1)*sqrt(n)+j+1]+ c[(i+1)*sqrt(n)+j+2] + // c[(i+2)*sqrt(n)+j]+ c[(i+2)*sqrt(n)+j+1]+ c[(i+2)*sqrt(n)+j+2] // >= 82-8*sqrt(2)-2*sqrt(5)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 4-x } } */ for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); /* start = clock(); int solveError = cplex.solve(); end = clock (); */ IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
void BNC::buildModelCF(){ env = new IloEnv; model = new IloModel(*env); variables = new IloNumVarArray(*env); constraints = new IloRangeArray(*env); list< list<int> > cliques; int count_chose = 0; bool chose_edge[n][n]; for( int i = 0; i < n; ++i ) for( int j = 0; j < n; ++j ) chose_edge[i][j] = false; while( count_chose < m ){ list<int> current_clique; //choose a initial node for( int i = 0; i < n; ++i ){ for( int j = 0; j < n; ++j ){ if( graph[i][j] ){ if( !chose_edge[i][j] ){ chose_edge[i][j] = chose_edge[j][i] = true; ++count_chose; current_clique.push_back(i); current_clique.push_back(j); goto done; } } } } done: //build a clique int i = current_clique.front(); for( int j = 0; j < n; ++j ){ if( graph[i][j] ){ if( !chose_edge[i][j] ){ bool add_node = true; list<int>::iterator it = current_clique.begin(); while( it != current_clique.end() ){ if( !graph[*it][j] ){ add_node = false; break; } ++it; } if( add_node ){ { list<int>::iterator it = current_clique.begin(); while( it != current_clique.end() ){ if( !chose_edge[*it][j] ) ++count_chose; chose_edge[*it][j] = chose_edge[j][*it] = true; ++it; } } current_clique.push_back(j); } } } } cliques.push_back( current_clique ); } //create variables for( int i = 0; i < n; ++i ){ variables->add( IloIntVar( *env, 0, 1 ) ); variables->add( IloIntVar( *env, 0, 1 ) ); } list< list<int> >::iterator it1 = cliques.begin(); while( it1 != cliques.end() ){ list<int>::iterator it2 = it1->begin(); IloRange newconstraintA( *env, 0, 1 ); IloRange newconstraintB( *env, 0, 1 ); while( it2 != it1->end() ){ newconstraintA.setLinearCoef( (*variables)[*it2], 1 ); newconstraintB.setLinearCoef( (*variables)[n+*it2], 1 ); ++it2; } constraints->add( newconstraintA ); constraints->add( newconstraintB ); ++it1; } for( int i = 0; i < n; ++i ){ IloRange newconstraintAB( *env, 0, 1 ); newconstraintAB.setLinearCoef( (*variables)[i], 1 ); newconstraintAB.setLinearCoef( (*variables)[n+i], 1 ); constraints->add( newconstraintAB ); } //objective function IloObjective obj = IloMaximize(*env); //objective for( int i = 0 ; i < n; i++ ){ obj.setLinearCoef((*variables)[i], 1 ); obj.setLinearCoef((*variables)[n+i], 1 ); } model->add( *constraints ); model->add( obj ); cplex = new IloCplex(*model); configureCPLEX(); //write model in file cplexmodel.lp cplex->exportModel("cplexmodel.lp"); }
int CProblem::setModel() { //time_t start, end; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n); // for (unsigned int u = 0; u < n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u] = IloNumVar(env, str.c_str()); } IloIntVarArray z0(env, Info.numAddVar); for (int i = 0; i < Info.numAddVar; i++) { std::stringstream ss; ss << i; std::string str = "z0_" + ss.str(); z0[i] = IloIntVar(env, 0, 1, str.c_str()); } /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ /* d=function of the distance */ IloArray<IloNumArray> Par_d(env, n); for (unsigned int u = 0; u < n; u++) { Par_d[u] = IloNumArray(env, n); for (unsigned int v = 0; v < n; v++) { Par_d[u][v] = d[u][v]; } } int M = (max_distance + 1) * n; for (int i = 0; i < Info.numAddVar; i++) { int u = Info.ConstrIndex[i * 2]; int v = Info.ConstrIndex[i * 2 + 1]; model.add(c[u] - c[v] + M * (z0[i]) >= Par_d[u][v]); model.add(c[v] - c[u] + M * (1 - z0[i]) >= Par_d[u][v]); } // d(x) = 3 - x if (max_distance == 2) { // Gridgraphen 1x2 (6 Knoten) for (int i = 0; i < sqrt(n)-1; i+=1) { for (int j = 0; j < sqrt(n)-2; j+=2) { model.add(c[i * sqrt(n) + j] + c[i * sqrt(n) + j+2] + c[(i+1) * sqrt(n) + j] + c[(i+1) * sqrt(n) + j+2] >= 16.2273); } } // } //d(x) = 4 - x if (max_distance == 3) { // Gridgraphen 1x2 (6 Knoten) for (int i = 0; i < sqrt(n)-1; i+=1) { for (int j = 0; j < sqrt(n)-2; j+=2) { model.add(c[i * sqrt(n) + j] + c[i * sqrt(n) + j+2] + c[(i+1) * sqrt(n) + j] + c[(i+1) * sqrt(n) + j+2] >= 30.283); } } } for (unsigned int v = 0; v < n; v++) { model.add(c[v] <= lambda); model.add(c[v] >= 0); } std::cout << "Number of variables " << Info.numVar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if (!solveError) { std::cout << "STATUS : " << cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : " << cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; Info.numConstr = cplex.getNrows(); env.out() << " Number of constraints = " << Info.numConstr << "\n"; lambda_G_d = cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u = 0; u < n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u] << " "; } } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int M=n*max_d; UB = M; /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ model.add(lambda - UB <= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); /* start = clock(); int solveError = cplex.solve(); end = clock (); */ // cplex.setParam(IloCplex::Threads, 1); cplex.setParam(IloCplex::ClockType , 1 ); // CPU time IloTimer timer(env); const double startt = cplex.getTime(); timer.start(); int solveError = cplex.solve(); timer.stop(); const double stopt = cplex.getTime() - startt; if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; env.out() << "Time cplex.getTime " << stopt << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int UB, LB; switch (lattice){ case 1: // hexagonal_lattice if (max_d==3) {LB = 5; UB = 10;} //d(x) = 3 -x else {LB = 9; UB = 27;} break; case 2:// triangular_lattice if (max_d==3) { LB = 8; UB = 16;} //d(x) = 3 -x else {LB = 18; UB = 54;} break; case 3:// square_lattice if (max_d==3) { LB = 6; UB = 12;} //d(x) = 3 -x else { LB = 11; UB = 33;} break; default: UB=n*max_d;break; } std::cout << "Lattice " << lattice << "\n"; std::cout << "UB for lambda " << UB << "\n"; /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ model.add(lambda - UB <= 0); model.add(lambda - LB >= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } int M = INT_MAX;//Par_d[0][0] + UB; for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
ILOSTLBEGIN int main(int argc, char** argv) { IloEnv env ; try { IloModel model(env) ; IloCplex cplex(env) ; // Get data // Import data from file try { //IloInt i, j ; const char* filename ; if (argc > 1) filename = argv[1] ; else filename = "data/tsp.dat" ; ifstream f(filename, ios::in) ; if (!f) { cerr << "No such file: " << filename << endl ; throw(1) ; } // Create data matrix IloArray<IloNumArray> costmatrix (env) ; f >> costmatrix ; IloInt n = costmatrix.getSize() ; // Define variables and "fill" them so as not to get seg faults IloArray<IloIntVarArray> x (env, n) ; for (i=0 ; i<n ; i++) { x[i] = IloIntVarArray (env, n) ; for(IloInt j=0 ; j<n ; j++) { x[i][j] = IloIntVar (env) ; } } IloIntVarArray u (env, n) ; for(i=0 ; i<n ; i++) { u[i] = IloIntVar (env) ; } // Define constraints IloExpr constr_i (env) ; IloExpr constr_j (env) ; IloExpr constr_u (env) ; // Vertical constraint // Create a full vector with a hole to express the vertical constraint as a scalar product IloIntArray basevector (env, n) ; basevector[0] = 0 ; for (int i=1 ; i<n ; i++) { basevector[i] = 1 ; } constr_i = IloScalProd(basevector, x[0]) ; model.add( constr_i == 1) ; for (i=1 ; i < n ; i++) { basevector[i-1] = 1 ; basevector[i] = 0 ; constr_i = IloScalProd(basevector, x[i]) ; model.add(constr_i == 1) ; for (j=0 ; j < n ; j++) { if (i != j) { constr_j += x[j][i] ; constr_u = u[i] - u[j] + n*x[i][j] ; model.add(constr_u == 1) ; } } model.add(constr_j == 1) ; } // Define objective IloExpr obj (env) ; for (i=1 ; i < n ; i++) { for (j=0 ; j < n ; j++) { if (i != j) { obj += costmatrix[i][j] * x[i][j] ; } } } model.add( IloMinimize(env, obj) ) ; // Extract model cplex.extract(model); // Export model // Set parameters // Solve if ( !cplex.solve() ) { env.error() << "Failed to optimize problem" << endl; cplex.out() << "Solution status " << cplex.getStatus() << endl ; cplex.out() << "Model" << cplex.getModel() << endl ; throw(-1); } cplex.out() << "Solution status " << cplex.getStatus() << endl; cplex.out() << "Objective value " << cplex.getObjValue() << endl; // Print solution details } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } } catch (...) { cerr << "Unknown exception caught" << endl ; } // freeing memory env.end(); return 0; } // END main
Variables *kMST_ILP::modelMTZ() { MTZVariables *v = new MTZVariables(); /***** generic part ***/ const vector<Instance::Edge> edges = directed_edges(instance.edges); const u_int n_edges = edges.size(); /* $x_{ij} \in \{0, 1\}$ variables denote whether edge (i, j) is active. */ v->xs = createVarArrayXs(env, edges, n_edges); /* $v_i \in \{0, 1\}$ variables denote whether node i is active. */ v->vs = createVarArrayVs(env, instance.n_nodes); /* add objective function */ addObjectiveFunction(env, model, v->xs, edges, n_edges); /* There are exactly k - 1 edges not counting edges from the artificial root node 0. */ addConstraint_k_minus_one_active_edges(env,model,v->xs,edges,n_edges,this->k); /* Exactly one node is chosen as the tree root. */ addConstraint_one_active_outgoing_arc_for_node_zero(env,model,v->xs,edges,n_edges); /* No edge leads back to the artificial root node 0. */ addConstraint_no_active_incoming_arc_for_node_zero(env,model,v->xs,edges,n_edges); IloExprArray e_in_degree = createExprArray_in_degree(env, edges, n_edges, v->xs, instance); IloExprArray e_out_degree = createExprArray_out_degree(env, edges, n_edges, v->xs, instance); /* Inactive nodes have no outgoing active edges, active ones at most k - 1. TODO: A tighter bound is to take the sum of incoming goods - 1.*/ addConstraint_bound_on_outgoing_arcs(model,v->vs,e_out_degree,instance,this->k); /* Active nodes have at least one active arc.*/ addConstraint_active_node_at_least_one_active_arc(model,v->vs,e_in_degree, e_out_degree,instance); /* Exactly one incoming edge for an active node and none for an inactive node (omitting artificial root). */ addConstraint_in_degree_one_for_active_node_zero_for_inactive(model,v->vs,e_in_degree,instance); //note: position matters. Tried worse positions than this one /* $\sum_{i > 0} v_i = k$. Ensure that exactly k nodes are active. */ addConstraint_k_nodes_active(env, model, v->vs, instance, this->k); e_in_degree.endElements(); e_out_degree.endElements(); /***** MTZ specific part ***/ /* $u_i \in [0, k]$ variables are used to impose an order on nodes. */ v->us = IloIntVarArray(env, instance.n_nodes); for (u_int i = 0; i < instance.n_nodes; i++) { v->us[i] = IloIntVar(env, 0, k, Tools::indicesToString("u", i).c_str()); } IloExpr e3(env); e3 += v->us[0]; /* $u_0 = 0$. Set level of artificial root 0 to 0. */ model.add(e3 == 0); e3.end(); for (u_int k = 0; k < n_edges; k++) { const u_int i = edges[k].v1; const u_int j = edges[k].v2; IloExpr e4(env); e4 = v->us[i] + v->xs[k] - v->us[j] - (-v->xs[k] + 1) * this->k; /* $\forall i, j: u_i + x_{ij} \leq u_j + (1 - x_{ij})k$. * Enforce order hierarchy on nodes. */ model.add(e4 <= 0); e4.end(); } for (u_int i = 0; i < instance.n_nodes; i++) { /* $\forall i: u_i <= nv_i$ force order of inactive nodes to 0 */ /* helps with big instances 6,7,8 */ model.add(v->us[i] <= v->vs[i] * (int) instance.n_nodes); } return v; }