本文整理汇总了C++中IloEnv::error方法的典型用法代码示例。如果您正苦于以下问题:C++ IloEnv::error方法的具体用法?C++ IloEnv::error怎么用?C++ IloEnv::error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IloEnv
的用法示例。
在下文中一共展示了IloEnv::error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ComputeCPLEXRevenue
void Instance::ComputeCPLEXRevenue() {
IloEnv env;
IloInt i, j;
IloModel model(env);
IloInt nbImpressions = num_impressions_;
IloInt nbAdvertisers = num_advertisers_;
NumVarMatrix x(env, nbImpressions);
for(i = 0; i < nbImpressions; i++) {
x[i] = IloNumVarArray(env, nbAdvertisers, 0.0, 1.0, ILOFLOAT);
}
// Add impression constraints.
for(i = 0; i < nbImpressions; i++) {
model.add(IloSum(x[i]) <= 1.0);
}
// Add weighted contraint.
for(j = 0; j < nbAdvertisers; j++) {
IloExpr curr_adv_constraint(env);
for (__gnu_cxx::hash_map<int, long double>::iterator iter = bids_matrix_[j].begin();
iter != bids_matrix_[j].end();
++iter) {
curr_adv_constraint += ((double) iter->second) * ((double) weights_[j]) * x[iter->first][j];
}
model.add(curr_adv_constraint <= ((double) budgets_[j]));
}
IloExpr obj_exp(env);
for(i = 0; i < nbImpressions; i++) {
for(j = 0; j < nbAdvertisers; j++) {
obj_exp += ((double) transpose_bids_matrix_[i][j]) * x[i][j];
}
}
model.add(IloMaximize(env, obj_exp));
obj_exp.end();
IloCplex cplex(env);
cplex.setOut(env.getNullStream());
cplex.extract(model);
// Optimize the problem and obtain solution.
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
cplex.exportModel("/Users/ciocan/Documents/Google/data/example.lp");
cout << "CPLEX opt is " << cplex.getObjValue() << "\n";
env.end();
}
示例2: model
int
main (void)
{
IloEnv env;
int retval = -1;
try {
// Create the model.
IloModel model(env);
IloCplex cplex(env);
IloObjective obj(env);
IloNumVarArray vars(env);
IloRangeArray rngs(env);
IloIntArray cone(env);
createmodel(model, obj, vars, rngs, cone);
// Extract model.
cplex.extract(model);
// Solve the problem. If we cannot find an _optimal_ solution then
// there is no point in checking the KKT conditions and we throw an
// exception.
cplex.setParam(IloCplex::Param::Barrier::QCPConvergeTol, CONVTOL);
if ( !cplex.solve() || cplex.getStatus() != IloAlgorithm::Optimal )
throw string("Failed to solve problem to optimality");
// Test the KKT conditions on the solution.
if ( !checkkkt (cplex, obj, vars, rngs, cone, TESTTOL) ) {
env.error() << "Testing of KKT conditions failed." << endl;
}
else {
env.out() << "Solution status: " << cplex.getStatus() << endl;
env.out() << "KKT conditions are satisfied." << endl;
retval = 0;
}
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;
}
示例3: model
int
main (int argc, char **argv)
{
IloEnv env;
try {
IloModel model(env);
IloNumVarArray var(env);
IloRangeArray con(env);
populatebyrow (model, var, con);
IloCplex cplex(model);
// Optimize the problem and obtain solution.
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
IloNumArray vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
cplex.getValues(vals, var);
env.out() << "Values = " << vals << endl;
cplex.getSlacks(vals, con);
env.out() << "Slacks = " << vals << endl;
cplex.getDuals(vals, con);
env.out() << "Duals = " << vals << endl;
cplex.getReducedCosts(vals, var);
env.out() << "Reduced Costs = " << vals << endl;
cplex.exportModel("qpex1.lp");
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
} // END main
示例4: vals
static void
solveanddisplay (IloEnv env, IloCplex cplex, IloNumVarArray var,
IloRangeArray con)
{
// Optimize the problem and obtain solution.
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
IloNumArray vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
cplex.getValues(vals, var);
env.out() << "Values = " << vals << endl;
cplex.getSlacks(vals, con);
env.out() << "Slacks = " << vals << endl;
cplex.getDuals(vals, con);
env.out() << "Duals = " << vals << endl;
cplex.getReducedCosts(vals, var);
env.out() << "Reduced Costs = " << vals << endl;
} // END solveanddisplay
示例5: setModel
//.........这里部分代码省略.........
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;
}
示例6: columnGeneration
//.........这里部分代码省略.........
// vector has been filled by REL_CHANGE_SPAN "proper" values...
recentObjValues[0] = 0.0;
for (i = 1; i < REL_CHANGE_SPAN; i++) {
recentObjValues[i] = 0.0;
}
// model before generation start:
//rmpSolver.exportModel("rmpModel.lp");
//write results to file
/* ofstream outFile(output);
outFile << "Writing out results from initial column generation, T=" << TIME_SPAN << endl
<< "Form: [(itNum) (dual solution value) (upper bound = tempRmpObj)]" << endl << endl;*/
//cout << endl << "WE GOT OURSELVES A SEGMENTATION FAULT!" << endl;
// start timer
start = clock();
// also use an alternative time counter..
time_t start2, end2;
start2 = time(NULL);
// loop until break...
for (;;) {
// increase iteration counter
itNum++;
// solve master
if ( !rmpSolver.solve() ) {
env.error() << "Failed to optimize RMP." << endl;
throw(-1);
}
// get the rmp objective value
tempRmpObj = rmpSolver.getValue(rmpObj);
// add to objective value array (size: iterations)
// objValues.add(tempRmpObj);
// update recentObjValues:
recentObjValues.remove(0);
// remove oldest:
recentObjValues.add(tempRmpObj);
// save newest
// report something (... report1() )
// UPDATE: colgen is to quick for anything to see anything reported... so skip this
// update weight: weight = min{ 2,
if (itNum>1) {
weight = (IloNum(itNum - 1) + IloNum(nrOfImprov))/2;
}
if ( weight > weightConst) {
weight = weightConst;
}
// update duals
// -> updates obj. functions for subproblems (w.r.t. duals)
for (m = 0; m < NR_OF_MODULES; m++) {
//cout << "weight = " << weight << endl;
示例7: setModel
//.........这里部分代码省略.........
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;
}
示例8: model
int
main (int argc, char **argv)
{
IloEnv env;
try {
IloModel model(env);
IloCplex cplex(env);
if (( argc != 3 ) ||
( strchr ("opdbn", argv[2][0]) == NULL ) ) {
usage (argv[0]);
throw(-1);
}
switch (argv[2][0]) {
case 'o':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::AutoAlg);
break;
case 'p':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Primal);
break;
case 'd':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Dual);
break;
case 'b':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Barrier);
break;
case 'n':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Network);
break;
default:
break;
}
IloObjective obj;
IloNumVarArray var(env);
IloRangeArray rng(env);
cplex.importModel(model, argv[1], obj, var, rng);
cplex.extract(model);
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
IloNumArray vals(env);
cplex.getValues(vals, var);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
env.out() << "Solution vector = " << vals << endl;
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
} // END main
示例9: setModel
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";
//.........这里部分代码省略.........
示例10: model
int
main (int argc, char **argv)
{
IloEnv env;
try {
IloModel model(env);
IloCplex cplex(env);
if (( argc != 3 ) ||
( strchr ("podhbnsc", argv[2][0]) == NULL ) ) {
usage (argv[0]);
throw(-1);
}
switch (argv[2][0]) {
case 'o':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::AutoAlg);
break;
case 'p':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Primal);
break;
case 'd':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Dual);
break;
case 'b':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Barrier);
cplex.setParam(IloCplex::Param::Barrier::Crossover, IloCplex::NoAlg);
break;
case 'h':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Barrier);
break;
case 'n':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Network);
break;
case 's':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Sifting);
break;
case 'c':
cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Concurrent);
break;
default:
break;
}
IloObjective obj;
IloNumVarArray var(env);
IloRangeArray rng(env);
cplex.importModel(model, argv[1], obj, var, rng);
cplex.extract(model);
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
for (IloInt i = 0; i < var.getSize(); i++) {
if ( var[i].getName() ) env.out() << var[i].getName();
else env.out() << "Fake" << i;
env.out() << ": " << cplex.getValue(var[i]);
try { // basis may not exist
env.out() << '\t' << cplex.getBasisStatus(var[i]);
}
catch (...) {
}
env.out() << endl;
}
env.out() << "Maximum bound violation = "
<< cplex.getQuality(IloCplex::MaxPrimalInfeas) << endl;
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
} // END main
示例11: calub
//.........这里部分代码省略.........
exp80c.end();
}//end n
// generation greater than demand
for(IloInt t=1; t<=T; t++) {
IloExpr expsr1(lazenv);
for(IloInt j=0; j<J; j++){
expsr1+=gc[j][t];
}
// maspc.add(expsr1 >= Dt[t]);
expsr1.end();
}
IloExpr submasobjc(lazenv);
for(IloInt j=0; j<J; j++)
for(IloInt t=1; t<=T; t++){
submasobjc+=cj[j]*gc[j][t];
}
for(IloInt t=1; t<=T; t++)
for(IloInt n= 0; n < N; n++){
submasobjc+=ldpen*dc[itrc][n][t];
submasobjc-=sellprice*sc[itrc][n][t];
}
maspc.add(eta>=submasobjc);
submasobjc.end();
IloCplex ccg_mas(lazenv);
ccg_mas.extract(maspc);
// ccg_mas.exportModel("C:/Dropbox/Robust_UC_ISONE/data/118bus/iit/results/uc_ccg_mas.lp") ;
ccg_mas.setParam(IloCplex::MIPSearch,solver_opt); //1, traditional branch & cut
if(presol==0){
ccg_mas.setParam(IloCplex::PreInd,false);
}
ccg_mas.setParam(IloCplex::EpGap, sol_gap);
if (!ccg_mas.solve()) {
lazenv.error() << "Failed to solve ccg mas model" << endl;
throw(-1);
}
cout<<"ccg mas obj: "<<ccg_mas.getObjValue()<<endl;
cout<<"obj: "<<costuvw+ccg_mas.getObjValue()<<endl;
// cout<<" diff: "<<obj_val-costuvw - ccg_mas.getObjValue()<<endl;
// update
for(IloInt t = 0; t <= T; t++)
for(IloInt j= 0; j < J; j++){
gcstar[j][t]=ccg_mas.getValue(gc[j][t]);
// ccgresults<<""<<gcstar[j][t]<<endl;
}
lb_sub = ccg_mas.getObjValue(); // best feasible for LB
ingap = abs((ub_sub -lb_sub)/ub_sub) ;
end=clock();
runningtime=double(end-begin)/CLOCKS_PER_SEC;
ccgresults<<"iteration: "<<itrc<<" ccg_lb: "<<lb_sub<<" ccg_ub: "<<ub_sub<<" gap: "<<ingap<<" time:"<<runningtime<<endl;
ccg_mas.end();
if(ingap<in_tlrn) break;
//***************************CCG sub problem*******************************
BoolVarMatrix zc(lazenv, N); //pi5lt
for(IloInt n=0;n<N;n++){zc[n]=IloBoolVarArray(lazenv,T+1);}
NumVarMatrix pi5c(lazenv, L); //pi5lt
for(IloInt l=0;l<L;l++){pi5c[l]=IloNumVarArray(lazenv,T+1,-IloInfinity,IloInfinity);}
NumVarMatrix pi6c(lazenv, N); //p6nt
for(IloInt n=0;n<N;n++){pi6c[n]=IloNumVarArray(lazenv,T+1,0,IloInfinity);}
NumVarMatrix pi6ac(lazenv, N); //p6nt
for(IloInt n=0;n<N;n++){pi6ac[n]=IloNumVarArray(lazenv,T+1,0,IloInfinity);}
NumVarMatrix pi7c(lazenv, L); //pi7lt
for(IloInt l=0;l<L;l++){pi7c[l]=IloNumVarArray(lazenv,T+1,0,IloInfinity);}
NumVarMatrix pi8c(lazenv, L); //pi8lt
for(IloInt l=0;l<L;l++){pi8c[l]=IloNumVarArray(lazenv,T+1,0,IloInfinity);}
NumVarMatrix pi9c(lazenv, N); //p9nt
示例12: setModel
//.........这里部分代码省略.........
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;
}
示例13: main
//.........这里部分代码省略.........
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
示例14: model
ILOSTLBEGIN
#include <unistd.h>
#include <math.h>
int
main (int argc, char **argv)
{
int N = 5000;
if (argc > 1){
N = atoi(argv[1]);
}
double M = 100000;
double** w = new double*[N];
for (int i = 0; i < N; i++){
w[i] = new double[N];
for (int j = 0; j < N; j++){
if (i == j){
w[i][j] = i;
}
else {
w[i][j] = i-abs(i-j);
}
}
}
IloEnv env;
try {
IloModel model(env);
IloNumVarArray xa(env);
IloNumVarArray xi(env);
for (int i = 0; i < N; i++){
xa.add(IloNumVar(env));
xi.add(IloNumVar(env));
}
IloNumExpr obj = xa[0] + xi[0];
for (int i = 1; i < N; i++){
obj = obj + xa[i];
obj = obj + xi[i];
}
IloRangeArray con(env);
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
con.add(xa[i] + xi[j] >= w[i][j]);
}
}
model.add(con);
model.add(IloMinimize(env, obj));
// populate model
IloCplex cplex(model);
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
env.out() << "Solution value = " << cplex.getObjValue() << endl;
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
}
示例15: subduality
//.........这里部分代码省略.........
for(IloInt t = 1; t <= T; t++){
IloExpr subbug(env);
for(IloInt n = 0; n < N; n++){
subp.add(znt[n][t]>=0);
subbug += znt[n][t];}
subp.add(subbug<=uncdbug); // budget on demand uncertaint
subbug.end();
}
Md=500;
for(IloInt n = 0; n < N; n++) //dnt
for(IloInt t = 1; t <= T; t++){
subp.add(beta1[n][t] <= pi6[n][t]);
subp.add(beta2[n][t] <= pi11[n][t]);
subp.add(beta3[n][t] <= pi14[t]);
subp.add(beta1[n][t] <= Md*znt[n][t]);
subp.add(beta2[n][t] <= Md*znt[n][t]);
subp.add(beta3[n][t] <= Md*znt[n][t]);
subp.add(beta1[n][t] >= Md*znt[n][t]+pi6[n][t]-Md);
subp.add(beta2[n][t] >= Md*znt[n][t]+pi11[n][t]-Md);
subp.add(beta3[n][t] >= Md*znt[n][t]+pi14[t]-Md);
}
//--------- dual constraints
for(IloInt j = 0; j < J; j++) // gjt
for(IloInt t = 1; t <= T-1; t++){
subp.add(pi1[j][t]-pi1[j][t+1]-pi2[j][t]+pi2[j][t+1]+pi3[j][t]-pi4[j][t]+pi6[Gloc[j]][t]/* + pi12[t]*/+ pi14[t]<= cj[j]);
}
for(IloInt j = 0; j < J; j++){
subp.add(pi1[j][T]-pi2[j][T]+pi3[j][T]-pi4[j][T]+pi6[Gloc[j]][T]/*+ pi12[T]*/+ pi14[T]<= cj[j]);
}
IloInt lm;
IloInt lr;
for(IloInt l = 0; l < L; l++) // plt
for(IloInt t = 1; t <= T; t++){
lm=line[l][0];
lr=line[l][1];
subp.add((x[l]/100)*pi5[l][t]-pi6[lm][t]+pi6[lr][t]+pi7[l][t]-pi8[l][t] == 0);
}
for(IloInt n = 0; n < N; n++) // delta nt
for(IloInt t = 1; t <= T; t++){
IloExpr iexpr1(env);
IloExpr iexpr2(env);
for(IloInt l = 0; l < L; l++)
{
if(line[l][1]==n) // D(l)
{
iexpr1+= pi5[l][t];
}//end if
if(line[l][0]==n) // O(l)
{
iexpr2+= -pi5[l][t];
} //end if
}//end for l
subp.add(iexpr2+iexpr1+pi9[n][t]-pi10[n][t]==0);
iexpr1.end();
iexpr2.end();
}
for(IloInt n = 0; n < N; n++) //dnt
for(IloInt t = 1; t <= T; t++){
subp.add(pi6[n][t]-pi11[n][t]<=ldpen);
}
IloCplex cplex_sub(subp);
if(presol==0){
cplex_sub.setParam(IloCplex::PreInd,false);
}
// cplex_sub.exportModel("C:/Dropbox/Robust_UC_ISONE/data/118bus/iit/results/uc_sub.lp") ;
cplex_sub.setParam(IloCplex::EpGap, 0.005);
cplex_sub.setParam(IloCplex::MIPSearch,0); //1, traditional branch & cut
if (!cplex_sub.solve()) {
env.error() << "Failed to solve model" << endl;
throw(-1);
}
subobjval = cplex_sub.getObjValue();
cout<<"dual obj: "<<cplex_sub.getObjValue()<<endl;
cout<<"obj: "<<costuvw+cplex_sub.getObjValue()<<endl;
// cout<<" diff: "<<obj_val-costuvw- cplex_sub.getObjValue()<<endl;
if(costuvw+subobjval <= outUB){
outUB = IloMin(costuvw+subobjval,outUB);
outgap = (outUB-outLB)/outUB;
ccgresults<<"---outUB: "<<outUB<<" outGap: "<<outgap <<endl;
if(uncdbug>=1){
for(IloInt t=1; t<=T; t++)
for(IloInt n= 0;n<N;n++){
Znode[n][t]=cplex_sub.getValue(z[n][t]); // current best feasible solution
}}
}
cplex_sub.end();
subp.end();
}// end try
catch (IloException& ex){ cerr << "Error: " << ex << endl; }
catch (...) { cerr << "Error" << endl; }
env.end();
//update bounds
UB = subobjval;
LB = subobjval;
return 0;
}//end ubcal