Académique Documents
Professionnel Documents
Culture Documents
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>
ILOSTLBEGIN
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");
fout<<"Iteration:"<<t;
fout<< "Objective Value = " << cplex.getObjValue() << endl;
fout<<"X1 = "<<X_val[0]<<endl;
fout<<"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
X1 = -2
X2 = 2
X1 = -0.75
X2 = 2
X1 = -1.375
X2 = 1.375
X1 = -0.920455
X2 = 1.54545
Iteration:IloNumVar(0)[0 .. 1.#INF] Objective Value = -2.46591
X1 = -1.10899
X2 = 1.35692
X1 = -1.25235
X2 = 1.21356
X1 = -1.17346
X2 = 1.27803
X1 = -1.20915
X2 = 1.2412
X1 = -1.22749
X2 = 1.22228
X1 = -1.21805
X2 = 1.23148
X1 = -1.2227
X2 = 1.22681
X1 = -1.22502
X2 = 1.22447
X1 = -1.22386
X2 = 1.22563
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>
ILOSTLBEGIN
if (!datafile)
{
cerr << "ERROR: could not open file " << data_filename << " for
reading" << endl;
cerr << "usage: " << argv[0] << " <datafile>" << endl;
throw(-1);
}
}
Obj= Obj-C*R;
ofstream fout;
fout.open("A3Q2-cut.txt");
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
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
#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>
ILOSTLBEGIN
if (!datafile)
{
cerr << "ERROR: could not open file " << data_filename << " for
reading" << endl;
cerr << "usage: " << argv[0] << " <datafile>" << endl;
throw(-1);
}
}
Obj= Obj-C*R;
//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");
}
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]);
}
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.