La ROADEF
La R.O.A.D
Evénements
Prix
Publications
Plus
Forums
Connexion
Livre blanc

Probl

Forum 'Discussions' - Sujet créé le 2008-04-19

Bonjour,

J'ai commencé à travailer sur Cplex il y a quelques jours, comment je ne suis pas familiarisée avec ce logiciel j'ai copié un exemple et l'ai fait de modifications. Mais ça ne marche pas et je suis pas capable de trouver mes erreurs. Mon modéle c'est:
min Sum (Covariance[i][j]*X[i]*X[j])
st Sum (R[i]*X[i]) =Rexp //Return Constraint
Sum (X[i])=1 //Budget Constraint
xmin[i]<= X[i] <= xmax[i] (1 i n) //Floor &Ceiling C.
max(X[i]-iniX[i],0) < Bmax (1 i n) //TurnoverPurchase C
max(iniX[i]-X[i],0) < Smax(1 i n) //TurnoverSale C.
X[i]=iniX[i] or X[i] >= (iniX[i]+Bmin) //Trading Constraint
or X[i] >= (iniX[i]-S[i])
|{i E {1,...,n}:X[i]!=0}| N //Maximun number of
elements in solution
------------------------------------------------------------
et mon code c'est:


#include <ilcplex/ilocplex.h>
#include <ilconcert/iloexpr.h>
#include <ilconcert/ilomodel.h>

ILOSTLBEGIN

typedef IloArray<IloNumArray> IloNumArray2;
double Rexp = .98;
IloInt nMaxAsset = 16;


void buildModelByRow(IloModel model,
IloNumVarArray X,
IloNumArray floors,
IloNumArray ceilings,
IloNumArray maxPurchases,
IloNumArray minPurchases,
IloNumArray maxSales,
IloNumArray minSales,
IloNumArray initialX,
IloNumArray returns,
IloNumArray2 Cov,
IloNumVar::Type type) {

IloEnv env = model.getEnv();
X.clear();
IloNumVarArray tmp(env, floors,ceilings);
tmp.end();

IloInt i, j;
IloInt nAsset = floors.getSize();

IloExpr obj;
for(i=0; i<nAsset; i++)
for(j=0; j<nAsset; j++){
obj+= Cov[i][j]*X[i]*X[j];
}
model.add(IloMinimize(env, obj ));
obj.end();


//Return Constraint
IloExpr sumRexp;
for(i=0; i<nAsset; i++)
sumRexp += returns[i]*X[i];
model.add(sumRexp >= Rexp);
sumRexp.end();


//Budget Constraint
IloExpr sumBudget;
for(i=0; i<nAsset; i++)
sumBudget += X[i];
model.add(sumBudget == 1);
sumBudget.end();

//Floor & Ceiling Constraint
for(i=0; i<nAsset; i++)
model.add( floors[i] <= X[i] <= ceilings[i] );

//Trading Constraint
for(i=0; i<nAsset; i++)
model.add( initialX[i] == X[i] || X[i] >= (initialX[i] + minPurchases[i]) ||
X[i] >= (initialX[i] - minSales[i]) );

//Maximun Number of asset
IloExpr nIncludedAsset;
for(i=0; i<nAsset; i++)
nIncludedAsset+=1;
model.add(nIncludedAsset <= nMaxAsset);
nIncludedAsset.end();

//Turnover Purchase
IloExpr purch;
for(i=0; i<nAsset; i++){
purch = X[i]-initialX[i];
if (purch < 0) purch = 0;
model.add(purch <= maxPurchases[i]);
}
purch.end();

//Turnover Sale
IloExpr sale;
for(i=0; i<nAsset; i++){
sale = initialX[i] - X[i];
if(sale < 0) sale = 0;
model.add(sale <= maxSales[i]);
}
sale.end();
}

int main(int argc, char **argv){
IloEnv env;

try {
const char* rfloor = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/floors.txt";
const char* rceiling = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/ceilings.txt";
const char* rmaxPurchase = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/maxPurchases.txt";
const char* rmaxSale = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/maxSales.txt";
const char* rminPurchase1 ="C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/minPurchases.txt";
const char* rminSale1 = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/minSales.txt";
const char* rinitialX= "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/initialX.txt";
const char* rreturn = "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/returns.txt";
const char* rcov= "C:/Documents and Settings/maria/Escritorio/Adriana/cplex2/covMatrix.txt";

IloBool byColumn = IloFalse;
IloNumVar::Type varType = ILOFLOAT;

IloInt i;

ifstream file1(rfloor);
if ( !file1 ) {
cerr << "ERROR: could not open file '" << rfloor << "' for reading" << endl;
throw (-1);
}

ifstream file2(rceiling);
if ( !file2 ) {
cerr << "ERROR: could not open file '" << rceiling << "' for reading" << endl;
throw (-1);
}

ifstream file3(rmaxPurchase);
if ( !file3 ) {
cerr << "ERROR: could not open file '" << rmaxPurchase << "' for reading" << endl;
throw (-1);
}

ifstream file4(rmaxSale);
if ( !file4 ) {
cerr << "ERROR: could not open file '" << rmaxSale << "' for reading" << endl;
throw (-1);
}

ifstream file7(rinitialX);
if ( !file7 ) {
cerr << "ERROR: could not open file '" << rinitialX<< "' for reading" << endl;
throw (-1);
}

ifstream file8(rreturn);
if ( !file8 ) {
cerr << "ERROR: could not open file '" << rreturn << "' for reading" << endl;
throw (-1);
}

ifstream file9(rcov);
if ( !file9 ) {
cerr << "ERROR: could not open file '" << rcov<< "' for reading" << endl;
throw (-1);
}


// model data double Rexp = .98;
IloNumArray floors(env), ceilings(env), maxPurchases(env),maxSales(env), returns(env), initialX(env);
IloNumArray minPurchases(env, 189, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05);
IloNumArray minSales (env, 189, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05);
IloNumArray2 Cov(env);

//file >> foodCost >> foodMin >> foodMax;
//file >> nutrMin >> nutrMax;
//file >> nutrPer;
file1 >> floors;
file2 >> ceilings;
file3 >> maxPurchases;
file4 >> maxSales;
file7 >> initialX;
file8 >> returns;
file9 >> Cov;

cerr << "floors" << floors.getSize()<<endl;
cerr << "ceilings" << ceilings.getSize()<<endl;
cerr << "maxPurchases" << maxPurchases.getSize()<<endl;
cerr << "maxSales" << maxSales.getSize()<<endl;
cerr << "minPurchases" << minPurchases.getSize()<<endl;
cerr << "minSales" << minSales.getSize()<<endl;
cerr << "initialX" << initialX.getSize()<<endl;
cerr << "returns" << returns.getSize()<<endl;
cerr << "cov" << Cov.getSize()<<endl;

IloInt nAsset = floors.getSize();
if(ceilings.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y floors contiene=" << floors.getSize()<<endl;
throw (-1);
}
if(maxPurchases.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y maxPurchases contiene=" << maxPurchases.getSize()<<endl;
throw (-1);
}
if(maxSales.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y maxSales contiene=" << maxSales.getSize()<<endl;
throw (-1);
}

if(minPurchases.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y minPurchases contiene=" << minPurchases.getSize()<<endl;
throw (-1);
}
if(minSales.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y minSales contiene=" << minSales.getSize()<<endl;
throw (-1);
}
if(initialX.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset="