Gurobi in Java - How can I add two Expression?
OngoingHello,
I declare a new expression such as GRBQuadExpr[ ] ObjExpr = new GRBQuadExpr[size];
I have another two expressions such as GRBLinExpr LinObjExpr and GRBQuadExpr QuadObjExpr.
for ( count=0; count<size; count++){
ObjExpr[count] = LinObjExpr + QuadObjExpr
}
This summation is wrong. In Cplex there is a built-in function, model.sum, to add expressions. Is there any built-in function in Gurobi?
Thank you in advance.
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi,
you could use the add() function as
ObjExpr[count].add(LinObjExpr);
ObjExpr[count].add(QuadObjExpr);Does this work for you?
Best regards,
Jaromił0 -
Hi Jaromil,
How do you define ObjExpr in this code? I wrote the code and defined ObjExpr as GRBQuadExpr[ ] but gives error?
Thank you in advance
Best regards,
Fulya
0 -
Dear Fulya,
What kind of error do you get?
Here is an example code:
GRBLinExpr l1 = new GRBLinExpr();
l1.addTerm(1.0, x); // adding 1.0 * x to l1 where x is a GRBVar
GRBQuadExpr q1 = new GRBQuadExpr();
q1.addTerm(1.0, x, y); // adding 1.0 * x * y to q1 where x and y are GRBVar
GRBQuadExpr obj = new GRBQuadExpr();
obj.add(l1); // adding GRBLinExpr to obj
obj.add(q1); // adding GRBQuadExpr to objDoes this help?
Best regards,
Jaromił0 -
Dear Jaromil,
The error is NullPointerException. The error line is shown in
GRBQuadExpr[] objExpr = new GRBQuadExpr[sizeOfTerm];
GRBQuadExpr Sum = new GRBQuadExpr(); // yeni Ekledim
GRBLinExpr linearCoeff = new GRBLinExpr();
GRBQuadExpr quadCoeff = new GRBQuadExpr();
for (Entry<Integer, Map<String, HourlySegmentNewGurobi>> entry : hourlySegments.entrySet()) {
period = entry.getKey();
for (String segmentId : hourlySegments.get(period).keySet()) {
linearCoeff = setLinearCoeffOfHourly(hourlySegments.get(period).get(segmentId),
model, hourlyVars.get(segmentId));
quadCoeff = setQuadraticCoeffOfHourly(hourlySegments.get(period).get(segmentId), model,
hourlyVars.get(segmentId));
objExpr[count].add(linearCoeff); // This line
objExpr[count].add(quadCoeff);
Sum.add(coeffsOfHourly[count]);
count++;
}
}Best Regards,
Thank you
0 -
Dear Fulya,
It seems like the \(\texttt{linearCoeff}\) object you calculate via your \(\texttt{setLinearCoeffOfHourly}\) function is a \(\texttt{NullPointer}\). You could check this by
if (linearCoeff == null){
System.out.println("Error: linearCoeff is a NullPointer");
}Best regards,
Jaromił0 -
Dear Jaromil,
I wrote after this code
linearCoeff = setLinearCoeffOfHourly(hourlySegments.get(period).get(segmentId),
model, hourlyVars.get(segmentId));and checked it but the code do not give this error message.
Best Regards,
Fulya
0 -
Dear Fulya,
Could you post the whole \(\texttt{NullPointerException}\) error message you are getting?
Best regards,
Jaromił0 -
Dear Jaromil,
The error message such that
Exception in thread "main" java.lang.NullPointerException
After that shows the places:
at ....ObjectiveFunctionNewGurobi.setExprForHourly(ObjectiveFunctionNewGurobi.java:117)
at ....ObjectiveFunctionNewGurobi.setQuadratic(ObjectiveFunctionNewGurobi.java:42)
at ....SolverNewGurobi.setupModel(SolverNewGurobi.java:363)
at ....SolverNewGurobi.<init>(SolverNewGurobi.java:76)
at ....OptimizationEngineNewGurobi.solvePreProblem(OptimizationEngineNewGurobi.java:191)
at ....OptimizationEngineNewGurobi.run(OptimizationEngineNewGurobi.java:131)
at ....Main.runThroughFile(Main.java:265)
at ....Main.main(Main.java:95)0 -
Dear Fulya,
The error message indicates that the error comes from the \(\texttt{setExprForHourly}\) function found in file \(\texttt{ObjectiveFuncitonNewGurobi.java}\) at line 117. Please try to fix the code therein to avoid the NullPointerException.
Best regards,
Jaromił0
Post is closed for comments.
Comments
10 comments