Na Freeman Na Freeman - 5 months ago 23
Java Question

Convert IloNumVar variabale of CPLEX to double in Java

I tried to read columns values from excel using java, here a part of code:

.....
List<ReportRow> listOfDataFromReport = new ArrayList<ReportRow>();
for(int x = 1; x<=sheet.getPhysicalNumberOfRows(); x++){
ReportRow rr = new ReportRow();
HSSFRow dataRow = sheet.getRow(x);

int idxForColumn1 = map.get("Cost");
int idxForColumn2 = map.get("Level");

HSSFCell cell1 = dataRow.getCell(idxForColumn1);
HSSFCell cell2 = dataRow.getCell(idxForColumn2);

rr.setCost(cell1.getNumericCellValue());
rr.setLevel(cell2.getNumericCellValue());

.....


So the code to read data from excel works fine, the problem I have is in setters functions, because I don't have the same type, I'm using cplex solver in another class :

private IloNumVar Cost = new IloNumVar;

public IloNumVar getCost() {
return Cost;
}
public void setCost(IloNumVar cost) {
Cost = cost;
}


So I get this error :

The method setCost is not applicable for the arguments (double)


so I want to know if it's possible to covert IloNumVar to double or any solution to use them both, get the value as double but use it as IloNumVar.

EDIT

To explain more, I use electrical powers values(from excel) as inputs to use them to solve problem of cost with cplex solver.

To read data from excel I can only use
getNumericCellValue()
method of
POI
to get values, so I will have variables as
double
, but when I use them to solve my model I must use them as
IloNumVar[]
because I wish to optimize the value of cost of energy during 24 hours depends of values of powers taken from excel file.

Answer Source

IloNumVar is a CPLEX / Concert class that is used to model a variable in a CPLEX model. During the model building stage, it usually does NOT have a single value. Rather, you need to specify the range of values that the IloNumVar can take, usually a lower bound and an upper bound. However, you can set the variable to always have a single value by setting the lower and upper bound of a variable to the same value. In most cases that is rare - perhaps for forcing some known values or decisions into a model for debugging.

Once you have run and solved the model, then the variable should have a single value, because CPLEX should have chosen a value for you. Then you can get that value from an IloNumVar using something like getValue().

Now an observation - in most models,things like 'costs' are known constant values, and we want CPLEX to decide how many things to use at that known cost. Typically, the costs are the coefficients of some terms in the objective function. In such cases, it would be normal to just use a double for those known values. You say nothing about your model, so we can't tell what you are using these IloNumVars for in your model and it may be a good design decision or not.