Nov 3, 2013 at 4:32am UTC
I am using VS 2008 and cplex 125 version. My computer got broken down and i need to get my code running, I have my thesis proposal this week. And getting the above results. I would appreciate any help.
Thank you so much!
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
#include <time.h>
#define N 60
int n; /* The number of objects */
clock_t cpu_1, cpu_2;
clock_t cltime;
double dif;
int main(int argc, char **argv)
{
IloEnv envi;
try {
const char* filename ="C:/Users/Alp/Desktop/Knapsackexample/Knapsackexample/biofueldata.dat";
ifstream file(filename);
if ( !file ) {
cerr << "No such file: " << filename << endl;
throw(-1);
}
IloInt items = 3;
IloInt i;
IloInt periods = 53;
IloInt t;
IloInt z=0.22;
IloInt v=0.45;
IloInt k=0.01;
IloInt me;
IloNumArray rt(envi, periods);
IloNumArray ct(envi, periods);
IloNumArray wtit(envi, periods);
IloNumArray ei(envi, items);
IloNumArray qt(envi, periods);
IloNumArray ht(envi, periods);
IloArray<IloNumArray> ait(envi, items);
for (int i=0;i<items;i++)
ait[i]=IloNumArray (envi, periods);
IloArray<IloNumArray> pit(envi, items);
for (int i=0;i<items;i++)
pit[i]=IloNumArray (envi, periods);
IloArray<IloNumArray> fit(envi, items);
for (int i=0;i<items;i++)
fit[i]=IloNumArray (envi, periods);
IloArray<IloNumArray> wit(envi, items);
for (int i=0;i<items;i++)
wit[i]=IloNumArray (envi, periods);
IloArray<IloNumArray> pitit(envi, items);
for (int i=0;i<items;i++)
pitit[i]=IloNumArray (envi, periods);
IloArray<IloNumArray> fitit(envi, items);
for (int i=0;i<items;i++)
fitit[i]=IloNumArray (envi, periods);
IloNumArray htit(envi, periods);
IloArray<IloNumArray> lit(envi, items);
for (int i=0;i<items;i++)
lit[i]=IloNumArray (envi, periods);
IloNumArray dt(envi, periods);
IloArray<IloNumArray> bit(envi, items);
for (int i=0;i<items;i++)
bit[i]=IloNumArray (envi, periods);
//Read Section
envi.out()<<'r'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> rt[t];
envi.out() <<rt[t]<<'\t';}
envi.out()<< '\n'<<endl;
envi.out()<<'c'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> ct[t];
envi.out() <<ct[t]<<'\t';}
envi.out()<< '\n'<<endl;
envi.out()<<'w'<<'t'<<'i'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> wtit[t];
envi.out() <<wtit[t]<<'\t';}
envi.out()<< '\n'<<endl;
envi.out()<<'e'<<'i'<<endl;
for (int i=0;i<items;i++)
{
file >> ei[i];
envi.out() <<ei[i]<<'\t';}
envi.out()<< '\n'<<endl;
envi.out()<<'q'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> qt[t];
envi.out() <<qt[t]<<'\t';}
envi.out()<< '\n'<<endl;
envi.out()<<'h'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> ht[t];
envi.out() << ht[t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'a'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> ait[i][t];
envi.out() << ait[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'p'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> pit[i][t];
envi.out() << pit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'f'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> fit[i][t];
envi.out() << fit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'w'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> wit[i][t];
envi.out() << wit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'p'<<'i'<<'t'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> pitit[i][t];
envi.out() << pitit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'f'<<'i'<<'t'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> fitit[i][t];
envi.out() << fitit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'h'<<'i'<<'t'<<'i'<<'t'<<endl;
for (int t=0;t<periods;t++){
file >> htit[t];
envi.out() << htit[t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'l'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> lit[i][t];
envi.out() << lit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'d'<<'t'<<endl;
for (int t=0;t<periods;t++)
{
file >> dt[t];
envi.out() << dt[t]<<'\t';
}
envi.out()<< '\n'<<endl;
envi.out()<<'b'<<'i'<<'t'<<endl;
for (int i=0;i<items;i++)
for (int t=0;t<periods;t++){
file >> bit[i][t];
envi.out() << bit[i][t]<<'\t';
}
envi.out()<< '\n'<<endl;
file >> me;
if(me=55)
cout << "true" << endl;
else
cout << "false" << endl;
IloModel model(envi);
// VARIABLES
IloArray<IloNumVarArray> xit(envi,items);
for (int i=0; i < items; i++) {
xit[i]=IloNumVarArray(envi,periods,0,IloInfinity);
}
IloNumVarArray it(envi,periods,0,IloInfinity);
IloArray<IloNumVarArray> yit(envi,items);
for (i = 0; i < items; i++) {
yit[i]= IloNumVarArray (envi,periods,0,1,ILOINT);
}
IloNumVarArray ut(envi,periods,0,IloInfinity);
// OBJECTIVE FUNCTION
IloExpr TotalCost(envi);
IloExpr Cost0(envi);
IloExpr Emission0(envi);
IloExpr Emission1(envi);
for (t = 0; t < periods; t++){
Cost0 += ht[t]*it[t];
Emission0 += z*ut[t];
for (i = 0; i < items; i++){
//Cost1 += pit[i][t]*xit[i][t]+fit[i][t]*yit[i][t];
//Emission1 += z*(pitit[i][t]*xit[i][t]+fitit[i][t]*yit[i][t]);
//Reduction -= v*(lit[i][t]*xit[i][t]);
Emission1 += pit[i][t]*xit[i][t]+fit[i][t]*yit[i][t]-v*(xit[i][t])-k*(ait[i][t]*xit[i][t]);
}}
TotalCost= Cost0+Emission0+Emission1;
model.add(IloMinimize(envi, TotalCost));
TotalCost.end();
Cost0.end();
Emission0.end();
Emission1.end();
//EMISSION CONSTRAINTS
// Constraint 1
for (int j = 1; j < periods; j++){
IloExpr Emissionconstraintone(envi);
for (i = 0; i < items; i++){
Emissionconstraintone += (pitit[i][j]*xit[i][j]+fitit[i][j]*yit[i][j]);
}
model.add(Emissionconstraintone + htit[j]*it[j] <= ct[j]+ ut[j]);
Emissionconstraintone.end();
}
//New
model.add(it[0] == 0);
// Constraint 2
for (t = 1; t < periods; t++){
IloExpr Emissionconstrainttwo(envi);
for (i = 0; i < items; i++){
Emissionconstrainttwo += (lit[i][t]*xit[i][t]);
}
model.add(Emissionconstrainttwo >= rt[t]);
Emissionconstrainttwo.end();
}
// Constraint 3
for (t = 1; t < periods; t++){
IloExpr Emissionconstraintthree(envi);
for (i = 0; i < items; i++){
Emissionconstraintthree += (wit[i][t]*xit[i][t]);
}
model.add(Emissionconstraintthree <= wtit[t]);
Emissionconstraintthree.end();
}
//PRODUCTION CONSTRAINTS
// Constraint 4
for (t = 1; t < periods; t++){
IloExpr productonconstraintone(envi);
for (i = 0; i < items; i++){
productonconstraintone += (xit[i][t]);
}
model.add(productonconstraintone <= qt[t]);
productonconstraintone.end();
}
// Constraint 5
for (t = 0; t < periods; t++){
IloExpr productonconstraintfivea(envi);
for (i = 0; i < items; i++){
productonconstraintfivea += ei[i];
model.add(productonconstraintfivea * bit[i][t] >= xit[i][t]);
}
productonconstraintfivea.end();
}
// Constraint 6
for (t = 1; t < periods; t++){
IloExpr productonconstrainttwo(envi);
for (i = 0; i < items; i++){
productonconstrainttwo += (xit[i][t]);
}
model.add(productonconstrainttwo+it[t-1] == dt[t]+it[t]);
productonconstrainttwo.end();
}
//// Constraint 7
for (t = 1; t < periods; t++){
IloExpr demand(envi);
for (int j = t; j < periods; j++)
{
demand += dt[j];
}
for (i = 0; i < items; i++){
//if ( qt[t] <= demand)
// xit[i][t] <= qt[t]*yit[i][t];
//else
// xit[i][t] <= demand*yit[i][t];
model.add(xit[i][t] <= qt[t]*yit[i][t]);
model.add(xit[i][t] <= demand*yit[i][t]);
}
demand.end();
//for (p = 0; p < nProd; p++) {
// envi.out() << '\t' << p
// << '\t' << cplex.getValue(x[p]) << endl;
'
Nov 3, 2013 at 9:43am UTC
Okay. What's wrong with your code? Some of us may be missing a header that your code relies on, so we can't even trivially tell if it compiles or not.
...and why did you delete the code tags? ;_;
-Albatross