You are on page 1of 5

Assignment 1

Large Scale Optimisation

Kavitha C Didugu

Question 1:

#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 X1(env, 0, 1, ILOFLOAT);
IloNum X1_val;
IloNumVar X2(env, 0, 1, ILOFLOAT);
IloNum X2_val;
IloNumVar X3(env, 0, 1, ILOFLOAT);
IloNum X3_val;
IloNumVar X4(env, 0, 1, ILOFLOAT);
IloNum X4_val;
IloModel model(env);
IloExpr Obj(env);
Obj = 16*X1 + 10*X2 + 4*X4;
model.add(IloMaximize(env,Obj)); // IloMinimize is used for minimization problems

Obj.end();
model.add( 8*X1 + 2*X2 + X3 + 4*X4 <= 10);
model.add(X1+X2<=1);
model.add(X3+X4<=1);
// Optimize
IloCplex cplex(model);
cplex.solve();//solving the MODEL
if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem is infeasible
{
env.out() << "Problem Infeasible" << endl;
}
X1_val = cplex.getValue(X1);
X2_val = cplex.getValue(X2);
X3_val = cplex.getValue(X3);
X4_val = cplex.getValue(X4);

// Print results
cout<< "Objective Value = " << cplex.getObjValue() << endl;
cout<<"X1 = "<<X1_val<<endl;
cout<<"X2 = "<<X2_val<<endl;
cout<<"X3 = "<<X3_val<<endl;
cout<<"X4 = "<<X4_val<<endl;

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

OUTPUT

X1=1, X2=0, X3=0, X4=0


Objective value=16

Question 2:

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<fstream>
#include<iosfwd>
#include<string>
#include <deque>
#include <sstream>
#include <time.h>
#include <stdlib.h>

#include <ilcplex/ilocplex.h>
#include <ilconcert/ilosys.h>

ILOSTLBEGIN

typedef IloArray<IloNumArray> TwoDMatrix;


//typedef IloArray<TwoDMatrix> ThreeDMatrix;
//typedef IloArray<ThreeDMatrix> FourDMatrix;

int main(int argc, char **argv)


{
IloEnv env;
int N=500,M=30; double D=9999; float alpha=0.216;

IloBoolVarArray y(env,M);
IloBoolArray yv(env,M);
IloNumArray alliance_of_carrier(env, 8);//xij

//A2D x(env);
IloNumArray lane_demand(env,N); IloNumArray MQC(env,N);
IloNumArray alliance_bound(env,N); IloNumArray carrier_bound(env,N);
//TwoDMatrix xv(env);
TwoDMatrix price_data(env,N); //flow data between pairs of nodes*/
int aoc[30][8];

try
{
///////// DATA FILE READING //////////
const char* data_filename1 = "500_30_data_1_price.dat";
const char* data_filename2 = "500_30_data_1_demand.dat";
fstream fin1, fin2;
fin1.open(data_filename1,ios::in);
fin2.open(data_filename2,ios::in);
fin2 >> lane_demand >> MQC >> carrier_bound >> alliance_bound;
fin2 >> alliance_of_carrier;
fin1 >> price_data;

fin1.close();
fin2.close();

N = lane_demand.getSize();
cout<<"Number of lanes = "<<N<<endl;
M = MQC.getSize();
cout<< "Number of carriers="<<M<<endl;
ofstream fout;
fout.open("A3Q2 Volumes.txt");
// ----------data read------------------------------------------------

IloModel model(env);

typedef IloArray<IloNumVarArray> arr2d;


arr2d x(env, N);
TwoDMatrix x_val(env, N);

//=======================================================================
for (int i=0;i<N;i++)
{
x[i]=IloNumVarArray(env, N, 0, IloInfinity,ILOFLOAT);
x_val[i]=IloNumArray(env, N);

IloExpr Obj(env); // Creates an expression with the name Obj (Objective)

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


{
cout<<i<<endl;
for (int j=0;j<N;j++)
{
//cout<<j<<endl;
//cout<<price_data[0][0];
Obj+=(price_data[i][j])*x[i][j];
}
}

// model.add is a function to add constraints and objectives in the CPLEX environment


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

for(int j=0;j<N;j++)
{

IloExpr SumZ(env);
for(int i=0;i<M;i++)
{
SumZ+=x[i][j];
}
model.add(SumZ>=lane_demand[j]);
SumZ.end();
}

//Constraint 2: for {i in 1..N}{k in 1..N}: z[i][k] <= z[k][k]


for(int i=0;i<M;i++)
{
IloExpr SumB(env);
for(int j=0;j<N;j++)
{
SumB+=x[i][j];
}
model.add(MQC[i]*y[i] <= SumB);
SumB.end();
}
for(int j=0; j<N; j++)
{

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


{
IloExpr SumP(env);
for(int t=0; t<M; t++)
{

SumP+=price_data[t][j]*x[t][j];
}
model.add(SumP<=(1+alpha)*price_data[i][j]*lane_demand[j]+(1-
y[i])*D);
SumP.end();
}

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


{
for(int k=0; k<8; k++)
{
if(alliance_of_carrier[i]==k)
{
aoc[i][k]=1;
fout<<"Carrier"<<i<<" "<<"Lane"<<k<<endl;
cout<<"Carrier"<<i<<" "<<"Lane"<<k<<endl;
}
else
{
aoc[i][k]=0;
}

for(int k=0;k<8; k++)


{
for(int j=0; j<N; j++)
{
IloExpr SumA(env);
for(int i=0; i<M; i++)
{

SumA+=x[i][j]*aoc[i][k];

}
model.add(SumA<=alliance_bound[j]*lane_demand[j]);
SumA.end();
}

//Constraint 5: for {i in 1..N}{j in 1..N}{m in 1..N}:sum {k in


1..N}(x[i][j][k][m])=z[j][m];
for(int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
model.add(x[i][j]<=carrier_bound[j]*lane_demand[j]*y[i]);
}
}
//==============================================================================

//Optimize
IloCplex cplex(model);

cplex.solve();//solving the MODEL

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


{
cout << "Problem is Infeasible" << endl;
}

// Print results

cout << "Minimum Cost = " << cplex.getObjValue() << endl;


cout<<"obj value"<<cplex.getObjValue()<<endl;
cout<<"Carrier \t Lane: "<<endl;

fout << "Minimum Cost = " << cplex.getObjValue() << endl;


fout<<"obj value"<<cplex.getObjValue()<<endl;
fout<<"Carrier \t Lane: "<<endl;

/*for(int i=0; i<M;i++)


{
yv[i]=cplex.getValue(y[i]);
cout<<yv[i]<<endl;
}
*/

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


{
if(yv[i]==1)
{
for (int k=0;k<N;k++)
{
if (cplex.getValue(x[i][k])>0)
{
cout<<i<<"\t"<<k<<endl;
fout<<i<<"\t"<<k<<endl;
x_val[i][k] = cplex.getValue(x[i][k]);
fout<<"Carrier"<<i<<" Lane"<<k<<"
"<<x_val[i][k]<<endl;
}
else
{
x_val[i][k] = cplex.getValue(x[i][k]);
}
}
}
}

env.end();

}//end of of try block


catch (IloException& ex)
{
cerr << "Error: " << ex << endl;
}
catch (...)
{
cerr << "Error" << endl;
}
return 0;
}

You might also like