Error: 10005 Unable to retrieve attribute 'X'
回答済みI made this code for this problem in 3 dimensions, for some reason it was giving the error, Unable to retrieve attribute 'ObjVal' and now it is giving the error Unable to retrieve attribute 'X', without any modification, can someone help me?
#include "gurobi_c++.h"
#include <sstream>
#include <fstream>
#include <vector>
#include <time.h>
using namespace std;
#define NREGIOES 4
#define NFABRICAS 4
#define NMESES 12
double FO;
double c[NREGIOES][NFABRICAS] = { 10.8, 12, 7.2, 26, 3.6, 4, 6.8, 25.2, 10.8, 9.2, 14.4, 21.6, 16.4, 14.8, 23.4, 10.8 };
double b[NREGIOES][NFABRICAS] = { 0, 7.2, 8.4, 18.4, 7.2, 0, 10, 17.2, 8.4, 10, 0, 22.8, 18.4, 17.2, 22.8, 0 };
double h[NREGIOES][NMESES] = { 10.3,12.4,9.7,10.5 };
double f[NREGIOES][NMESES] = { 8.1,7.2,7.6,5.4 };
double g[NREGIOES][NMESES] = { 5.3,4.1,4.6,3.8 };
double pmax[NFABRICAS] = { 240000,180000,160000,170000 };
double ebmax[NFABRICAS] = { 1500000,750000,1250000,1000000 };
double epmax[NFABRICAS] = { 1000000,800000,1000000,900000 };
double T[NREGIOES][NMESES] = {
140000, 250000, 130000, 150000,
180000, 360000, 240000, 250000,
260000, 380000, 370000, 260000,
280000, 370000, 400000, 250000,
380000, 350000, 420000, 200000,
350000, 230000, 380000, 120000,
230000, 120000, 210000, 0,
150000, 0, 110000, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 100000,
0, 110000, 0, 110000 };
double d[NMESES] = { 700000, 600000, 600000, 500000, 500000, 400000, 300000, 500000, 700000, 900000, 800000, 800000 };
double respostaX[NREGIOES][NFABRICAS][NMESES];
double respostaY[NREGIOES][NFABRICAS][NMESES];
double respostaZ[NREGIOES][NFABRICAS][NMESES];
double respostaEB[NFABRICAS][NMESES];
double respostaEP[NFABRICAS][NMESES];
double respostaP[NFABRICAS][NMESES];
time_t t_ini, t_fim;
double tempo;
int main() {
//---------------------------------------------------------------------
// GUROBI
//---------------------------------------------------------------------
time(&t_ini);
GRBEnv* env = 0;
GRBVar** eb = 0;
GRBVar** ep = 0;
GRBVar** p = 0;
GRBVar*** x = 0;
GRBVar*** y = 0;
GRBVar*** z = 0;
try
{
cout << "\n---------------------------------\n\n";
cout << "\nIniciando a resolucao do problema.\n\n";
env = new GRBEnv();
GRBModel model = GRBModel(*env);
model.set(GRB_StringAttr_ModelName, "Exercicio_41");
model.getEnv().set(GRB_DoubleParam_TimeLimit, 3600);
model.getEnv().set(GRB_DoubleParam_Heuristics, 0.05);
//---------------------------------------------------------------------
// DADOS DE ENTRADA
//---------------------------------------------------------------------
x = new GRBVar** [NREGIOES];
for (int i = 0; i < NREGIOES; i++) {
x[i] = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
x[i][j] = new GRBVar[NMESES];
for (int k = 0; k < NMESES; k++) {
x[i][j][k] = model.addVar(GRB_DoubleAttr_LB, GRB_DoubleAttr_UB, GRB_DoubleAttr_Obj, GRB_INTEGER);
model.update();
ostringstream vname;
vname << "x" << i << j << k;
x[i][j][k].set(GRB_DoubleAttr_Obj, 0);
x[i][j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
}
y = new GRBVar * *[NREGIOES];
for (int i = 0; i < NREGIOES; i++) {
y[i] = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
y[i][j] = new GRBVar[NMESES];
for (int k = 0; k < NMESES; k++) {
y[i][j][k] = model.addVar(GRB_DoubleAttr_LB, GRB_DoubleAttr_UB, GRB_DoubleAttr_Obj, GRB_INTEGER);
model.update();
ostringstream vname;
vname << "y" << i << j << k;
y[i][j][k].set(GRB_DoubleAttr_Obj, 0);
y[i][j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
}
z = new GRBVar * *[NREGIOES];
for (int i = 0; i < NREGIOES; i++) {
z[i] = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
z[i][j] = new GRBVar[NMESES];
for (int k = 0; k < NMESES; k++) {
z[i][j][k] = model.addVar(GRB_DoubleAttr_LB, GRB_DoubleAttr_UB, GRB_DoubleAttr_Obj, GRB_INTEGER);
model.update();
ostringstream vname;
vname << "z" << i << j << k;
z[i][j][k].set(GRB_DoubleAttr_Obj, 0);
z[i][j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
}
eb = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
eb[j] = model.addVars(NMESES, GRB_INTEGER);
model.update();
for (int k = 0; k < NMESES; k++) {
ostringstream vname;
vname << "eb" << j << k;
eb[j][k].set(GRB_DoubleAttr_Obj, 0);
eb[j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
ep = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
ep[j] = model.addVars(NMESES, GRB_INTEGER);
model.update();
for (int k = 0; k < NMESES; k++) {
ostringstream vname;
vname << "ep" << j << k;
ep[j][k].set(GRB_DoubleAttr_Obj, 0);
ep[j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
p = new GRBVar * [NFABRICAS];
for (int j = 0; j < NFABRICAS; j++) {
p[j] = model.addVars(NMESES, GRB_INTEGER);
model.update();
for (int k = 0; k < NMESES; k++) {
ostringstream vname;
vname << "p" << j << k;
p[j][k].set(GRB_DoubleAttr_Obj, 0);
p[j][k].set(GRB_StringAttr_VarName, vname.str());
}
}
//---------------------------------------------------------------------
// FO - FUNÇÃO OBJETIVO
//---------------------------------------------------------------------
GRBLinExpr obj = 0;
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
obj += (c[i][j] * x[i][j][k]) + b[i][j] * (y[i][j][k] + z[i][j][k]) + h[j][k] * p[j][k] + f[j][k] * eb[j][k] + g[j][k] * ep[j][k];
}
}
}
model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE);
model.update();
//---------------------------------------------------------------------
// RESTRIÇÃO 1 - CAPACIDADE DE PROCESSAMENTO
//---------------------------------------------------------------------
for (int j = 0; j < NFABRICAS; j++) {
GRBLinExpr r1;
for (int k = 0; k < NMESES; k++) {
r1 = p[j][k];
model.addConstr(r1 <= pmax[j], "R1");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 2 - CAPACIDADE DE ESTOQUE BRUTO
//---------------------------------------------------------------------
for (int j = 0; j < NFABRICAS; j++) {
GRBLinExpr r2;
for (int k = 0; k < NMESES; k++) {
r2 = eb[j][k];
model.addConstr(r2 <= ebmax[j], "R2");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 3 - CAPACIDADE DE ESTOQUE PROCESSADO
//---------------------------------------------------------------------
for (int j = 0; j < NFABRICAS; j++) {
GRBLinExpr r3;
for (int k = 0; k < NMESES; k++) {
r3 = ep[j][k];
model.addConstr(r3 <= epmax[j], "R3");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 4 - PRODUTOR PARA A FÁBRICA
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r4;
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
r4 += x[i][j][k];
}
model.addConstr(r4 <= T[i][k], "R4");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 5 - PROCESSADO = DEMANDA
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r5;
for (int j = 0; j < NFABRICAS; j++) {
if (k==0)
{
r5 += p[j][k];
}
else {
r5 += p[j][k] + ep[j][k - 1];
}
}
model.addConstr(r5 == d[k], "R5");
}
//---------------------------------------------------------------------
// RESTRIÇÃO 6 - PROCESSADO = DEMANDA + sobra
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r6;
for (int j = 0; j < NFABRICAS; j++) {
if (k == 0)
{
r6 += p[j][k];
}
else {
r6 += p[j][k] + ep[j][k - 1];
}
model.addConstr(r6 == d[k] + ep[j][k], "R6");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 7 - PROCESSADO + BRUTO = BRUTO
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r7;
for (int j = 0; j < NFABRICAS; j++) {
for (int i = 0; i < NREGIOES; i++) {
if (k == 0)
{
r7 += y[i][j][k];
}
else {
r7 += y[i][j][k] + eb[j][k - 1];
}
}
model.addConstr(r7 == eb[j][k] + p[j][k], "R7");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 8 - O QUE SAI DE BRUTO = AO BRUTO
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r8;
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
r8 += y[i][j][k];
}
model.addConstr(r8 <= eb[i][k], "R8");
}
}
//---------------------------------------------------------------------
// RESTRIÇÃO 9 - O QUE SAI DE PROCESSADO = AO PROCESSADO
//---------------------------------------------------------------------
for (int k = 0; k < NMESES; k++) {
GRBLinExpr r9;
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
r9 += z[i][j][k];
}
model.addConstr(r9 <= ep[i][k], "R9");
}
}
//---------------------------------------------------------------------
// INICIA RESOLUÇÃO DO MODELO
//---------------------------------------------------------------------
model.update();
model.write("Exercicio41.modelo.lp");
model.optimize();
model.write("Exercicio41.modelo.sol");
//---------------------------------------------------------------------
// EXPORTAR A SOLUÇÃO
//---------------------------------------------------------------------
FO = model.get(GRB_DoubleAttr_ObjVal);
cout << "\nFUNCAO OBJETIVO = " << FO << endl;
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
respostaX[i][j][k] = x[i][j][k].get(GRB_DoubleAttr_X);
respostaY[i][j][k] = y[i][j][k].get(GRB_DoubleAttr_X);
respostaZ[i][j][k] = z[i][j][k].get(GRB_DoubleAttr_X);
respostaEB[j][k] = eb[j][k].get(GRB_DoubleAttr_X);
respostaEP[j][k] = ep[j][k].get(GRB_DoubleAttr_X);
respostaP[j][k] = p[j][k].get(GRB_DoubleAttr_X);
}
}
}
}
catch (GRBException e) {
cout << "Gurobi - Código de erro: " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
}
catch (...) {
cout << "Gurobi - Erro durante a Otimização" << endl;
}
delete[] x;
delete[] y;
delete[] z;
delete[] eb;
delete[] ep;
delete[] p;
delete env;
time(&t_fim);
tempo = difftime(t_fim, t_ini);
cout << "O problema foi resolvido em: " << tempo << "segundos. \n";
cout << "\n---------------------------------\n\n";
ofstream destino("Exercicio41.saida.txt");
if (!destino) {
cerr << "\nErro ao abrir o arquivo Exercicio41.saida.txt\n\n";
}
destino << "\n\tFUNCAO OBJETIVO: " << FO << endl;
destino << "X[i][j][K]";
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaX[i][j][k];
}
}
}
destino << "Y[i][j][K]";
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaY[i][j][k];
}
}
}
destino << "Z[i][j][K]";
for (int i = 0; i < NREGIOES; i++) {
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaZ[i][j][k];
}
}
}
destino << "EB[j][K]";
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaEB[j][k];
}
}
destino << "EP[j][K]";
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaEP[j][k];
}
}
destino << "P[j][K]";
for (int j = 0; j < NFABRICAS; j++) {
for (int k = 0; k < NMESES; k++) {
destino.width(2);
destino << respostaP[j][k];
}
}
return 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 why not try our AI Gurobot?. -
Hi José,
You model is infeasible. Thus, the ObjVal and X attributes are not available. You should only check for these attributes if your problem has been solved to optimality (or hit a timelimit). You can check the optimization status
int optimstatus = model.get(GRB_IntAttr_Status);
if (optimstatus == GRB_OPTIMAL) {
// your code
}Example mip2_c++ has some additional checks for the optimization status. To determine why your model is infeasible, please refer to the Knowledge Base article How do I determine why my model is infeasible?
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント