A simple rolling-horizon model is implemented using gurobi, but the result is wrong.(C++ api)
回答済みI am implementing a model based on rolling-horizon and the original problem is very complex so I have given a simple example here, this is my code
#include <cassert>
#include <vector>
#include "gurobi_c++.h"
using namespace std;
int main()
{
GRBEnv env = GRBEnv();
GRBException error;
GRBModel model = GRBModel(env);
vector<GRBVar*> NUM;
for (int i = 1; i <= 3; i++)
{
NUM.push_back(new GRBVar);
}
GRBLinExpr lhs = 0;
GRBQuadExpr obj = 0;
try
{
for (int stage = 0; stage < 3; stage++)
{
int n = stage + 1;
*NUM[stage] = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "x");
lhs += *NUM[stage];
obj += (*NUM[stage]) * (*NUM[stage]);
model.addConstr(lhs == 12, "c0");
model.setObjective(obj, GRB_MINIMIZE);
model.optimize();
for (int i = 0; i <= stage; i++) {
cout << NUM[i]->get(GRB_StringAttr_VarName) << " = " << NUM[i]->get(GRB_DoubleAttr_X) << endl;
}
}
}
catch(...)
{
cout << error.getErrorCode() << endl;
}
}
Here, a mathematical explanation of my model is given:
In stage1, we calculate min x1^2 with the constrain x1 == 12.
In stage2, we calculate min x1^2 + x2^2 with the constrain x1+x2 == 12.
In stage2, we calculate min x1^2 + x2^2 + x3^2 with the constrain x1+x2+x3 == 12.
The result always shows x1 == 12 and the rest of the variables are 0. This is obviously wrong, what is wrong with my program?
0
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
You could use the GRBModel::write() method to write your model to a human readable LP file
model.write("myModel.lp")and then analyze the generated file named \(\texttt{myModel.lp}\). Maybe there is something missing or wrong in your model construction which you should be able to see in the file.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント