Originally posted by: SystemAdmin
[adriana said:]
I began to work with c++ Concert Technology in cplex a few days ago, I copied the example ilodiet.cpp and tried to adapt it to my problem, but I can't do it and I don't find the error this is my model :
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.<br /> 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
------------------------------------------------------------
and my code:
#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++)<br /> for(j=0; j<nAsset; j++){<br /> 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++) <br /> sumRexp += returns[i]*X[i];
model.add(sumRexp >= Rexp);
sumRexp.end();
//Budget Constraint
IloExpr sumBudget;
for(i=0; i<nAsset; i++) <br /> sumBudget += X[i];
model.add(sumBudget == 1);
sumBudget.end();
//Floor & Ceiling Constraint
for(i=0; i<nAsset; i++) <br /> model.add( floors[i] <= X[i] <= ceilings[i] );<br />
//Trading Constraint
for(i=0; i<nAsset; i++)<br /> 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++)<br /> nIncludedAsset+=1;
model.add(nIncludedAsset <= nMaxAsset);<br /> nIncludedAsset.end();
//Turnover Purchase
IloExpr purch;
for(i=0; i<nAsset; i++){<br /> purch = X[i]-initialX[i];
if (purch < 0) purch = 0;<br /> model.add(purch <= maxPurchases[i]);<br /> }
purch.end();
//Turnover Sale
IloExpr sale;
for(i=0; i<nAsset; i++){<br /> sale = initialX[i] - X[i];
if(sale < 0) sale = 0;<br /> model.add(sale <= maxSales[i]);<br /> }
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;<br /> throw (-1);
}
ifstream file2(rceiling);
if ( !file2 ) {
cerr << "ERROR: could not open file '" << rceiling << "' for reading" << endl;<br /> throw (-1);
}
ifstream file3(rmaxPurchase);
if ( !file3 ) {
cerr << "ERROR: could not open file '" << rmaxPurchase << "' for reading" << endl;<br /> throw (-1);
}
ifstream file4(rmaxSale);
if ( !file4 ) {
cerr << "ERROR: could not open file '" << rmaxSale << "' for reading" << endl;<br /> throw (-1);
}
ifstream file7(rinitialX);
if ( !file7 ) {
cerr << "ERROR: could not open file '" << rinitialX<< "' for reading" << endl;<br /> throw (-1);
}
ifstream file8(rreturn);
if ( !file8 ) {
cerr << "ERROR: could not open file '" << rreturn << "' for reading" << endl;<br /> throw (-1);
}
ifstream file9(rcov);
if ( !file9 ) {
cerr << "ERROR: could not open file '" << rcov<< "' for reading" << endl;<br /> 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;<br /> cerr << "ceilings" << ceilings.getSize()<<endl;<br /> cerr << "maxPurchases" << maxPurchases.getSize()<<endl;<br /> cerr << "maxSales" << maxSales.getSize()<<endl;<br /> cerr << "minPurchases" << minPurchases.getSize()<<endl;<br /> cerr << "minSales" << minSales.getSize()<<endl;<br /> cerr << "initialX" << initialX.getSize()<<endl;<br /> cerr << "returns" << returns.getSize()<<endl;<br /> cerr << "cov" << Cov.getSize()<<endl;<br />
IloInt nAsset = floors.getSize();
if(ceilings.getSize()!=nAsset){
cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y floors contiene=" << floors.getSize()<<endl;<br /> throw (-1);<br /> }<br /> if(maxPurchases.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y maxPurchases contiene=" << maxPurchases.getSize()<<endl;<br /> throw (-1);<br /> }<br /> if(maxSales.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y maxSales contiene=" << maxSales.getSize()<<endl;<br /> throw (-1);<br /> }<br /><br /> if(minPurchases.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y minPurchases contiene=" << minPurchases.getSize()<<endl;<br /> throw (-1);<br /> }<br /> if(minSales.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y minSales contiene=" << minSales.getSize()<<endl;<br /> throw (-1);<br /> }<br /> if(initialX.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y initialX contiene=" << initialX.getSize()<<endl;<br /> throw (-1);<br /> }<br /> if(returns.getSize()!=nAsset){<br /> cerr << "Tamaño inconsistente nAsset=" <<nAsset <<" y returnscontiene=" << returns.getSize()<<endl;<br /> throw (-1);<br /> }<br /><br /><br /> <br /> // Build model<br /> IloModel model(env);<br /> IloNumVarArray X(env); <br /> <br /> buildModelByRow( model, X, floors, ceilings,maxPurchases,minPurchases,maxSales,minSales,<br /> initialX, returns, Cov,varType);<br /><br /> // Solve model<br /> IloCplex cplex(model);<br /> cplex.exportModel("diet.lp");<br />
cplex.solve();
cplex.out() << "solution status = " << cplex.getStatus() << endl;<br />
cplex.out() << "cost = " << cplex.getObjValue() << endl;<br />
for (i = 0; i < X.getSize(); i++) {<br /> cplex.out() << " X" << i << " = " << cplex.getValue(X[i]) << endl;<br /> }
cplex.out() << "X = " << X << endl;<br /> }
catch (IloException& ex) {
cerr << "Error: " << ex << endl;<br /> }
catch (...) {
cerr << "Error" << endl;<br /> }
env.end();
return 0;
}
----------------------------------
These are the errors:
[font=Verdana]C:\ILOG\cplex81\examples\msvc6\stat_sta\copiaPortfolio3.cpp(79) : error C2451: conditional expression of type 'class IloRange' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\ILOG\cplex81\examples\msvc6\stat_sta\copiaPortfolio3.cpp(79) : error C2593: 'operator =' is ambiguous
C:\ILOG\cplex81\examples\msvc6\stat_sta\copiaPortfolio3.cpp(88) : error C2451: conditional expression of type 'class IloRange' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\ILOG\cplex81\examples\msvc6\stat_sta\copiaPortfolio3.cpp(88) : error C2593: 'operator =' is ambiguous
Error executing cl.exe.[/font]
[B]Can somebody help me?[/B]
[font=Verdana][size=10pt]Thanks!!![/size][/font]
#CPLEXOptimizers#DecisionOptimization