Está en la página 1de 13

Assignment 3

Large Scale Optimisaton

Kavitha C Didugu
Question 1:

/************************************************************************************
* This is the cplex code to solve iteration by iteration the example presnted by Kelley
(1960): The cutting-plane method for solving convex programs.
* Minimize x1 - x2
* s.t.:
* -2 <= x1 <= 2
* -2 <= x2 <= 2
* 3x1^2 -2x1x2 + x2^2 - <= 0
***********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<fstream>
#include<iosfwd>
#include<string>
#include <deque>
#include <sstream>
#include <time.h>
//#include <iomanip.h>
#include <stdlib.h>
#include <vector>//for vectors
#include <math.h>

//#include <cplex.h>
#include <ilcplex/ilocplex.h>
//#include <iloconcert/ilomodel.h>
//#include <iloconcert/iloenv.h>
#include <ilconcert/ilosys.h>

using namespace std;

ILOSTLBEGIN

//typedef IloArray<IloNumArray> TwoDMatrix;

int main(int argc, char **argv)


{
IloEnv env;
try
{
IloNumVar t(env, 0, IloInfinity, ILOFLOAT);
IloNum tv;
IloNumVarArray X(env, 2, -2, 2, ILOFLOAT);
IloNumArray X_val(env, 2);
IloModel model(env);
IloExpr Obj(env);
Obj = X[0] - X[1];
//model.add(-16*X[0] + 8*X[1] <= 25);
model.add(IloMinimize(env,Obj)); // IloMinimize is used for minimization
problems
//Alternatively, model.add(IloMinimize(env,Obj)); can be replaced by the
following three lines.
//This is useful while iterating in Decomposition Techniques where the
objective function is redefined at each iteration
//IloObjective Objective = IloMinimize(env);
//model.add(Objective);
//Objective.setExpr(Obj);

Obj.end();
// Optimize
IloCplex cplex(model);
//cplex.setOut(env.getNullStream()); // This is to supress the output of
Branch & Bound Tree on screen
//cplex.setWarning(env.getNullStream()); //This is to supress warning
messages on screen
cplex.solve();//solving the MODEL
if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem is
infeasible
{
env.out() << "Problem Infeasible" << endl;
}
X_val[0] = cplex.getValue(X[0]);
X_val[1] = cplex.getValue(X[1]);
// Print results

ofstream fout;
fout.open("A3Q1 output.txt");

cout<< "Objective Value = " << cplex.getObjValue() << endl;


cout<<"X1 = "<<X_val[0]<<endl;
cout<<"X2 = "<<X_val[1]<<endl;

fout<<"Iteration:"<<t;
fout<< "Objective Value = " << cplex.getObjValue() << endl;
fout<<"X1 = "<<X_val[0]<<endl;
fout<<"X2 = "<<X_val[1]<<endl;

while (IloPower(X_val[0],2)+IloPower(X_val[1],2) -3 > 0.000001){


IloExpr cut_lhs(env);
cut_lhs = (2*X_val[0]*X[0] + 2*X_val[1]*X[1] -IloPower(X_val[0],2)-
IloPower(X_val[1],2) -3);
//cout <<"New constraint is: "<<cut_lhs<<" <= 0"<<endl;
model.add(cut_lhs<= 0);
cut_lhs.end();
cplex.solve();//solving the MODEL
if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem is
infeasible
{
env.out() << "Problem Infeasible" << endl;
}
X_val[0] = cplex.getValue(X[0]);
X_val[1] = cplex.getValue(X[1]);
fout<<"Iteration:"<<t;
fout<< "Objective Value = " << cplex.getObjValue() << endl;
fout<<"X1 = "<<X_val[0]<<endl;
fout<<"X2 = "<<X_val[1]<<endl;
}
// Print results
cout<<"Iteration:"<<t;
cout<< "Objective Value = " << cplex.getObjValue() << endl;
cout<<"X1 = "<<X_val[0]<<endl;
cout<<"X2 = "<<X_val[1]<<endl;

//IloExpr cut_lhs1(env);
//cut_lhs1 = (2*X_val[0]*X[0] + 2*X_val[1])*X[1] -IloPower(X_val[0],2)-
IloPower(X_val[1],2) -3);
//cout <<"New constraint is: "<<cut_lhs1<<" <= 0"<<endl;
fout << flush;
fout.close();
}

catch(IloException &e)
{
env.out() << "ERROR: " << e << endl;
}
catch(...)
{
env.out() << "Unknown exception" << endl;
}
env.end();
return 0;
}

OUTPUT

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -4

X1 = -2

X2 = 2

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.75

X1 = -0.75

X2 = 2

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.75

X1 = -1.375

X2 = 1.375

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.46591

X1 = -0.920455

X2 = 1.54545
Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.46591

X1 = -1.10899

X2 = 1.35692

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.46591

X1 = -1.25235

X2 = 1.21356

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.45149

X1 = -1.17346

X2 = 1.27803

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.45035

X1 = -1.20915

X2 = 1.2412

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.44977

X1 = -1.22749

X2 = 1.22228

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.44952

X1 = -1.21805

X2 = 1.23148

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.4495

X1 = -1.2227

X2 = 1.22681

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.44949

X1 = -1.22502

X2 = 1.22447

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.44949

X1 = -1.22386

X2 = 1.22563

Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.44949


X1 = -1.22444

X2 = 1.22505

Question 2:
(a) Cutting Plane Algorithm:

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<fstream>
#include<iosfwd>
#include<string>
#include <deque>
#include <sstream>
#include <time.h>
//#include <iomanip.h>
#include <stdlib.h>
#include <vector>//for vectors
#include <math.h>

//#include <cplex.h>
#include <ilcplex/ilocplex.h>
//#include <iloconcert/ilomodel.h>
//#include <iloconcert/iloenv.h>
#include <ilconcert/ilosys.h>

using namespace std;

ILOSTLBEGIN

//typedef IloArray<IloNumArray> TwoDMatrix;

int main(int argc, char **argv)


{
IloEnv env;
int N=500; int C=100; float SR=0 ; float P=0; int i=0;
IloNumVar M(env, 0, IloInfinity, ILOFLOAT);
IloNum MV; float LB=0; float UB= 99999; float check = 0;
IloNumVar R(env, 0, IloInfinity, ILOFLOAT);
IloNum RV;
//IloNumVarArray AR(env, N, 0, IloInfinity, ILOFLOAT);
IloNumArray ARV(env,N);
//IloNumVarArray P(env, N, 0, IloInfinity, ILOFLOAT);
IloNumArray PV(env,N);
//IloNumVar SR(env, 0, IloInfinity, ILOFLOAT);
IloNumVarArray x(env, N, 0, 1, ILOINT);
IloIntArray xv(env, N);
try
{
const char* data_filename = "Data_NLKnapsack_500node.dat";
if (argc > 1)
{
data_filename = argv[1];
}
fstream datafile;
datafile.open(data_filename,ios::in);

if (!datafile)
{
cerr << "ERROR: could not open file " << data_filename << " for
reading" << endl;
cerr << "usage: " << argv[0] << " <datafile>" << endl;
throw(-1);
}

datafile >> ARV >> PV >> SR;


//N = ARV.getSize();
datafile.close();
cout<<SR;
/*=======================================*/
IloExpr L(env);
//L=0;
for(int i=0;i<N;i++)
{
L=L+ARV[i]*x[i];
}
//cout<<L;
IloModel model(env);
IloExpr Obj(env);

for(int i=0; i<N; i++)


{
Obj=Obj+ x[i]*PV[i];

}
Obj= Obj-C*R;

model.add(IloMaximize(env,Obj)); // IloMinimize is used for minimization


problems
Obj.end();
model.add(M-L==0);
model.add(L<=SR);
//model.add(R>=0.01);
// Optimize
IloCplex cplex(model);
cplex.solve();
//cplex.setWarning(env.getNullStream()); //This is to supress warning
messages on screen
float j=0;

ofstream fout;
fout.open("A3Q2-cut.txt");

//int num = 150;

while(j==0)
{

RV=cplex.getValue(R);
MV=cplex.getValue(M);
IloExpr cut_lhs(env);
float MV1; MV1 = RV*SR/(1+RV);
cut_lhs = ((M-MV1)-(R-RV)*SR/((1+RV)*(1+RV)));
model.add(cut_lhs <= 0);
cut_lhs.end();
cplex.solve();//solving the MODEL

if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem


is infeasible
{
env.out() << "Problem Infeasible" << endl;
}
//cout<< "Objective Value = " << cplex.getObjValue() << endl;
RV=cplex.getValue(R);
MV=cplex.getValue(M);

for(int i=0;i<N; i++)


{
xv[i]=cplex.getValue(x[i]);
}

check = cplex.getObjValue() + 100*RV - 100*(MV/(SR-MV));


UB = cplex.getObjValue();
if(check>LB){LB=check;}
fout<<"LB is "<<LB<<endl<<"check is "<<check<<endl<<"UB is
"<<UB<<endl<<endl<<endl;

if(MV-SR*RV/(1+RV) < 0.001)


{
if(MV-SR*RV/(1+RV) > (-1)*0.001)
{
j=1;
}
}
//j=j+0.01;

for(int i=0; i<500; i++)


{
if(xv[i]==1)
{
fout<<"xv "<< i+1 << " "<<xv[i]<<endl;
}
}
fout << flush;
fout.close();
cout<< "Objective Value = " << cplex.getObjValue() << endl;
}
catch(IloException &e)
{
env.out() << "ERROR: " << e << endl;
}
catch(...)
{
env.out() << "Unknown exception" << endl;
}
env.end();
return 0;
}

OUTPUT:
1)LB is 12955.9
check is 12955.9
UB is 22621

2) LB is 12955.9
check is 12955.9
UB is 22522

3)LB is 12955.9
check is 12955.9
UB is 22327.1

4)LB is 12955.9
check is 12955.9
UB is 21949.1

5)LB is 12955.9
check is 12955.9
UB is 21238.2

6)LB is 20826
check is 20826
UB is 20833.4

7)LB is 20826
check is 20826
UB is 20826.1

8)LB is 20826
check is 20826
UB is 20826

Nodes Served (1 denotes their selection)


Node 7 1
Node 52 1
Node 59 1
Node 86 1
Node 93 1
Node 119 1
Node 134 1
Node 136 1
Node 152 1
Node 197 1
Node 199 1
Node 203 1
Node 226 1
Node 288 1
Node 362 1
Node 368 1
Node 382 1
Node 388 1
Node 426 1
Node 471 1
Node 478 1

(b) Finite constraint linearization:

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<fstream>
#include<iosfwd>
#include<string>
#include <deque>
#include <sstream>
#include <time.h>
//#include <iomanip.h>
#include <stdlib.h>
#include <vector>//for vectors
#include <math.h>

//#include <cplex.h>
#include <ilcplex/ilocplex.h>
//#include <iloconcert/ilomodel.h>
//#include <iloconcert/iloenv.h>
#include <ilconcert/ilosys.h>

using namespace std;

ILOSTLBEGIN

//typedef IloArray<IloNumArray> TwoDMatrix;

int main(int argc, char **argv)


{
IloEnv env;
int N=500; int C=100; float SR=0 ; float P=0; int i=0;
//IloNumVar M(env, 0, IloInfinity, ILOFLOAT);
float MV; float LB=0; float UB= 99999; float check = 0;
IloNumVar R(env, 0, IloInfinity, ILOFLOAT);
IloNum RV;
int D= 9999;
//IloNumVarArray AR(env, N, 0, IloInfinity, ILOFLOAT);
IloNumArray ARV(env,N);
//IloNumVarArray P(env, N, 0, IloInfinity, ILOFLOAT);
IloNumArray PV(env,N);
//IloNumVar SR(env, 0, IloInfinity, ILOFLOAT);
IloBoolVarArray x(env, N);
IloNumArray xv(env, N);
IloNumVarArray y(env, N, 0, IloInfinity, ILOFLOAT);
IloNumArray yv(env, N);
IloNumVar M(env, 0, IloInfinity, ILOFLOAT);
try
{
const char* data_filename = "Data_NLKnapsack_500node.dat";
if (argc > 1)
{
data_filename = argv[1];
}
fstream datafile;
datafile.open(data_filename,ios::in);

if (!datafile)
{
cerr << "ERROR: could not open file " << data_filename << " for
reading" << endl;
cerr << "usage: " << argv[0] << " <datafile>" << endl;
throw(-1);
}

datafile >> ARV >> PV >> SR;


//N = ARV.getSize();
datafile.close();
cout<<SR;
/*=======================================*/
IloExpr L(env);
//L=0;
for(int i=0;i<N;i++)
{
L=L+ARV[i]*x[i];
}
//L.end();
//cout<<L;
IloModel model(env);
IloExpr Obj(env);

for(int i=0; i<N; i++)


{
Obj=Obj+ x[i]*PV[i];

}
Obj= Obj-C*R;

model.add(IloMaximize(env,Obj)); // IloMinimize is used for minimization


problems
Obj.end();
IloExpr Nowitisright(env);
for(int i=0;i<N; i++)
{
Nowitisright = Nowitisright + ARV[i]*y[i];
} model.add(L==M);
model.add(Nowitisright<=(R*SR - M));
model.add(Nowitisright>=(R*SR - M));
for(int i=0; i<N; i++)
{
model.add(y[i]<=D*x[i]);
model.add(y[i]<=R+D*(1-x[i]));
model.add(y[i]>=R-D*(1-x[i]));

//cout<<i<<endl;
}

//model.add(R>=0.01);
// Optimize

IloCplex cplex(model);
//cplex.setWarning(env.getNullStream()); //This is to supress warning
messages on screen
float j=0;

ofstream fout;
fout.open("A3Q2-2.txt");

cplex.solve();//solving the MODEL

if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem is


infeasible
{
env.out() << "Problem Infeasible" << endl;
}
//cout<< "Objective Value = " << cplex.getObjValue() << endl;
RV=cplex.getValue(R);
MV=cplex.getValue(M);
float Q= 0;
Q = cplex.getValue(Nowitisright);
cout<<endl<<"MV"<<MV;
for(int i=0;i<N; i++)
{
xv[i]=cplex.getValue(x[i]);
yv[i]=cplex.getValue(y[i]);
}
int sum=0; float objec=0; float Si=0;
for(int i=0; i<500; i++)
{
if(xv[i]==1)
{
cout<<"xv "<< i+1 << " "<<xv[i]<<" "<<ARV[i]<<endl;
sum=sum+ARV[i];
objec=objec + PV[i];
}

}
objec = objec - (100*sum/(SR-sum));
cout<<"sum"<<" "<<sum<<endl;
cout<<"sum"<<" "<<sum<<endl;
cout<<"objec"<<" "<<objec<<endl;
cout<<"objec"<<" "<<objec<<endl;
cout<<"RV"<<RV<<endl;
cout<<"Si"<<sum/(SR-sum)<<endl;
if(Q==RV*SR - MV){cout<<endl<<"Q";}
for(int i=0;i<N; i++)
{
xv[i]=cplex.getValue(x[i]);
}

fout<< "Objective Value = " << objec <<endl;

for(int i=0; i<500; i++)


{
if(xv[i]==1)
{
fout<<"xv "<< i+1 << endl;
cout<<"xv "<< i+1 << endl ;
}
}

fout << flush;


fout.close();
cout<< "Objective Value = " << cplex.getObjValue() << endl;
}
catch(IloException &e)
{
env.out() << "ERROR: " << e << endl;
}
catch(...)
{
env.out() << "Unknown exception" << endl;
}
env.end();
return 0;
}

OUTPUT
xv 7
xv 52
xv 59
xv 86
xv 93
xv 119
xv 134
xv 136
xv 152
xv 197
xv 199
xv 203
xv 226
xv 288
xv 362
xv 368
xv 382
xv 388
xv 426
xv 471
xv 478
Optimal value= 20826

VERDICT: Finite constraint linearization takes much more time compared to the cutting plane algorithm.
The cutting plane algorithm , as we can see, has run a small number of iterations before the upper and
lower bound converged to the optimal value. But the finite constraint linearistaion method only
marginally improves the optimality gap for each iteration and hence it is more time taking.

También podría gustarte