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();
	}
}
Exemple #2
0
// This routine creates the master ILP (arc variables x and degree constraints).
//
// Modeling variables:
// forall (i,j) in A:
//    x(i,j) = 1, if arc (i,j) is selected
//           = 0, otherwise
//
// Objective:
// minimize sum((i,j) in A) c(i,j) * x(i,j)
//
// Degree constraints:
// forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1
// forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1
//
// Binary constraints on arc variables:
// forall (i,j) in A: x(i,j) in {0, 1}
//
void
createMasterILP(IloModel mod, Arcs x, IloNumArray2 arcCost)
{
   IloInt i, j;
   IloEnv env = mod.getEnv();
   IloInt numNodes = x.getSize();

   // Create variables x(i,j) for (i,j) in A 
   // For simplicity, also dummy variables x(i,i) are created.
   // Those variables are fixed to 0 and do not partecipate to 
   // the constraints.

   char varName[100];
   for (i = 0; i < numNodes; ++i) {
      x[i] = IloIntVarArray(env, numNodes, 0, 1);
      x[i][i].setBounds(0, 0); 
      for (j = 0; j < numNodes; ++j) {
         sprintf(varName, "x.%d.%d", (int) i, (int) j); 
         x[i][j].setName(varName);
      }
      mod.add(x[i]);
   }
  
   // Create objective function: minimize sum((i,j) in A ) c(i,j) * x(i,j)

   IloExpr obj(env);
   for (i = 0; i < numNodes; ++i) {
      arcCost[i][i] = 0;
      obj += IloScalProd(x[i], arcCost[i]);
   }
   mod.add(IloMinimize(env, obj));
   obj.end();

   // Add the out degree constraints.
   // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1

   for (i = 0; i < numNodes; ++i) {
      IloExpr expr(env);
      for (j = 0;   j < i; ++j)  expr += x[i][j];
      for (j = i+1; j < numNodes; ++j)  expr += x[i][j];
      mod.add(expr == 1);
      expr.end();
   }

   // Add the in degree constraints.
   // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1

   for (i = 0; i < numNodes; i++) {
      IloExpr expr(env);
      for (j = 0;   j < i; j++)  expr += x[j][i];
      for (j = i+1; j < numNodes; j++)  expr += x[j][i];
      mod.add(expr == 1);
      expr.end();
   }

}// END createMasterILP
Exemple #3
0
/* $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;
}
Exemple #4
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));
		//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;
}
Exemple #5
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;
}
Exemple #6
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;
}
Exemple #7
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         
Exemple #8
0
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());
		}

		IloArray<IloIntVarArray> z0(env,n);
		for (unsigned int i=0; i<n; i++)
		{
		    std::stringstream ss;
		    ss << i;
		    std::string str = "z0_" + ss.str();
		    z0[i]= IloIntVarArray(env, n, 0, 1);
		}
		/*
		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]);

		}
		*/
		for (unsigned u=0; u<n; u++)
		{
		  for (unsigned v=0; v<u; v++)
		  {
		    if (Par_d[u][v] <= max_distance)
		    {
	    		model.add(c[v]-c[u]+M*z0[u][v] >=Par_d[u][v]);
	    		model.add(c[u]-c[v]+M*(1-z0[u][v]) >=Par_d[u][v]);
			//Info.numvar++;
		    }
		  }
		}


		
		// d(x) = 3 - x
		if (max_distance == 2) 
		{
		  // Sechsecke mit der Seitenlaenge 1
		  model.add(c[0]+c[2] +c[3] +c[5] +c[6] +c[9]>=16.6077);
		  model.add(c[1]+c[3] +c[4] +c[6] +c[7] +c[10]>=16.6077);
		  model.add(c[5]+c[8] +c[9] +c[12]+c[13]+c[16]>=16.6077);
		  model.add(c[6]+c[9] +c[10]+c[13]+c[14]+c[17]>=16.6077);
		  model.add(c[7]+c[10]+c[11]+c[14]+c[15]+c[18]>=16.6077);
		  model.add(c[13]+c[16]+c[17]+c[19]+c[20]+c[22]>=16.6077);
		  model.add(c[14]+c[17]+c[18]+c[20]+c[21]+c[23]>=16.6077);
		}
		
		//d(x) = 4 - x	
		if (max_distance == 3) 
		{
		  // Sechsecke mit der Seitenlaenge 1
		  model.add(c[0]+c[2] +c[3] +c[5] +c[6] +c[9]>=31.6077);
		  model.add(c[1]+c[3] +c[4] +c[6] +c[7] +c[10]>=31.6077);
		  model.add(c[5]+c[8] +c[9] +c[12]+c[13]+c[16]>=31.6077);
		  model.add(c[6]+c[9] +c[10]+c[13]+c[14]+c[17]>=31.6077);
		  model.add(c[7]+c[10]+c[11]+c[14]+c[15]+c[18]>=31.6077);
		  model.add(c[13]+c[16]+c[17]+c[19]+c[20]+c[22]>=31.6077);
		  model.add(c[14]+c[17]+c[18]+c[20]+c[21]+c[23]>=31.6077);
		}
		
		

		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");

		cplex.setParam(IloCplex::ClockType,1);
		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;
}
Exemple #9
0
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;
}
Exemple #10
0
int main(int argc, const char * argv[]) {

    IloEnv env;
    try {
        IloModel model(env);
        int n = 9;
        IloArray<IloIntVarArray> matrix(env,n);
        //All elements in a  row are different
        for(int i=0;i<n;i++)
        {
            matrix[i] = IloIntVarArray(env,n,1,9);
            for(int j=0;j<n;j++)
            {
                model.add(IloAllDiff(env,matrix[i]));
            }
        }
        //All elements in a coloum are different
        for(int i=0;i<n;i++)
        {
            IloIntVarArray window(env);
            for(int j=0;j<n;j++)
            {
                window.add(matrix[j][i]);
            }
            model.add(IloAllDiff(env,window));
        }
        //All elements in each of the 3x3sub-squares are different
        for(int i=0;i<9;i+=3)
        {
            for(int j=0;j<9;j+=3)
            {
                IloIntVarArray window(env);
                for(int k=i;k<i+3;k++)
                {
                    for(int l=j;l<j+3;l++)
                    {
                        window.add(matrix[k][l]);
                    }
                }
                model.add(IloAllDiff(env,window));
            }
        }
        //Already provided numbers
        int PreSolve[][9] = {9,5,0,3,7,0,0,0,0,
                            0,0,0,0,0,0,0,0,0,
                            0,0,4,0,8,0,5,7,0,
                            3,9,0,0,0,0,0,0,4,
                            0,0,5,0,4,0,8,0,0,
                            8,0,0,0,0,0,0,1,7,
                            0,6,8,0,3,0,7,0,0,
                            0,0,0,0,0,0,0,0,0,
                            0,0,0,0,1,8,0,2,9};
        //Add presolved amtrix
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(PreSolve[i][j] != 0)
                model.add(matrix[i][j] == PreSolve[i][j]);
            }
        }
        //Solve
        IloCP cp(model);
        if(cp.solve())
        {
            for(IloInt i = 0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    cp.out() << cp.getValue(matrix[i][j]) << "\t";
                }
                cp.out()<<std::endl;
            }
        }
        
    }
    catch (IloException & ex) {
        env.out() << "Exception Caught : " << ex << std::endl;
    }
}