示例#1
0
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");
}
示例#2
0
CPlexSolver::CPlexSolver(const Matrix& A, unsigned int lambda) :
env(), model(env), vars(env), constraints(env),
solutionVectors() {
	// Add variables
	for (int i = 0; i < A.shape()[1]; i++) {
		vars.add(IloBoolVar(env));						// For simple t-designs
	}
	
	// Add constraints
	for (int i = 0; i < A.shape()[0]; i++) {
		constraints.add(IloRange(env, lambda, lambda));		// RHS = lambda in every equation
	}
	
	// Set up model to have as few block orbits as possible
	IloObjective objective = IloMinimize(env);
	for (int i = 0; i < A.shape()[1]; i++) {
		objective.setLinearCoef(vars[i], 1);
	}
	
	// Set up constraints according to input matrix
	for (int i = 0; i < A.shape()[0]; i++) {
		for (int j = 0; j < A.shape()[1]; j++) {
			constraints[i].setLinearCoef(vars[j], A[i][j]);
		}
	}
	
	// TODO - names for constraints/vars may be necessary
	// Might have to name these after the row/col labels for K-M Matrix
	
	// Finish setting up the model
	model.add(objective);
	model.add(constraints);
}
示例#3
0
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
示例#4
0
static void
populatebynonzero(IloModel model, NumVarMatrix varOutput, NumVar3Matrix varHelp, Range3Matrix con)
{
	IloEnv env = model.getEnv();

	IloObjective obj = IloMaximize(env); //maximization function


	for (int j = current; j < current + J; ++j)
	{
		for (int k = 0; k < K; ++k)
		{
			
			obj.setLinearCoef(varOutput[j][k], 1.0);//add all variables to objective function, factor 1 
			
			//constraint 0: express value of output objective variables
			model.add(varOutput[j][k] + varHelp[j][k][2] - varHelp[j][k][3] == 0);

			//constraint 1: Td2a>=+Td2b
			model.add(varHelp[j][k][5] - varHelp[j][k][4] >= 0);

			//constraint 2: Tj>=Td2a + Tdc + Tblow
			model.add(varHelp[j][k][5] <= T[j] - Tdc - Tblow[j] - Tslack);

			//constraint 3: Td2b = Tfa+Tfd
			model.add(Tfd[k] == varHelp[j][k][4] - varHelp[j][k][3]);

			//constraint 4: Td1a >= Td1b
			model.add(0 <= varHelp[j][k][1] - varHelp[j][k][0]);

			//constraint 5: Tfb >= Td1a+Tdf
			model.add(Tdf[k] <= varHelp[j][k][2] - varHelp[j][k][1]);

			//constraint 6: Td1b = T(j-a)+Tcd
			model.add(T[j - a[k]] + Tcd == varHelp[j][k][0]);

			//constraint 7: Td1a >= Td2b(j-b) + Tloss, 1
			model.add(TlossD[k] <= varHelp[j][k][1] - varHelp[j - b[k]][k][4]);

			//constraint 8: Tfb >= Tfa(j-1)+Tloss, 2
			model.add(TlossF[k] <= varHelp[j][k][2] - varHelp[j - 1][k][3]);

			//constraint 9: at least X s for every load
			model.add(varOutput[j][k] >= TloadMin[k]);

		}
		//constraint 10: both spoons are picked up at same time at dropoff: Td2a,1 == Td2a,2
		model.add(varHelp[j][1][5] == varHelp[j][0][5]);

	}

	model.add(obj);
	

}
void generateProblem(const ILPModel& m, IloModel& model, IloNumVarArray& x, IloRangeArray& con)	{
    IloEnv env = model.getEnv();
    IloObjective obj = (m.obj == MINIMIZE ? IloMinimize(env) : IloMaximize(env));
    for (unsigned long v = 0; v < m.numberOfVariables(); ++v)	{
        switch (m.x[v].type)	{
        case FLT:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Float));
            break;
        case BIN:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Bool));
            break;
        default:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Int));
        }

        obj.setLinearCoef(x[v], m.c[v]);
        x[v].setName(m.varDesc[v].c_str());
    }

    for (unsigned long c = 0; c < m.numberOfConstraints(); ++c)	{
        switch (m.ops[c])	{
        case LESS_EQUAL:
            con.add(IloRange(env, -IloInfinity, m.b[c]));
            break;
        case EQUAL:
            con.add(IloRange(env, m.b[c], m.b[c]));
            break;
        case GREATER_EQUAL:
            con.add(IloRange(env, m.b[c], IloInfinity));
        }

        for (const pair<uint32_t, double>& p : m.A[c])
            con[c].setLinearCoef(x[p.first], p.second);

        con[c].setName(m.conDesc[c].c_str());
    }

    model.add(obj);
    model.add(con);
}
示例#6
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");
}
void Instance::optKara2007()
{
	CPUTimer t;
	t.start();

	int size = Gifts.size();
	double totalWeight = 0;
	for (int i = 0; i < (int)Gifts.size(); i++)
	{
		totalWeight += Gifts[i].weight;
	}

	cout  << "Optimizing Instance by Kara2007 Two-Index One-Commodity Flow Integer Model to CPLEX..." << endl;
	cout  << "Number of Gifts:\t" << size << endl;

	vector<vector<double> > c(size+1);

	for (int i = 0; i < size+1; i++)
	{
		c[i].resize(size+1);
	}

	for (int i = 0; i < size+1; i++)
	{
		for (int j = i+1; j < size+1; j++)
		{
			c[i][j] = getDistance(getGiftId(i), getGiftId(j));			
			c[j][i] = c[i][j];
		} 
	}

	IloEnv env;
	IloModel model(env);		
	IloObjective obj = IloMinimize(env);
	NumVarMatrix var_x(env);
	NumVarMatrix var_f(env);
	string varName;
	string consName;
	
	for (int i = 0; i < size+1; i++)
	{
		var_x.add(IloNumVarArray(env));
		var_f.add(IloNumVarArray(env));
	}

	varName = "K";
	IloNumVar var_K(env, 1, size, IloNumVar::Int, (char*)varName.c_str());

	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{
			varName = "x" + to_string(getGiftId(i)) + "_" + to_string(getGiftId(j));
			var_x[i].add( IloNumVar(env, 0, 1, IloNumVar::Int, (char*)varName.c_str()) );

			varName = "f" + to_string(getGiftId(i)) + "_" + to_string(getGiftId(j));
			var_f[i].add( IloNumVar(env, 0, 1010, IloNumVar::Float, (char*)varName.c_str()) );
		}
	}

	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{
			obj.setLinearCoef(var_x[i][j], 10*c[i][j]);
			obj.setLinearCoef(var_f[i][j], c[i][j]);
		}
	}
	model.add(obj);

	//numeracao segundo Fukasawa 2015
	// (1a) - tudo que sai de todo i
	for (int i = 0; i < size; i++)
	{
		consName = "c1a_" + to_string(getGiftId(i));
		IloRange c1a(env, 1, 1, (char*)consName.c_str());

		for (int j = 0; j < size+1; j++)
		{
			if (c[i][j] > 0)
			{
				c1a.setLinearCoef(var_x[i][j], 1);
			}
		}
		model.add(c1a);
	}

	// (1b) - tudo que entra em todo i
	for (int i = 0; i < size; i++)
	{
		consName = "c1b_" + to_string(getGiftId(i));
		IloRange c1b( env, 1, 1, (char*)consName.c_str() );

		for (int j = 0; j < size+1; j++)
		{
			if (c[i][j] > 0)
			{
				c1b.setLinearCoef(var_x[j][i], 1);
			}
		}
		model.add(c1b);
	}
	
	//(1c) ensures that q_i units are delivered
	//tudo que sai de i deve ser menor w_i de tudo que entra
	for (int i = 0; i < size; i++)
	{
		consName = "c1c_" + to_string(getGiftId(i));
		IloRange c1c( env, getWeight(i), getWeight(i), (char*)consName.c_str() );

		for (int j = 0; j < size+1; j++)
		{
			if (c[j][i] > 0) //tudo que entra
			{
				c1c.setLinearCoef(var_f[j][i], 1);
			}
			if (c[i][j] > 0) //tudo que sai
			{
				c1c.setLinearCoef(var_f[i][j], -1);
			}
			
		}
		model.add(c1c);
	}

	consName = "c1c_0";
	IloRange c1c( env, totalWeight, totalWeight, (char*)consName.c_str() );

	for (int j = 0; j < size; j++)
	{
		if (c[size][j] > 0)
		{
			c1c.setLinearCoef(var_f[size][j], 1);
		}
		if (c[j][size] > 0)
		{
			c1c.setLinearCoef(var_f[j][size], -1);
		}
	}
	model.add(c1c);

	//(1d) f bounds
	for (int i = 0; i < size+1; i++)
	{
		for (int j = 0; j < size+1; j++)
		{		
			if (c[i][j] > 0)
			{
				model.add(IloConstraint( getWeight(j)* var_x[i][j] <= var_f[i][j] )); //o que entra em j deve pelo menos carregar o peso de j
				model.add(IloConstraint( var_f[i][j] <= (totalWeight - getWeight(i)) * var_x[i][j] )); //o que sai de i nao pode carregar o peso de i
			}			
		}
	}

	// (1e) - tudo que entra em 0	
	consName = "c1e_in";
	IloRange c1e_in( env, 0, 0, (char*)consName.c_str() );
	consName = "c1e_out";
	IloRange c1e_out( env, 0, 0, (char*)consName.c_str() );

	c1e_in.setLinearCoef(var_K, -1);
	c1e_out.setLinearCoef(var_K, -1);

	for (int i = 0; i < size; i++)
	{
		if (c[i][size] > 0)
		{
			c1e_in.setLinearCoef(var_x[i][size], 1);
		}
		if (c[size][i] > 0)
		{
			c1e_out.setLinearCoef(var_x[size][i], 1);
		}
	}
	model.add(c1e_in);
	model.add(c1e_out);	
		
	IloCplex cplex(model);

	#ifdef _WIN32
		cplex.setParam(IloCplex::Threads,1);
		//cplex.setParam(IloCplex::TiLim,10);
		//cplex.setParam(IloCplex::VarSel,3);
	#else
		//cplex.setParam(IloCplex::TiLim,60);
	#endif	
		
	cplex.setParam(IloCplex::CutUp, 13632670.39);

	try {
		cplex.exportModel("trip_kara2007.lp"); 
	}
	catch (IloException e) 
	{
		cerr << e;
	}		
		
	try {
		if ( cplex.solve() ) 
		{   
			cout << "Solution status = " << cplex.getStatus() << endl;
			double objValue = cplex.getObjValue();
			cout << "Best Solution Found:\t" << setprecision(17) << objValue << endl;
			cout << "Num Trips:\t" << setprecision(17) << cplex.getValue(var_K) << endl;

			vector<vector<int> > adjacencyMatrix (size+1);
			for (int i = 0; i < size+1; i++)
			{
				adjacencyMatrix[i].resize(size + 1);
			}
    
			for (int i = 0; i < size+1; i++)
			{
				for (int j = 0; j < size+1; j++)
				{	
					if (c[i][j] > 0)
					{				
						double val = cplex.getValue(var_x[i][j]);
						double valf = cplex.getValue(var_f[i][j]);
						if (val != 0)
						{
							cout << var_x[i][j].getName() << ": " << val << endl;
							cout << var_f[i][j].getName() << ": " << valf << endl;

							adjacencyMatrix[i][j] = 1;
							//adjacencyMatrix[j][i] = 1;
						}
					}				
				}	
			}

			vector<bool> visited(size+1);

			//starts at north pole
			visited[size] = true;
			int last = size;
	
			/*Trip Kara_Trip();

			while (Kara_Trip.getSize() < size)
			{
				for (int j = 0; j < size+1; j++)
				{
					if ( ( adjacencyMatrix[last][j] == 1) && (!visited[j] ) )
					{
						visited[j] = true;	
						Kara_Trip.appendGift(inst, Gifts[j-1]);
						last = j;
						break;
					}
				}
			}

			cout << "OriginalTripCost:\t" << std::setprecision(17) << getCost() << endl;
			cout << "OptimalKaraTripCost:\t" << std::setprecision(17) << Kara_Trip.getCost() << endl;*/
		}
	}
	catch (IloException e) 
	{
		cerr << e;
	}

	env.end();
}