需要帮助才能为我的CPLEX旅行销售人员代码添加约束

时间:2017-04-24 19:46:49

标签: cplex

我已经为TSP问题编写了下面的代码,但它有一些子游览。必须消除分巡回赛。这些约束必须添加到代码中,但我现在不知道如何。

U1 = 1

所有我的第1个

2< = Ui< = n

Ui-Uj + 1< =(n-1)(1-Xij)所有i和j除1

code:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <ilcplex\ilocplex.h>

using namespace std;
ILOSTLBEGIN


typedef IloArray<IloBoolVarArray> IloBoolVarArray2;


const int NUMCITIES = 10;

int main() 
{


std::cout << "////////////////////Beginning the math programming example////////////////////" << endl;

long int i, j;
long int CityDistance[NUMCITIES][NUMCITIES] = {99,8,5,9,12,14,12,16,17,22,8,99,9,15,16,8,11,18,14,22,5,9,99,7,9,11,7,12,12,17,9,15,7,99,3,17,10,7,15,15,12,16,9,3,99,8,10,6,15,15,14,8,11,17,8,99,9,14,8,16,12,11,7,10,10,9,99,8,6,11,16,18,12,7,6,14,8,99,11,11,17,14,12,15,15,8,6,11,99,10
22,22,17,15,15,16,11,11,10,99};

std::cout <<"Distance matrix: " << endl;
for(i=0;i<NUMCITIES;i++)
{
    for(j=0;j<NUMCITIES;j++)
        std::cout << CityDistance[i][j] << ", ";
    std::cout << endl;
}
std::cout << endl;

for(ii=0;ii<NUMCITIES;ii++)
    X[ii] = IloBoolVarArray(env,NUMCITIES); //Define "2nd" dimension

for(ii=0;ii<NUMCITIES;ii++)
{
    IloExpr constraint1(env);
        for(jj=0;jj<NUMCITIES;jj++)
            constraint1 += X[ii][jj];
    mod.add(constraint1 == 1);
    constraint1.end();
}

for(jj=0;jj<NUMCITIES;jj++)
{
    IloExpr constraint2(env);
        for(ii=0;ii<NUMCITIES;ii++)
            constraint2 += X[ii][jj];
    mod.add(constraint2 == 1);
    constraint2.end();
}

IloExpr objExpression(env);
for(ii=0;ii<NUMCITIES;ii++)
    for(jj=0;jj<NUMCITIES;jj++)
        objExpression += CityDistance[ii][jj]*X[ii][jj];
IloObjective theObjective(env,objExpression,IloObjective::Minimize);
mod.add(theObjective);
objExpression.end();


IloCplex cplex(mod);
IloBool result = cplex.solve();

cplex.out() << "solution status = " << cplex.getStatus() << endl;
cplex.out() << "detailed solution status = " << cplex.getCplexStatus() << endl;

if(result == IloTrue)
{
    cplex.out() << "Objective Function: " << cplex.getObjValue() << endl;

    std::cout << endl;
    std::cout << "Solution obtained by CPLEX: " << endl << endl;
    for(ii=0;ii<NUMCITIES;ii++) 
    {
        for(jj=0;jj<NUMCITIES;jj++) 
            cplex.out() << cplex.getValue(X[ii][jj]) << '\t';
        cplex.out() << endl;
    }
    std::cout <<endl;
}

env.end();
cout << "////////////////////End of the math programming example////////////////////" << endl << endl;
cout << endl << endl;

return 0;
}

0 个答案:

没有答案