Error when assigning a GRBVar into a 3d dynamically array in c++
回答済みHello,
I have a problem with the assignment of a GRBVar into a 3d dynamically allocated array in C++ and i dont know why the error occures. The code is down below so
// In test.cpp
GRBVar*** init_x(GRBVar*** x)
{
GRBVar tmp;
x = new GRBVar * *[customers];
for (int i = 0; i < customers; i++)
{
x[i] = new GRBVar * [customers];
for (int k = 0; k < vehicles; k++)
{
x[i][k] = new GRBVar[vehicles];
}
}
return x;
}
// in main.cpp
int main(int argc,char* argv[])
{
GRBVar*** _x{};
GRBVar*** x= init_x(_x);
for (int i = 0; i < customers; i++)
{
for (int j = 0; j < customers; j++)
{
for (int k = 0; k < vehicles; k++)
{
x[i][j][k] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "from x_ " + std::to_string(i) + " goes to " + std::to_string(j) + " with vehicle " + std::to_string(k)); // Here is the error while the compilation is ok the debugging shows me a problem with the operator =
}
}
}
Thanks in advance.
-
正式なコメント
This post is more than two 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. -
Hi Giorgos,
Could you please share the error you see?
Your code is not directly reproducible. But if I initialize customers, vehicles, and the Gurobi modelint customers = 4;
int vehicles = 5;
GRBEnv* env = new GRBEnv();
GRBModel model = GRBModel(*env);I can compile without errors.
Best regards,
Marika0 -
Yes the error is directing me in this line of gurobi_c++.h which is
GRBVar& operator=(const GRBVar& xvar) { varRep = xvar.varRep; return *this; }
and the error is
"Exception thrown: read access violation.
this was 0xFFFFFFFFFFFFFFFF."0 -
Could you post the full example?
What do you mean by "the debugging shows" the error?0 -
Yes sure,
I am running this code in VS 2019 and when i start debugging
then an exception is thrown that points me to this line of "gurobi_c++.h" file
"GRBVar& operator=(const GRBVar& xvar) { varRep = xvar.varRep; return *this; }"
I made some moving from main.cpp to this function in test.cpp
namespace PDPTW_Init
{int vehicles = 10;
int customers = 101;
GRBVar*** init_x(GRBModel& model)
{
GRBVar ***x = new GRBVar * *[customers];
for (int i = 0; i < customers; i++)
{
x[i] = new GRBVar * [customers];
for (int k = 0; k < vehicles; k++)
{
x[i][k] = new GRBVar[vehicles];
}
}// Initialize x if vehicle goes from node i to node j
for (int i = 0; i < customers; i++)
{
for (int j = 0; j < customers; j++)
{
for (int k = 0; k < vehicles; k++)
{
std::string msg = "from x_ " + std::to_string(i) + " goes to " + std::to_string(j) + " with vehicle " + std::to_string(k);
x[i][j][k] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x");
}
}
}return x;
}
};
// main.cpp
#include "PDPTW_Init.h"
#include "gurobi_c++.h"
#include<string>
int main(int argc, char* argv[])
{
GRBVar*** x = nullptr;try
{
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);// Set variables
x = PDPTW_Init::init_x(model);}
catch (GRBException e)
{
std::cout << "Error code = "
<< e.getErrorCode()
<< std::endl;
std::cout << e.getMessage() << std::endl;
}
catch (...)
{
std::cout << "Exception during optimization"
<< std::endl;
}
return 0;
}0 -
I think your error is in init_x() when declaring the variables. The second for-loop should be
for (int k = 0; k < customers; k++)
instead offor (int k = 0; k < vehicles; k++)
0 -
God bless you! Thank you so much i am struggling with this for days... Solved <3
0
投稿コメントは受け付けていません。
コメント
7件のコメント