int solveAndPrint(GRBModel& model, GRBVar& totSlack, int nWorkers, string* Workers, GRBVar* totShifts) throw(GRBException) { model.optimize(); int status = model.get(GRB_IntAttr_Status); if ((status == GRB_INF_OR_UNBD) || (status == GRB_INFEASIBLE) || (status == GRB_UNBOUNDED)) { cout << "The model cannot be solved " << "because it is infeasible or unbounded" << endl; return status; } if (status != GRB_OPTIMAL) { cout << "Optimization was stopped with status " << status << endl; return status; } // Print total slack and the number of shifts worked for each worker cout << endl << "Total slack required: " << totSlack.get(GRB_DoubleAttr_X) << endl; for (int w = 0; w < nWorkers; ++w) { cout << Workers[w] << " worked " << totShifts[w].get(GRB_DoubleAttr_X) << " shifts" << endl; } cout << endl; return status; }
void printSolution(GRBModel& model, int nCategories, int nFoods, GRBVar* buy, GRBVar* nutrition) throw(GRBException) { if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) { cout << "\nCost: " << model.get(GRB_DoubleAttr_ObjVal) << endl; cout << "\nBuy:" << endl; for (int j = 0; j < nFoods; ++j) { if (buy[j].get(GRB_DoubleAttr_X) > 0.0001) { cout << buy[j].get(GRB_StringAttr_VarName) << " " << buy[j].get(GRB_DoubleAttr_X) << endl; } } cout << "\nNutrition:" << endl; for (int i = 0; i < nCategories; ++i) { cout << nutrition[i].get(GRB_StringAttr_VarName) << " " << nutrition[i].get(GRB_DoubleAttr_X) << endl; } } else { cout << "No solution" << endl; } }
int main(int argc, char *argv[]) { try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); // Create variables GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x"); GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y"); GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "z"); // Integrate new variables model.update(); // Set objective: maximize x + y + 2 z model.setObjective(x + y + 2 * z, GRB_MAXIMIZE); // Add constraint: x + 2 y + 3 z <= 4 model.addConstr(x + 2 * y + 3 * z <= 4, "c0"); // Add constraint: x + y >= 1 model.addConstr(x + y >= 1, "c1"); // Optimize model model.optimize(); cout << x.get(GRB_StringAttr_VarName) << " " << x.get(GRB_DoubleAttr_X) << endl; cout << y.get(GRB_StringAttr_VarName) << " " << y.get(GRB_DoubleAttr_X) << endl; cout << z.get(GRB_StringAttr_VarName) << " " << z.get(GRB_DoubleAttr_X) << endl; cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl; } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return 0; }
int monitoramento_em_grafo_bipartido( ListGraph &g, NodeName &vname, ListGraph::NodeMap<double> &custo, ListGraph::NodeMap<int> &solucao) { int seed=0; GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.getEnv().set(GRB_IntParam_Seed, seed); model.set(GRB_StringAttr_ModelName, "Monitoramento em Grafo Bipartido"); // prob. name model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem // ------------------------------------------------------ // Construa o modelo daqui para baixo // ------------------------------------------------------ // Exemplos de como voce pode declarar variaveis indexadas nos vertices ou nas arestas. // Nao necessariamente voce precisa dos dois tipos // ListGraph::NodeMap<GRBVar> x(g); // variables for each node // ListGraph::EdgeMap<GRBVar> y(g); // variables for each edge ListGraph::NodeMap<GRBVar> x(g); // variables for each node ListGraph::EdgeMap<GRBVar> y(g); // variables for each edge int name = 0; char namme[100]; for(ListGraph::NodeIt v(g); v != INVALID; ++v) { sprintf(namme,"PC_%s",vname[v].c_str()); x[v] = model.addVar(0.0, 1.0, custo[v],GRB_CONTINUOUS,namme); } model.update(); try { for(ListGraph::EdgeIt e(g); e != INVALID; ++e) { //Para cada aresta, um dos lados e 1 GRBLinExpr expr; expr += x[g.u(e)]; expr += x[g.v(e)]; model.addConstr(expr >= 1); } model.update(); // ------------------------------------------------------ // Construa o modelo daqui para cima // ------------------------------------------------------ //model.write("model.lp"); system("cat model.lp"); model.optimize(); for (ListGraph::NodeIt v(g); v!=INVALID; ++v) { if (x[v].get(GRB_DoubleAttr_X)>1-EPS) solucao[v] = 1; else solucao[v] = 0; //solucao[v] = 1; } return(1); } catch (...) {cout << "Error during callback..." << endl; return(0);} }
vector <double> solve(int verbose, vector <vector <double> > M) { vector <double> solution; try { GRBEnv env = GRBEnv(); int numPoints = M.size(); int numCircles = M.back().size(); GRBVar p[numPoints]; double coeff[numPoints]; GRBModel model = GRBModel(env); GRBLinExpr expr; if(!verbose) model.getEnv().set(GRB_IntParam_OutputFlag, 0); for (int i=0;i<numPoints;i++){ p[i] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY); coeff[i] = 1.0; } expr.addTerms(coeff, p,numPoints); model.update(); model.setObjective(expr, GRB_MINIMIZE); for(int i=0;i<numCircles;i++){ GRBLinExpr cexpr; double ccoeff[numPoints]; for(int j=0;j<numPoints;j++){ ccoeff[j] = M[j].back(); M[j].pop_back(); } cexpr.addTerms(ccoeff,p,numPoints); model.addConstr(cexpr, GRB_GREATER_EQUAL,1.0); } // Optimize model model.optimize(); for (int i=0;i<numPoints;i++){ solution.push_back((double)p[i].get(GRB_DoubleAttr_X)); } } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return solution; }
int main(int argc, char *argv[]) { try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); // Create variables GRBVar x1 = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_INTEGER, "x1"); GRBVar x2 = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_INTEGER, "x2"); // Integrate new variables model.update(); // Set objective: maximize 2 x1 + x2 model.setObjective(2 * x1 + x2, GRB_MAXIMIZE); // Add constraint: x2 + 0.1 x1 <= 8 model.addConstr(x2 + 0.1 * x1 <= 8, "c0"); // Add constraint: x2 + 5 x1 <= 70 model.addConstr(x2 + 5 * x1 <= 70, "c1"); // Optimize model model.optimize(); cout << x1.get(GRB_StringAttr_VarName) << " " << x1.get(GRB_DoubleAttr_X) << endl; cout << x2.get(GRB_StringAttr_VarName) << " " << x2.get(GRB_DoubleAttr_X) << endl; cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl; } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return 0; }
int main(int argc, char *argv[]) { int k,found; Digraph g; // graph declaration string digraph_kpaths_filename, source_node_name, target_node_name; DiNodeName vname(g); // name of graph nodes Digraph::NodeMap<double> px(g),py(g); // xy-coodinates for each node Digraph::NodeMap<int> vcolor(g);// color of nodes Digraph::ArcMap<int> ecolor(g); // color of edges ArcWeight lpvar(g); // used to obtain the contents of the LP variables ArcWeight weight(g); // edge weights Digraph::ArcMap<GRBVar> x(g); // binary variables for each arc vector <DiNode> V; DiNode source,target; int seed=0; srand48(1); // uncomment one of these lines to change default pdf reader, or insert new one //set_pdfreader("open"); // pdf reader for Mac OS X //set_pdfreader("xpdf"); // pdf reader for Linux set_pdfreader("evince"); // pdf reader for Linux //set_pdfreader("open -a Skim.app"); // double cutoff; // used to prune non promissing branches (of the B&B tree) if (argc!=5) {cout<<endl<<"Usage: "<< argv[0]<<" <digraph_kpaths_filename> <source_node_name> <target_node_name> <k>"<< endl << endl; cout << "Example: " << argv[0] << " digr_triang_sparse_100 12 50 5" << endl << endl; exit(0);} digraph_kpaths_filename = argv[1]; source_node_name = argv[2]; target_node_name = argv[3]; k = atoi(argv[4]); GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.getEnv().set(GRB_IntParam_Seed, seed); model.set(GRB_StringAttr_ModelName, "Oriented k-Paths with GUROBI"); // prob. name model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem ReadListDigraph(digraph_kpaths_filename,g,vname,weight,px,py,0); found=0; for (DiNodeIt v(g);v!=INVALID;++v) if(vname[v]==source_node_name){source=v;found=1;break;} if (!found) {cout<<"Could not find source node "<<source_node_name<<endl;exit(0);} found=0; for (DiNodeIt v(g);v!=INVALID;++v) if(vname[v]==target_node_name){target=v;found=1;break;} if (!found) {cout<<"Could not find target node "<<target_node_name<<endl;exit(0);} kPaths_Instance T(g,vname,px,py,weight,source,target,k); //for (DiNodeIt v(g);v!=INVALID;++v){ if(v==T.V[0])vcolor[v]=RED; else vcolor[v]=BLUE;} //for (int i=1;i<T.nt;i++) vcolor[T.V[i]] = MAGENTA; //for (ArcIt e(g); e != INVALID; ++e) ecolor[e] = BLUE; //ViewListDigraph(g,vname,px,py,vcolor,ecolor,"Triangulated graph"); // Generate the binary variables and the objective function // Add one binary variable for each edge and set its cost in the objective function for (Digraph::ArcIt e(g); e != INVALID; ++e) { char name[100]; sprintf(name,"X_%s_%s",vname[g.source(e)].c_str(),vname[g.target(e)].c_str()); x[e] = model.addVar(0.0, 1.0, weight[e],GRB_CONTINUOUS,name); } model.update(); // run update to use model inserted variables try { //if (time_limit >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,time_limit); //model.getEnv().set(GRB_DoubleParam_ImproveStartTime,10); //try better sol. aft. 10s // if (cutoff > 0) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff ); //model.write("model.lp"); system("cat model.lp"); // Add degree constraint for each node (sum of solution edges incident to a node is 2) for (Digraph::NodeIt v(g); v!=INVALID; ++v) { GRBLinExpr exprin, exprout; for (Digraph::InArcIt e(g,v); e != INVALID; ++e) exprin += x[e]; for (Digraph::OutArcIt e(g,v); e != INVALID; ++e) exprout += x[e]; if (v==source) model.addConstr(exprout - exprin == k ); else if (v==target) model.addConstr(exprin - exprout == k ); else model.addConstr(exprin - exprout == 0 ); } model.optimize(); double soma=0.0; for (DiNodeIt v(g);v!=INVALID;++v) vcolor[v]=BLUE; // all nodes BLUE vcolor[source]=RED; // change the colors of the source node vcolor[target]=RED; // and the target node to RED for (Digraph::ArcIt e(g); e!=INVALID; ++e) { lpvar[e] = x[e].get(GRB_DoubleAttr_X); if (lpvar[e] > 1.0 - EPS) soma += weight[e]; } cout << "kPaths Tree Value = " << soma << endl; //----------------------------------------------------------------- // By Lucas Prado Melo: coloring paths by bfs bool ok = true; for(int i=0; i < k && ok; i++) { queue<DiNode> q; Digraph::NodeMap<Arc> pre(g, INVALID); q.push(source); while (!q.empty()) { DiNode cur = q.front(); q.pop(); for(Digraph::OutArcIt e(g, cur); e!=INVALID; ++e) { DiNode nxt = g.runningNode(e); if (pre[nxt] == INVALID && ecolor[e] == NOCOLOR && lpvar[e] > 1.0 - EPS) { pre[nxt] = e; q.push(nxt); } } } if (pre[target] == INVALID) ok = false; else { DiNode x = target; while (x != source) { ecolor[pre[x]] = 3+i%6; // use colors 3(RED) to 8(CYAN), see myutils.h x = g.oppositeNode(x, pre[x]); } } } if (!ok) {cout << "Nao eh possivel encontrar os " << k << " caminhos!" << endl;} for(Digraph::ArcIt e(g); e!=INVALID; ++e) { if (lpvar[e] > 1.0 - EPS && ecolor[e] == NOCOLOR ) { cout << "Alguma(s) aresta(s) nao pertencem a qualquer caminho!" << endl; break; } } for(Digraph::ArcIt e(g); e!=INVALID; ++e) { if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) { ecolor[e] = GRAY; } } for(Digraph::ArcIt e(g); e!=INVALID; ++e) { if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) { cout << "Alguma(s) aresta(s) possuem valor fracionario! (marcadas com cor cinza claro)" << endl; break; } } //----------------------------------------------------------------- cout << "kPaths Tree Value = " << soma << endl; ViewListDigraph(g,vname,px,py,vcolor,ecolor, "minimum kPaths cost in graph with "+IntToString(T.nnodes)+ " nodes and "+IntToString(k)+" paths: "+DoubleToString(soma)); } catch (...) {cout << "Error during callback..." << endl; } return 0; }
ChannelsFitness ILPChannelingGurobi(DataStructure *dataStructure, double const alpha) { try { unsigned int messageCount; double alphaCoef = alpha; GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); //model.getEnv().set(GRB_DoubleParam_TimeLimit, 3600); ChannelsFitness cfResult; CAMessage *messages; messageCount = static_cast<unsigned int>(AggregateMessages(dataStructure, messages)); vector<int> w(messageCount); transform(messages, messages + messageCount, w.begin(), [](CAMessage &message) { return message.payload; }); int sumW = 0; for (int i = 0; i < messageCount; i++) { for (int j = 0; j < static_cast<int>(messages[i].nodes.size()); j++) { if (dataStructure->nodesOnBothChannels[messages[i].nodes[j]] != 1) { sumW += messages[i].payload; break; } } } if (alphaCoef < 0) alphaCoef = 1.0 / sumW; GRBVar z = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "z"); GRBVar Pa = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "Pa"); GRBVar Pb = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "Pb"); GRBVar Pg = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "Pg"); vector<GRBVar> Uea(messageCount); vector<GRBVar> Ueb(messageCount); vector<GRBVar> xi(static_cast<unsigned int>(dataStructure->nodeCount)); generate_n(Uea.begin(), messageCount, [&model]() { return model.addVar(0.0, 1.0, 0.0, GRB_CONTINUOUS, "Uea"); }); generate_n(Ueb.begin(), messageCount, [&model]() { return model.addVar(0.0, 1.0, 0.0, GRB_CONTINUOUS, "Ueb"); }); generate_n(xi.begin(), dataStructure->nodeCount, [&model]() { return model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "xi"); }); model.update(); model.setObjective(z + alphaCoef * Pg, GRB_MINIMIZE); model.addConstr(Pa <= z); model.addConstr(Pb <= z); model.addConstr(Pa + Pb - sumW == Pg); model.addConstr(expSum(Uea, w) == Pa); model.addConstr(expSum(Ueb, w) == Pb); for (int e = 0; e < messageCount; e++) { for (int i = 0; i < static_cast<int>(messages[e].nodes.size()); i++) // Problem!!! { if (dataStructure->nodesOnBothChannels[messages[e].nodes[i]] != 1) { model.addConstr(xi[messages[e].nodes[i]] - Uea[e] <= 0); model.addConstr(xi[messages[e].nodes[i]] + Ueb[e] >= 1); } } } model.addConstr(xi[0] == 1); model.optimize(); switch (model.get(GRB_IntAttr_Status)) { case GRB_INFEASIBLE: case GRB_UNBOUNDED: cfResult.channelA = cfResult.channelB = cfResult.gateway = -1; return cfResult; case GRB_TIME_LIMIT: if (model.get(GRB_IntAttr_SolCount) <= 0) { cfResult.channelA = cfResult.channelB = cfResult.gateway = -1; return cfResult; } default: break; } for (int i = 0; i < dataStructure->nodeCount; i++) dataStructure->nodesChannel[i] = static_cast<char>(xi[i].get(GRB_DoubleAttr_X)); /*for(i = 0; i < number_variables; i++) { printf("%d ", static_cast<int>(glp_mip_col_val(mip,i+1))); }*/ cfResult.channelA = static_cast<int>(floor(Pa.get(GRB_DoubleAttr_X) + 0.5)); cfResult.channelB = static_cast<int>(floor(Pb.get(GRB_DoubleAttr_X) + 0.5)); cfResult.gateway = static_cast<int>(floor(Pg.get(GRB_DoubleAttr_X) + 0.5)); for (int i = 0; i < dataStructure->nodeCount; i++) { printf("%d ", static_cast<int>(xi[i].get(GRB_DoubleAttr_X))); } printf("\n"); printf("/ %d, %d, %d\n", cfResult.channelA, cfResult.channelB, cfResult.gateway); delete[] messages; return cfResult; } catch (GRBException e) { cout << e.getMessage() << "\n"; } }
int main(int argc,char *argv[]) { srand48(1); if (argc!=2) {cout<<endl<<"Usage: "<< argv[0]<<" <filename>"<< endl << endl; cout << "Example: " << argv[0] << " arq1.in" << endl << endl; exit(0);} ifstream ifile; ifile.open(argv[1]); if (!ifile) return(false); int n, m; ifile >> n; ifile >> m; vector<double> jobsize(n); vector<double> machinespeed(m); for(int i=0; i<n; i++) ifile >> jobsize[i]; for(int i=0; i<m; i++) ifile >> machinespeed[i]; ifile.close(); cout << endl; cout << "Numero de tarefas: " << n << endl; cout << "Tamanho das tarefas" << endl; for(int i=0; i<n; i++) cout << "T_" << i << ": " << jobsize[i] << endl; cout << "Velocidade das maquinas" << endl; for(int i=0; i<m; i++) cout << "M_" << i << ": " << machinespeed[i] << endl; cout << endl << endl; try { /*--------------------------------------------------------------------------------- */ /*--------------------------- ALTERE DAQUI PARA ABAIXO ---------------------------- */ /*--------------------------------------------------------------------------------- */ // Voce deve atualizar esta variavel com valores 0 e 1 de tal forma que // tarefa_maquina[i][j] = 1 SE_E_SOMENTE_SE a tarefa i foi escalonada na maq. j vector<vector<int> > tarefa_maquina(n, vector<int>(m)); // mais abaixo eh feito um escalonamento aleatorio. // Nao se esqueca de alterar a geracao do escalonamento aleatorio para o // escalonamento obtido pelo seu programa linear inteiro // cabecalho comum para os programas lineares (descomente) int seed=0; GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.getEnv().set(GRB_IntParam_Seed, seed); model.set(GRB_StringAttr_ModelName, "Escalonamento de Tarefas"); // prob. name model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem GRBVar min; GRBVar x[m][n]; min = model.addVar(0, 30000, 1 , GRB_CONTINUOUS, "min"); int i,j; for(i=0;i<m;i++){ for(j=0;j<n;j++) { char name[100]; sprintf(name,"I%dM%d",i,j); x[i][j] = model.addVar(0, 1, 0 , GRB_BINARY, name); } } model.update(); //cada tarefa em 1 máquina for (int i=0; i<n; i++){ GRBLinExpr sum2; for (int j=0; j<m; j++){ sum2= sum2 + x[j][i]; } model.addConstr(sum2 == 1); } //min eh pior caso for(int j=0; j<m; j++){ GRBLinExpr sum3; for (i=0; i<n ; i++) { sum3 = sum3 + jobsize[i]*x[j][i]/ machinespeed[j]; } model.addConstr(min >= sum3); } model.update(); model.write("model.lp"); system("cat model.lp"); model.optimize(); // Verifica se obteve solucao otima if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) { cout << "Erro, sistema impossivel" << endl; exit(1); } for (int i=0; i<n; i++){ for (int j=0; j<m; j++){ if(x[j][i].get(GRB_DoubleAttr_X)>0.999) tarefa_maquina[i][j] = 1; else tarefa_maquina[i][j] = 0; } } /*--------------------------------------------------------------------------------- */ /*--------------------------- ALTERE DAQUI PARA CIMA ------------------------------ */ /*--------------------------------------------------------------------------------- */ cout << "\n\n"; double makespan=0; cout << "Escalonamento obtido das tarefas nas maquinas\n"; cout << "Notacao para tarefas : T_id(tamanho_original)\n"; cout << "Notacao para maquinas: M_id(velocidade)\n\n"; for (int j=0; j<m; j++) { double tmaq=0.0; cout << "M_"<<j<<"("<< machinespeed[j] << ") : "; for(int i=0; i<n; i++) { if (tarefa_maquina[i][j] == 1) { cout << "T_" << i << "(" << jobsize[i] << ") "; tmaq += jobsize[i] / machinespeed[j]; } } cout << endl << "Tempo gasto pela maquina M_" <<j << ": " << tmaq << endl<<endl; if (tmaq>makespan) makespan = tmaq; } cout << "\nTempo para completar todas as tarefas: " << makespan << "s\n\n"; // } else cout << "No solution" << "\n"; } // catch (GRBException e) { // cout << "Error code = " << e.getErrorCode() << endl; // cout << e.getMessage() << endl; // } catch (...) { cout << "Exception during optimization" << endl; } return 0; }
// ATENÇÃO: Não modifique a assinatura deste método. bool brach_and_bound999999(TSP_Data_R &tsp, const vector<DNode> &terminais, const vector<DNode> &postos, const DNode source, int delta, int maxTime, vector<DNode> &sol, double &lbound){ // Converte o TSP direcionado para um nao direcionado com duas arestas ListGraph graph; EdgeValueMap weights(graph); // Adiciona os nos for (ListDigraph::NodeIt u(tsp.g); u!=INVALID; ++u) { Node v = graph.addNode(); } // Adiciona as arestas for (ListDigraph::ArcIt ait(tsp.g); ait!=INVALID; ++ait) { // pega os dois nos incidentes Arc a(ait); DNode u = tsp.g.source(a); DNode v = tsp.g.target(a); // cria a mesma aresta no grafo não direcionado Node gu = graph.nodeFromId(tsp.g.id(u)); Node gv = graph.nodeFromId(tsp.g.id(v)); // insere a aresta no grafo nao direcionado Edge e = graph.addEdge(gu, gv); // Atribui pesos as arestas weights[e] = tsp.weight[a]; } NodeStringMap nodename(graph); NodePosMap posicaox(graph); NodePosMap posicaoy(graph); TSP_Data utsp(graph, nodename, posicaox, posicaoy, weights); // utiliza o convertido ListGraph::EdgeMap<GRBVar> x(graph); GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); // TODO: [Opcional] Comente a linha abaixo caso não queira inserir cortes durante a execução do B&B model.getEnv().set(GRB_IntParam_LazyConstraints, 1); model.getEnv().set(GRB_IntParam_Seed, 0); model.set(GRB_StringAttr_ModelName, "TSPR - TSP with Refueling"); // name to the problem model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem // Add one binary variable for each arc and also sets its cost in the objective function for (EdgeIt e(utsp.g); e!=INVALID; ++e) { char name[100]; Edge edge(e); unsigned uid = utsp.g.id(utsp.g.u(edge)); unsigned vid = utsp.g.id(utsp.g.v(edge)); sprintf(name,"x_%s_%s",tsp.vname[tsp.g.nodeFromId(uid)].c_str(),tsp.vname[tsp.g.nodeFromId(vid)].c_str()); x[e] = model.addVar(0.0, 1.0, utsp.weight[e],GRB_BINARY,name); } model.update(); // run update to use model inserted variables // converte os terminais e os postos vector<Node> uterminais; for (auto t : terminais) { unsigned tid = tsp.g.id(t); uterminais.push_back(utsp.g.nodeFromId(tid)); } NodeBoolMap upostos(utsp.g, false); for (auto p: postos) { unsigned pid = tsp.g.id(p); // upostos.push_back(utsp.g.nodeFromId(pid)); upostos[utsp.g.nodeFromId(pid)] = true; } // Adicione restrições abaixo // (1) Nós terminais devem ser visitados exatamente uma vez for (auto v : uterminais) { GRBLinExpr expr = 0; for (IncEdgeIt e(utsp.g,v); e!=INVALID; ++e){ expr += x[e]; } model.addConstr(expr == 2 ); } // (3) Nó source sempre presente no início do caminho Node usource = utsp.g.nodeFromId(tsp.g.id(source)); GRBLinExpr expr = 0; for (IncEdgeIt e(utsp.g,usource); e!=INVALID; ++e){ expr += x[e]; } model.addConstr(expr >= 1 ); try { model.update(); // Process any pending model modifications. //if (maxTime >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,maxTime); subtourelim cb = subtourelim(utsp , x, usource, upostos, delta); model.setCallback(&cb); // TODO: [Opcional] Pode-se utilizar o valor de uma solução heurística p/ acelerar o algoritmo B&B (cutoff value). //cutoff = tsp.BestCircuitValue-MY_EPS; double cutoff = 0.0; if (cutoff > MY_EPS) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff ); model.update(); // Process any pending model modifications. model.optimize(); // Obtém o status da otimização int status = model.get(GRB_IntAttr_Status); if(status == GRB_INFEASIBLE || status == GRB_INF_OR_UNBD){ cout << "Modelo inviavel ou unbounded." << endl; return false; } // Limitante inferior e superior do modelo //lbound = model.get(GRB_DoubleAttr_ObjBoundC); if( model.get(GRB_IntAttr_SolCount) <= 0 ){ cout << "Modelo nao encontrou nenhuma solucao viavel no tempo. LowerBound = " << lbound << endl; return false; } else if (status == GRB_OPTIMAL){ if(verbose) cout << "O modelo foi resolvido ate a otimalidade." << endl; } else { if(verbose) cout << "O modelo encontrou uma solucao sub-otima (i.e. nao ha garantia de otimalidade)." << endl; } double custo_solucao = model.get(GRB_DoubleAttr_ObjVal); int uncovered=0; EdgeBoolMap cover(utsp.g, false); for (EdgeIt e(utsp.g); e!=INVALID; ++e) { if (BinaryIsOne(x[e].get(GRB_DoubleAttr_X))) { cover[e] = true; uncovered++; } } sol.push_back(tsp.g.nodeFromId(utsp.g.id(usource))); convertSol(x, sol, tsp, utsp, usource, cover, uncovered); // Calculo manual do custo da solução (deve ser igual ao ObjVal do Gurobi). double soma=0.0; ArcName aname(tsp.g); vector<Arc> edgesSol; ArcColorMap acolor(tsp.g); // if( verbose ) cout << "####### " << endl << "Edges of Solution (B&B):" << endl; // for (EdgeIt e(utsp.g); e!=INVALID; ++e){ // if (BinaryIsOne(x[e].get(GRB_DoubleAttr_X))){ // Note que se este método serve para variáveis binárias, p/ inteiras terá de usar outro método. // soma += utsp.weight[e]; // edgesSol.push_back(tsp.g.arcFromId(utsp.g.id(e))); // if( verbose) cout << "(" << tsp.vname[tsp.g.nodeFromId(utsp.g.id(utsp.g.u(e)))] << "," << tsp.vname[tsp.g.nodeFromId(utsp.g.id(utsp.g.v(e)))] << ")" << endl; // acolor[tsp.g.arcFromId(utsp.g.id(e))] = BLUE; // } // } // if( verbose ) cout << "####### " << endl; if( verbose ) cout << "####### " << endl << "Edges of Solution (B&B):" << endl; DNode u = sol[0]; for (int i=1; i<sol.size(); i++) { DNode v = sol[i]; soma += tsp.AdjMatD.Cost(u,v); if ( verbose ) cout << "(" << tsp.vname[u] << "," << tsp.vname[v] << ")" << endl; u = v; } if( verbose ) cout << "####### " << endl; if( verbose ) cout << "Custo calculado pelo B&B = "<< soma << " / " << custo_solucao << endl; if( verbose ){ cout << "Caminho encontrado a partir do vértice de origem (" << tsp.vname[source] << "): "; for(auto node : sol){ cout << tsp.vname[node] << " "; } // Obs: O caminho é gerado a partir do nó source, se o conjunto de arestas retornado pelo B&B for desconexo, o caminho retornado por 'path_search' será incompleto. cout << endl << "Custo calculado da solucao (caminho a partir do no origem) = " << solutionCost(tsp, sol) << endl; ostringstream out; out << "TSP with Refueling B&B, cost= " << custo_solucao; ViewListDigraph(tsp.g, tsp.vname, tsp.posx, tsp.posy, tsp.vcolor, acolor, out.str()); } return true; } catch(GRBException e) { cerr << "Gurobi exception has been thrown." << endl; cerr << "Error code = " << e.getErrorCode() << endl; cerr << e.getMessage(); } catch (...) { cout << "Model is infeasible" << endl; return false; } return false; }
void BVH_balancing::Solve( const std::map<int, std::set<int> > &compatibility, size_t no_bones, std::map<int, int>& assignments ){ std::map<int, int> Bn_to_index, index_to_Bn; size_t index = 0; size_t no_branchings = compatibility.size(); assert( no_bones > 0 ); assert( no_branchings > 0 ); for( const auto& item : compatibility ){ if( Bn_to_index.count(item.first) > 0 ) { continue; } // skip if alredy mapped Bn_to_index[item.first] = index; index_to_Bn[index] = item.first; ++index; } // entire optimization goes inside a try catch statement. // in case of errors, I want it to crash! try{ GRBEnv env; GRBModel model = GRBModel( env ); /* CREATE VARIABLES */ std::vector<GRBVar> vars( no_bones * no_branchings ); for( size_t r = 0; r < no_bones; ++r ){ for( size_t c = 0; c < no_branchings; ++c ){ size_t idx = matIndex( r, c, no_branchings ); std::stringstream var_name; var_name << "A( " << r << ", " << c << " )"; assert( compatibility.count( index_to_Bn[c] ) > 0 ); bool is_connected = compatibility.at( index_to_Bn[c] ).count( r ); vars[idx] = model.addVar( 0.0, ( is_connected > 0 ? 1.0 : 0.0 ), 1.0, GRB_BINARY, var_name.str()); } } model.update(); // Create Objective Function. // for each branching node i : #bones( Bn_i )^2 // hence I need to sum the number of bones assigned to each branching node GRBQuadExpr obj; std::vector<GRBLinExpr> cols( no_branchings ); for( size_t c = 0; c < no_branchings; ++c ){ for( size_t r = 0; r < no_bones; ++r ){ size_t idx = matIndex( r, c, no_branchings ); cols[c] += vars[idx]; } } for( size_t c = 0; c < no_branchings; ++c ){ obj += cols[c] * cols[c]; } model.setObjective( obj ); model.update(); // create constraint : each bone can be assigned to only one branching node. // this means that the summation of each row's values must be equal to 1.0 for( size_t r = 0; r < no_bones; ++r ){ GRBLinExpr row_sum; for( size_t c = 0; c < no_branchings; ++c ){ size_t idx = matIndex( r, c, no_branchings ); row_sum += vars[idx]; } model.addConstr( row_sum == 1.0 ); } model.update(); // Optimize model.optimize(); int status = model.get(GRB_IntAttr_Status); if (status == GRB_OPTIMAL) { std::cout << "The optimal objective is " << model.get(GRB_DoubleAttr_ObjVal) << std::endl; // return results! // printResults(vars, no_bones, no_branchings ); getResults( index_to_Bn, vars, assignments, no_bones, no_branchings ); return; } /************************************/ /* ERROR HANDLING */ /************************************/ if (status == GRB_UNBOUNDED){ std::cout << "The model cannot be solved because it is unbounded" << std::endl; assert(false); } if ((status != GRB_INF_OR_UNBD) && (status != GRB_INFEASIBLE)) { std::cout << "Optimization was stopped with status " << status << std::endl; assert( false ); } GRBConstr* c = 0; // do IIS std::cout << "The model is infeasible; computing IIS" << std::endl; model.computeIIS(); std::cout << "\nThe following constraint(s) cannot be satisfied:" << std::endl; c = model.getConstrs(); for (int i = 0; i < model.get(GRB_IntAttr_NumConstrs); ++i){ if (c[i].get(GRB_IntAttr_IISConstr) == 1) { std::cout << c[i].get(GRB_StringAttr_ConstrName) << std::endl; } } } /* EXCEPTION HANDLING */ catch (GRBException e) { std::cout << "Error code = " << e.getErrorCode() << std::endl; std::cout << e.getMessage() << std::endl; assert( false ); } catch (...){ std::cout << "Exception during optimization" << std::endl; assert( false ); } }
std::map<std::string,int> ILP(char* argv) { map<string, int > results; GRBVar* vars = 0; // if (argc < 2) { // cout << "Usage: lp_c++ filename" << endl; // return 1; // } try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env, argv); //model.getEnv().set(GRB_DoubleParam_IntFeasTol,1e-6); model.getEnv().set(GRB_DoubleParam_Heuristics,0.95); model.getEnv().set(GRB_DoubleParam_TimeLimit,timeMax); model.getEnv().set(GRB_DoubleParam_MIPGap, ilpGap); vars = model.getVars(); model.optimize(); int optimstatus = model.get(GRB_IntAttr_Status); if (optimstatus == GRB_INF_OR_UNBD) { model.getEnv().set(GRB_IntParam_Presolve, 0); model.optimize(); optimstatus = model.get(GRB_IntAttr_Status); } if (optimstatus == GRB_OPTIMAL) { double objval = model.get(GRB_DoubleAttr_ObjVal); cout << "Optimal objective: " << objval << endl; for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X); // cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } ofstream varNames; ofstream varResults; varNames.open("varName.txt"); varResults.open("varResults.txt"); for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X)+0.5; // +0.5 to make sure its round if(varName == "co2o3storagey3" ){ cout << "gotte ya" <<endl; } varNames << varName << "\n"; varResults << varName << " "<< results[varName] << "\n"; //cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } varNames.close(); varResults.close(); cout << "size of results is " << sizeof(results)*results.size()<< endl; cout << "using mapSize function, size is " << mapSize(results)<<endl; toFile(results,mapSize(results)); } else if (optimstatus == GRB_INFEASIBLE) { cout << "Model is infeasible" << endl; // compute and write out IIS //model.computeIIS(); //model.write("model.ilp"); } else if (optimstatus == GRB_UNBOUNDED) { cout << "Model is unbounded" << endl; } else { cout << "Optimization was stopped with status = " << optimstatus << endl; double objval = model.get(GRB_DoubleAttr_ObjVal); cout << "Optimal objective: " << objval << endl; for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X); // cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } ofstream varNames; ofstream varResults; varNames.open("varName.txt"); varResults.open("varResults.txt"); for(int i =0; i<model.get(GRB_IntAttr_NumVars);i++){ string varName = vars[i].get(GRB_StringAttr_VarName); results[varName] = vars[i].get(GRB_DoubleAttr_X)+0.5; // +0.5 to make sure its round if(varName == "co2o3storagey3" ){ cout << "gotte ya" <<endl; } varNames << varName << "\n"; varResults << varName << " = "<< results[varName] << "\n"; //cout << vars[i].get(GRB_StringAttr_VarName) << " " << vars[i].get(GRB_DoubleAttr_X) << endl; } varNames.close(); varResults.close(); } } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } return results; //return 0; }
int main(int argc, char *argv[]) { GRBEnv* env = 0; GRBVar* covered = 0; // we want to maximize demand covered GRBVar* facility = 0; int max_n_Facilities = 10; try { const string filename = "/Users/katarzyna/Dropbox/matlab/2015-05 PKITS/aij_matrix.txt"; std::vector<std::vector<long int> > aij_matrix; readMatrix_aij(filename, aij_matrix); std::cout << "matrix size "<< aij_matrix.size() <<" x "<< aij_matrix[0].size() << std::endl; // Model env = new GRBEnv(); GRBModel model = GRBModel(*env); model.set(GRB_StringAttr_ModelName, "minimize number of facilities"); // Demand coverage decision variable, covered[i] == 1 if demand is covered covered = model.addVars(aij_matrix.size(), GRB_BINARY); model.update(); // Facility open variables: facility[j] == 1 if facility j is open. facility = model.addVars(aij_matrix.size(), GRB_BINARY); model.update(); // Set the objective to maximize the demand covered for (unsigned int i = 0; i < aij_matrix.size(); ++i) { ostringstream vname; vname << "Demand " << i; covered[i].set(GRB_DoubleAttr_Obj, 1); covered[i].set(GRB_StringAttr_VarName, vname.str()); } for (unsigned int i = 0; i < aij_matrix.size(); ++i) { ostringstream vname; vname << "Open " << i; facility[i].set(GRB_DoubleAttr_Obj, 0); facility[i].set(GRB_StringAttr_VarName, vname.str()); } // The objective is to maximize the total demand covered model.set(GRB_IntAttr_ModelSense, 0); // Update model model.update(); // Constraints // If demand i is covered by at least one station, then covered[i] == 1 for (unsigned int i = 0; i < aij_matrix.size(); ++i) { GRBLinExpr demand_is_covered = 0; for (unsigned int j = 0; j < aij_matrix.size(); ++j) { demand_is_covered += aij_matrix[i][j] * facility[j]; } ostringstream cname; cname << "Demand_i_is_covered " << i; model.addConstr(demand_is_covered >= covered[i], cname.str()); } // We allow no more than n facilities GRBLinExpr facilities_to_open = 0; for (unsigned int j = 0; j < aij_matrix.size(); ++j) { facilities_to_open += facility[j]; } ostringstream vname; vname << "Max facility... "; model.addConstr(facilities_to_open <= max_n_Facilities); model.update(); // Solve model.optimize(); model.write("/Users/katarzyna/Dropbox/matlab/2015-05 PKITS/output.lp"); // Print solution cout << "\nTOTAL COSTS: " << model.get(GRB_DoubleAttr_ObjVal) << endl; cout << "SOLUTION:" << endl; int sum_d = 0; for (unsigned int i = 0; i < aij_matrix.size(); ++i) { if (covered[i].get(GRB_DoubleAttr_X) == 1.0) { //cout << "Demand " << i << " covered." << endl; sum_d += 1; } } int sum_f = 0; for (unsigned int i = 0; i < aij_matrix.size(); ++i) { if (facility[i].get(GRB_DoubleAttr_X) == 1.0) { cout << "Facility " << i << " is opened." << endl; sum_f += 1; } } cout << sum_f << " facilities is open." << endl; cout << sum_d << " customers is covered." << endl; } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Exception during optimization" << endl; } }
int main(int argc, char *argv[]) { Digraph g; // graph declaration string digraph_matching_filename; DiNodeName vname(g); // name of graph nodes Digraph::NodeMap<double> px(g),py(g); // xy-coodinates for each node Digraph::NodeMap<int> vcolor(g);// color of nodes Digraph::ArcMap<int> ecolor(g); // color of edges ArcWeight lpvar(g); // used to obtain the contents of the LP variables ArcWeight weight(g); // edge weights srand48(1); // uncomment one of these lines to change default pdf reader, or insert new one //set_pdfreader("open"); // pdf reader for Mac OS X //set_pdfreader("xpdf"); // pdf reader for Linux //set_pdfreader("evince"); // pdf reader for Linux // double cutoff; // used to prune non promissing branches (of the B&B tree) if (argc!=2) { cout<<endl<<"Usage: "<< argv[0]<<" <digraph_matching_filename>"<<endl<<endl; cout << "Example: " << argv[0] << " digr_bipartite_100_10" << endl << endl; exit(0);} digraph_matching_filename = argv[1]; ReadListDigraph(digraph_matching_filename,g,vname,weight,px,py,0); //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.set(GRB_IntAttr_ModelSense, GRB_MAXIMIZE); // is a maximization problem /* LPI variables */ Digraph::ArcMap<GRBVar> x(g); // variable for connections, 1=connected, 0=not connected GRBLinExpr expressao; for (Digraph::ArcIt e(g); e != INVALID; ++e) { x[e] = model.addVar(0.0, 1.0, weight[e], GRB_CONTINUOUS); // Exercise: Using bipartite graphs, explain why we can use continuous // variables and still obtain integer solutions } model.update(); for (Digraph::NodeIt v(g); v!=INVALID; ++v) { GRBLinExpr exprin, exprout; int n_arcs_in=0,n_arcs_out=0; // for each node, the number of arcs leaving is at most 1 // remember: the graph is bipartite, with arcs going from one part to the other for (Digraph::InArcIt e(g,v); e != INVALID; ++e) {exprin += x[e]; n_arcs_in++;} if (n_arcs_in > 0) {model.addConstr(exprin <= 1 ); vcolor[v] = BLUE;} // for each node, the number of arcs entering is at most 1 for (Digraph::OutArcIt e(g,v); e != INVALID; ++e) {exprout += x[e]; n_arcs_out++;} if (n_arcs_out > 0) {model.addConstr(exprout <= 1 ); vcolor[v] = RED;} } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% try { model.optimize(); double soma=0.0; int cor=0; for (Digraph::ArcIt e(g); e!=INVALID; ++e) { lpvar[e] = x[e].get(GRB_DoubleAttr_X); if (lpvar[e] > 1.0 - EPS) { soma += weight[e]; ecolor[e] = (cor % 8) + 2; cor++; } else ecolor[e] = NOCOLOR; } cout << "Maximum Bipartite Matching = " << soma << endl; // Esta rotina precisa do programa neato/dot do Graphviz ViewListDigraph(g,vname,px,py,vcolor,ecolor, "maximum weighted matching in graph with "+IntToString(countNodes(g))+ " nodes:"+DoubleToString(soma)); } catch(GRBException e) { cerr << "Nao foi possivel resolver o PLI." << endl; cerr << "Codigo de erro = " << e.getErrorCode() << endl; cerr << e.getMessage(); } return 0; }
int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: feasopt_c++ filename" << endl; return 1; } GRBEnv* env = 0; GRBConstr* c = 0; try { env = new GRBEnv(); GRBModel feasmodel = GRBModel(*env, argv[1]); // Create a copy to use FeasRelax feature later */ GRBModel feasmodel1 = GRBModel(feasmodel); // clear objective feasmodel.setObjective(GRBLinExpr(0.0)); // add slack variables c = feasmodel.getConstrs(); for (int i = 0; i < feasmodel.get(GRB_IntAttr_NumConstrs); ++i) { char sense = c[i].get(GRB_CharAttr_Sense); if (sense != '>') { double coef = -1.0; feasmodel.addVar(0.0, GRB_INFINITY, 1.0, GRB_CONTINUOUS, 1, &c[i], &coef, "ArtN_" + c[i].get(GRB_StringAttr_ConstrName)); } if (sense != '<') { double coef = 1.0; feasmodel.addVar(0.0, GRB_INFINITY, 1.0, GRB_CONTINUOUS, 1, &c[i], &coef, "ArtP_" + c[i].get(GRB_StringAttr_ConstrName)); } } feasmodel.update(); // optimize modified model feasmodel.write("feasopt.lp"); feasmodel.optimize(); // use FeasRelax feature */ feasmodel1.feasRelax(GRB_FEASRELAX_LINEAR, true, false, true); feasmodel1.write("feasopt1.lp"); feasmodel1.optimize(); } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] c; delete env; return 0; }
vector <double> solve2(vector <vector <double> > M) { vector <double> solution; try { GRBEnv env = GRBEnv(); vector <vector <double> > N = M; int numPoints = M.size(); int numCircles = M.back().size(); //cout<<"solving for "<<numCircles<<" circles and "<<numPoints<<" points"<<endl; GRBVar p[numPoints]; double coeff[numPoints]; GRBModel model = GRBModel(env); GRBLinExpr expr; // Create variables for (int i=0;i<numPoints;i++){ p[i] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY); coeff[i] = 1.0; } expr.addTerms(coeff, p,numPoints); // Integrate new variables model.update(); // Set objective: maximize x + y + 2 z model.setObjective(expr, GRB_MINIMIZE); for(int i=0;i<numCircles;i++){ GRBLinExpr cexpr; double ccoeff[numPoints]; for(int j=0;j<numPoints;j++){ ccoeff[j] = M[j].back(); M[j].pop_back(); } cexpr.addTerms(ccoeff,p,numPoints); model.addConstr(cexpr, GRB_GREATER_EQUAL,1.0); } // Optimize model model.optimize(); int c=0; for (int i=0;i<numPoints;i++){ solution.push_back((double)p[i].get(GRB_DoubleAttr_X)); c += solution.back(); } model.addConstr(expr, GRB_EQUAL, c); int temp; int temp2; for (int i=0;i<numPoints;i++){ temp=0; while(N.back().size()){ temp = temp + (N.back()).back(); N.back().pop_back(); } coeff[i] = temp; N.pop_back(); } expr.addTerms(coeff, p,numPoints); if (rand()%2) model.setObjective(expr, GRB_MINIMIZE); else model.setObjective(expr, GRB_MAXIMIZE); model.optimize(); solution.clear(); c=0; for (int i=0;i<numPoints;i++){ solution.push_back((double)p[i].get(GRB_DoubleAttr_X)); c += solution.back(); } } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; // cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return solution; }
int main(){//int argc,char** argv){ int nbPOI= -1; int maxT= -1; //int dt= 1; cin>>nbPOI>> maxT; vector<double> reward (nbPOI, 0); vector<double> time (nbPOI, 0); vector< vector<double > > dist (nbPOI, vector<double>(nbPOI)); for(int i =0; i<nbPOI; ++i){ cin>> reward[i]; } for(int i =0; i<nbPOI; ++i){ cin>> time[i]; } for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ cin>> dist[i][j]; } } try { GRBEnv env = GRBEnv(); GRBModel* model = new GRBModel(env); vector< vector<GRBVar> > x(nbPOI, vector<GRBVar>(nbPOI)); vector< GRBVar > ordre(nbPOI); for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ x[i][j]=model->addVar(0.0, 1.0, 0.0, GRB_BINARY, "x_"+to_string(i)+to_string(j)); } } for(int i =0; i<nbPOI; ++i){ ordre[i]=model->addVar(0.0, GRB_INFINITY, 0.0, GRB_INTEGER, "o_"+to_string(i)); } model->update(); int constr_count=0; for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ //sum of entering flow = sum entering flow GRBLinExpr expr1 = 0; GRBLinExpr expr2 = 0; for(int k=0; k<nbPOI; ++k){ expr1+= x[k][i]; expr2+=x[i][k]; } model->addConstr(expr1-expr2, GRB_EQUAL, 0,"c_"+to_string(constr_count++)); if(i==0) model->addConstr(expr1, GRB_EQUAL, 1,"c_"+to_string(constr_count++)); else model->addConstr(expr1, GRB_LESS_EQUAL, 1,"c_"+to_string(constr_count++)); } } for(int i =0; i<nbPOI; ++i){ for(int j =1; j<nbPOI; ++j){ //order constraint GRBLinExpr expr = 0; expr+= ordre[i]-ordre[j]+1-nbPOI*(1-x[i][j]); model->addConstr(expr, GRB_LESS_EQUAL, 0,"c_"+to_string(constr_count++)); } } //Time constraint GRBLinExpr total_time = 0; for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ total_time+= (dist[i][j]+time[j])*x[i][j]; } } model->addConstr(total_time, GRB_LESS_EQUAL, maxT,"Time"); //objective GRBLinExpr total_reward = 0; for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ total_reward+= reward[j]*x[i][j]; } } model->setObjective(total_reward, GRB_MAXIMIZE); model->update(); model->optimize(); int verbose=1; if(verbose>1){ for(int i =0; i<nbPOI; ++i){ for(int j =0; j<nbPOI; ++j){ cout<<x[i][j].get(GRB_StringAttr_VarName) << ":"<<x[i][j].get(GRB_DoubleAttr_X)<<endl; } } for(int j =0; j<nbPOI; ++j){ cout<<ordre[j].get(GRB_StringAttr_VarName) << ":"<<ordre[j].get(GRB_DoubleAttr_X)<<endl; } } //extract the path vector<int> path; bool found=0; int current=0; int final=0; bool finish=0; while(!finish){ for(int j=0; j<(int)x[0].size();++j){ if(x[current][j].get(GRB_DoubleAttr_X)>0){ // cout<<"on node"<<current<<" "<<j<<endl; path.push_back(current); // x[current][j]=0; current=j; if(current== final) finish=1; found=1; break; } } if(!found){ cerr<<"Nothing interesting to do, going directly to final node"<<endl; path.push_back(current); current=final; finish=1; } }
int main(int argc, char *argv[]) { GRBEnv* env = 0; GRBVar* open = 0; GRBVar** transport = 0; int transportCt = 0; try { // Number of plants and warehouses const int nPlants = 5; const int nWarehouses = 4; // Warehouse demand in thousands of units double Demand[] = { 15, 18, 14, 20 }; // Plant capacity in thousands of units double Capacity[] = { 20, 22, 17, 19, 18 }; // Fixed costs for each plant double FixedCosts[] = { 12000, 15000, 17000, 13000, 16000 }; // Transportation costs per thousand units double TransCosts[][nPlants] = { { 4000, 2000, 3000, 2500, 4500 }, { 2500, 2600, 3400, 3000, 4000 }, { 1200, 1800, 2600, 4100, 3000 }, { 2200, 2600, 3100, 3700, 3200 } }; // Model env = new GRBEnv(); GRBModel model = GRBModel(*env); model.set(GRB_StringAttr_ModelName, "facility"); // Plant open decision variables: open[p] == 1 if plant p is open. open = model.addVars(nPlants, GRB_BINARY); model.update(); int p; for (p = 0; p < nPlants; ++p) { ostringstream vname; vname << "Open" << p; open[p].set(GRB_DoubleAttr_Obj, FixedCosts[p]); open[p].set(GRB_StringAttr_VarName, vname.str()); } // Transportation decision variables: how much to transport from a plant p to a warehouse w transport = new GRBVar* [nWarehouses]; int w; for (w = 0; w < nWarehouses; ++w) { transport[w] = model.addVars(nPlants); transportCt++; model.update(); for (p = 0; p < nPlants; ++p) { ostringstream vname; vname << "Trans" << p << "." << w; transport[w][p].set(GRB_DoubleAttr_Obj, TransCosts[w][p]); transport[w][p].set(GRB_StringAttr_VarName, vname.str()); } } // The objective is to minimize the total fixed and variable costs model.set(GRB_IntAttr_ModelSense, 1); // Update model to integrate new variables model.update(); // Production constraints // Note that the right-hand limit sets the production to zero if // the plant is closed for (p = 0; p < nPlants; ++p) { GRBLinExpr ptot = 0; for (w = 0; w < nWarehouses; ++w) { ptot += transport[w][p]; } ostringstream cname; cname << "Capacity" << p; model.addConstr(ptot <= Capacity[p] * open[p], cname.str()); } // Demand constraints for (w = 0; w < nWarehouses; ++w) { GRBLinExpr dtot = 0; for (p = 0; p < nPlants; ++p) { dtot += transport[w][p]; } ostringstream cname; cname << "Demand" << w; model.addConstr(dtot == Demand[w], cname.str()); } // Guess at the starting point: close the plant with the highest // fixed costs; open all others // First, open all plants for (p = 0; p < nPlants; ++p) { open[p].set(GRB_DoubleAttr_Start, 1.0); } // Now close the plant with the highest fixed cost cout << "Initial guess:" << endl; double maxFixed = -GRB_INFINITY; for (p = 0; p < nPlants; ++p) { if (FixedCosts[p] > maxFixed) { maxFixed = FixedCosts[p]; } } for (p = 0; p < nPlants; ++p) { if (FixedCosts[p] == maxFixed) { open[p].set(GRB_DoubleAttr_Start, 0.0); cout << "Closing plant " << p << endl << endl; break; } } // Use barrier to solve root relaxation model.getEnv().set(GRB_IntParam_Method, GRB_METHOD_BARRIER); // Solve model.optimize(); // Print solution cout << "\nTOTAL COSTS: " << model.get(GRB_DoubleAttr_ObjVal) << endl; cout << "SOLUTION:" << endl; for (p = 0; p < nPlants; ++p) { if (open[p].get(GRB_DoubleAttr_X) == 1.0) { cout << "Plant " << p << " open:" << endl; for (w = 0; w < nWarehouses; ++w) { if (transport[w][p].get(GRB_DoubleAttr_X) > 0.0001) { cout << " Transport " << transport[w][p].get(GRB_DoubleAttr_X) << " units to warehouse " << w << endl; } } } else { cout << "Plant " << p << " closed!" << endl; } } } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Exception during optimization" << endl; } delete[] open; for (int i = 0; i < transportCt; ++i) { delete[] transport[i]; } delete[] transport; delete env; return 0; }
int main(int argc, char *argv[]) { GRBEnv* env = 0; GRBConstr* c = 0; GRBVar* v = 0; GRBVar** x = 0; GRBVar* slacks = 0; GRBVar* totShifts = 0; GRBVar* diffShifts = 0; int xCt = 0; try { // Sample data const int nShifts = 14; const int nWorkers = 7; // Sets of days and workers string Shifts[] = { "Mon1", "Tue2", "Wed3", "Thu4", "Fri5", "Sat6", "Sun7", "Mon8", "Tue9", "Wed10", "Thu11", "Fri12", "Sat13", "Sun14" }; string Workers[] = { "Amy", "Bob", "Cathy", "Dan", "Ed", "Fred", "Gu" }; // Number of workers required for each shift double shiftRequirements[] = { 3, 2, 4, 4, 5, 6, 5, 2, 2, 3, 4, 6, 7, 5 }; // Worker availability: 0 if the worker is unavailable for a shift double availability[][nShifts] = { { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1 }, { 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1 }, { 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; // Model env = new GRBEnv(); GRBModel model = GRBModel(*env); model.set(GRB_StringAttr_ModelName, "assignment"); // Assignment variables: x[w][s] == 1 if worker w is assigned // to shift s. This is no longer a pure assignment model, so we must // use binary variables. x = new GRBVar*[nWorkers]; for (int w = 0; w < nWorkers; ++w) { x[w] = model.addVars(nShifts); xCt++; model.update(); for (int s = 0; s < nShifts; ++s) { ostringstream vname; vname << Workers[w] << "." << Shifts[s]; x[w][s].set(GRB_DoubleAttr_UB, availability[w][s]); x[w][s].set(GRB_CharAttr_VType, GRB_BINARY); x[w][s].set(GRB_StringAttr_VarName, vname.str()); } } // Slack variables for each shift constraint so that the shifts can // be satisfied slacks = model.addVars(nShifts); model.update(); for (int s = 0; s < nShifts; ++s) { ostringstream vname; vname << Shifts[s] << "Slack"; slacks[s].set(GRB_StringAttr_VarName, vname.str()); } // Variable to represent the total slack GRBVar totSlack = model.addVar(0, GRB_INFINITY, 0, GRB_CONTINUOUS, "totSlack"); // Variables to count the total shifts worked by each worker totShifts = model.addVars(nWorkers); model.update(); for (int w = 0; w < nWorkers; ++w) { ostringstream vname; vname << Workers[w] << "TotShifts"; totShifts[w].set(GRB_StringAttr_VarName, vname.str()); } // Update model to integrate new variables model.update(); GRBLinExpr lhs; // Constraint: assign exactly shiftRequirements[s] workers // to each shift s for (int s = 0; s < nShifts; ++s) { lhs = 0; lhs += slacks[s]; for (int w = 0; w < nWorkers; ++w) { lhs += x[w][s]; } model.addConstr(lhs == shiftRequirements[s], Shifts[s]); } // Constraint: set totSlack equal to the total slack lhs = 0; for (int s = 0; s < nShifts; ++s) { lhs += slacks[s]; } model.addConstr(lhs == totSlack, "totSlack"); // Constraint: compute the total number of shifts for each worker for (int w = 0; w < nWorkers; ++w) { lhs = 0; for (int s = 0; s < nShifts; ++s) { lhs += x[w][s]; } ostringstream vname; vname << "totShifts" << Workers[w]; model.addConstr(lhs == totShifts[w], vname.str()); } // Objective: minimize the total slack GRBLinExpr obj = 0; obj += totSlack; model.setObjective(obj); // Optimize int status = solveAndPrint(model, totSlack, nWorkers, Workers, totShifts); if (status != GRB_OPTIMAL) { return 1; } // Constrain the slack by setting its upper and lower bounds totSlack.set(GRB_DoubleAttr_UB, totSlack.get(GRB_DoubleAttr_X)); totSlack.set(GRB_DoubleAttr_LB, totSlack.get(GRB_DoubleAttr_X)); // Variable to count the average number of shifts worked GRBVar avgShifts = model.addVar(0, GRB_INFINITY, 0, GRB_CONTINUOUS, "avgShifts"); // Variables to count the difference from average for each worker; // note that these variables can take negative values. diffShifts = model.addVars(nWorkers); model.update(); for (int w = 0; w < nWorkers; ++w) { ostringstream vname; vname << Workers[w] << "Diff"; diffShifts[w].set(GRB_StringAttr_VarName, vname.str()); diffShifts[w].set(GRB_DoubleAttr_LB, -GRB_INFINITY); } // Update model to integrate new variables model.update(); // Constraint: compute the average number of shifts worked lhs = 0; for (int w = 0; w < nWorkers; ++w) { lhs += totShifts[w]; } model.addConstr(lhs == nWorkers * avgShifts, "avgShifts"); // Constraint: compute the difference from the average number of shifts for (int w = 0; w < nWorkers; ++w) { lhs = 0; lhs += totShifts[w]; lhs -= avgShifts; ostringstream vname; vname << Workers[w] << "Diff"; model.addConstr(lhs == diffShifts[w], vname.str()); } // Objective: minimize the sum of the square of the difference from the // average number of shifts worked GRBQuadExpr qobj; for (int w = 0; w < nWorkers; ++w) { qobj += diffShifts[w] * diffShifts[w]; } model.setObjective(qobj); // Optimize status = solveAndPrint(model, totSlack, nWorkers, Workers, totShifts); if (status != GRB_OPTIMAL) { return 1; } } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Exception during optimization" << endl; } delete[] c; delete[] v; for (int i = 0; i < xCt; ++i) { delete[] x[i]; } delete[] x; delete[] slacks; delete[] totShifts; delete[] diffShifts; delete env; return 0; }
int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: tsp_c++ filename" << endl; return 1; } int n = atoi(argv[1]); double* x = new double[n]; double* y = new double[n]; for (int i = 0; i < n; i++) { x[i] = ((double) rand())/RAND_MAX; y[i] = ((double) rand())/RAND_MAX; } GRBEnv *env = NULL; GRBVar **vars = new GRBVar*[n]; try { int i, j; env = new GRBEnv(); GRBModel model = GRBModel(*env); // Must disable dual reductions when using lazy constraints model.getEnv().set(GRB_IntParam_DualReductions, 0); // Create binary decision variables for (i = 0; i < n; i++) vars[i] = model.addVars(n); model.update(); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { vars[i][j].set(GRB_CharAttr_VType, GRB_BINARY); vars[i][j].set(GRB_DoubleAttr_Obj, distance(x, y, i, j)); vars[i][j].set(GRB_StringAttr_VarName, "x_"+itos(i)+"_"+itos(j)); } } // Integrate new variables model.update(); // Degree-2 constraints for (i = 0; i < n; i++) { GRBLinExpr expr = 0; for (j = 0; j < n; j++) expr += vars[i][j]; model.addConstr(expr == 2, "deg2_"+itos(i)); } // Forbid edge from node back to itself for (i = 0; i < n; i++) vars[i][i].set(GRB_DoubleAttr_UB, 0); // Symmetric TSP for (i = 0; i < n; i++) for (j = 0; j < i; j++) model.addConstr(vars[i][j] == vars[j][i]); // Set callback function subtourelim cb = subtourelim(vars, n); model.setCallback(&cb); // Optimize model model.optimize(); // Extract solution if (model.get(GRB_IntAttr_SolCount) > 0) { double **sol = new double*[n]; for (i = 0; i < n; i++) sol[i] = model.get(GRB_DoubleAttr_X, vars[i], n); int* tour = new int[n]; int len; findsubtour(n, sol, &len, tour); cout << "Tour: "; for (i = 0; i < len; i++) cout << tour[i] << " "; cout << endl; for (i = 0; i < n; i++) delete[] sol[i]; delete[] sol; delete[] tour; } } catch (GRBException e) { cout << "Error number: " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } for (int i = 0; i < n; i++) delete[] vars[i]; delete[] vars; delete[] x; delete[] y; delete env; return 0; }
int main(int argc, char *argv[]) { try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); GRBVar vars[n][n][n]; int i, j, v; // Create 3-D array of model variables for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { for (v = 0; v < n; v++) { string s = "G_" + itos(i) + "_" + itos(j) + "_" + itos(v); vars[i][j][v] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, s); } } } // Integrate variables into model model.update(); // Add constraints // Each cell must take one value for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { GRBLinExpr expr = 0; for (v = 0; v < n; v++) expr += vars[i][j][v]; string s = "V_" + itos(i) + "_" + itos(j); model.addConstr(expr, GRB_EQUAL, 1.0, s); } } // Each value appears once per row for (i = 0; i < n; i++) { for (v = 0; v < n; v++) { GRBLinExpr expr = 0; for (j = 0; j < n; j++) expr += vars[i][j][v]; string s = "R_" + itos(i) + "_" + itos(v); model.addConstr(expr == 1.0, s); } } // Each value appears once per column for (j = 0; j < n; j++) { for (v = 0; v < n; v++) { GRBLinExpr expr = 0; for (i = 0; i < n; i++) expr += vars[i][j][v]; string s = "C_" + itos(j) + "_" + itos(v); model.addConstr(expr == 1.0, s); } } // Each value appears once per sub-grid for (v = 0; v < n; v++) { for (int i0 = 0; i0 < sd; i0++) { for (int j0 = 0; j0 < sd; j0++) { GRBLinExpr expr = 0; for (int i1 = 0; i1 < sd; i1++) { for (int j1 = 0; j1 < sd; j1++) { expr += vars[i0*sd+i1][j0*sd+j1][v]; } } string s = "Sub_" + itos(v) + "_" + itos(i0) + "_" + itos(j0); model.addConstr(expr == 1.0, s); } } } // Fix variables associated with pre-specified cells char input[10]; for (i = 0; i < n; i++) { cin >> input; for (j = 0; j < n; j++) { int val = (int) input[j] - 48 - 1; // 0-based if (val >= 0) vars[i][j][val].set(GRB_DoubleAttr_LB, 1.0); } } // Optimize model model.optimize(); // Write model to file model.write("sudoku.lp"); cout << endl; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { for (v = 0; v < n; v++) { if (vars[i][j][v].get(GRB_DoubleAttr_X) > 0.5) cout << v+1; } } cout << endl; } cout << endl; } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } return 0; }
// Start the optimization SolverResult SolverGurobi::runOptimizer() { if (!getInitialized()) initialize(); try { // Create Gurobi environment and set parameters GRBEnv env = GRBEnv(); env.set(GRB_IntParam_OutputFlag, 0); GRBModel model = GRBModel(env); // Get problem info int numVars = constraints->getNumVariables(); int numConstraints = constraints->getNumConstraints(); // Get variables auto variables = constraints->getVariables(); // Create array of model variables GRBVar vars[numVars]; for (int i = 0; i < numVars; i++) { //vars[i] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY); // Set variable type char type = GRB_CONTINUOUS; if (variables.at(i)->getType() == VariableType::BINARY) { type = GRB_BINARY; } else if (variables.at(i)->getType() == VariableType::INTEGER) { type = GRB_INTEGER; } vars[i] = model.addVar(variables.at(i)->getLowerBound(), variables.at(i)->getUpperBound(), variables.at(i)->getCost(), type); } // Integrate variables into model model.update(); // Set starting points (does not help much...) for (int i = 0; i < numVars; i++) vars[i].set(GRB_DoubleAttr_Start, variables.at(i)->getValue()); /* * Add constraints Ax <= b (or Ax = b) * by evaluating gradient and build A matrix */ DenseVector x = DenseVector::Zero(numVars); DenseVector dx = constraints->evalJacobian(x); // Get constraint bounds std::vector<double> clb; std::vector<double> cub; constraints->getConstraintBounds(clb,cub); std::vector<int> rowGradient, colGradient; constraints->structureJacobian(rowGradient, colGradient); int nnzJacobian = constraints->getNumNonZerosJacobian(); // Add constraints one row at the time for (int row = 0; row < numConstraints; row++) { // Build constraint GRBLinExpr expr = 0; // Loop through all non-zeros (inefficient) for (int i = 0; i < nnzJacobian; i++) { if (rowGradient.at(i) == row) { int j = colGradient.at(i); expr += dx(i)*vars[j]; } } // Add constraint to model if (clb.at(row) == cub.at(row)) { model.addConstr(expr, GRB_EQUAL, cub.at(row)); } else { model.addConstr(expr, GRB_LESS_EQUAL, cub.at(row)); } } // More efficient method - avoids dense matrix // std::vector<int> rows = {1,1,1,2,2,3,4,4,4,4,4,5}; // std::vector<int>::iterator start,stop; // start = rows.begin(); // stop = start; // while (start != rows.end()) // { // while (stop != rows.end()) // { // if (*stop == *start) // ++stop; // else // break; // } // for (std::vector<int>::iterator it = start; it != stop; ++it) // cout << *it << endl; // start = stop; // } model.update(); assert(numVars == model.get(GRB_IntAttr_NumVars)); assert(numConstraints == model.get(GRB_IntAttr_NumConstrs)); // Optimize model model.optimize(); // Check status int optimstatus = model.get(GRB_IntAttr_Status); if (optimstatus == GRB_INF_OR_UNBD) { model.getEnv().set(GRB_IntParam_Presolve, 0); model.optimize(); optimstatus = model.get(GRB_IntAttr_Status); } // Create result object SolverResult result(SolverStatus::ERROR, INF, std::vector<double>(numVars,0)); // Check Gurobi status if (optimstatus == GRB_OPTIMAL) { result.status = SolverStatus::OPTIMAL; // Get solution info result.objectiveValue = model.get(GRB_DoubleAttr_ObjVal); std::vector<double> optimalSolution; for (int i = 0; i < numVars; i++) { optimalSolution.push_back(vars[i].get(GRB_DoubleAttr_X)); } result.primalVariables = optimalSolution; /* * Reduced costs and constraint duals are * only available for continuous models */ std::vector<double> reducedCosts; std::vector<double> constraintDuals; if (!model.get(GRB_IntAttr_IsMIP)) { for (int i = 0; i < numVars; i++) { // Get reduced costs (related to range constraint duals) reducedCosts.push_back(vars[i].get(GRB_DoubleAttr_RC)); } for (int i = 0; i < numConstraints; i++) { GRBConstr c = model.getConstr(i); double pi = c.get(GRB_DoubleAttr_Pi); constraintDuals.push_back(pi); } } result.lowerBoundDualVariables = reducedCosts; result.upperBoundDualVariables = reducedCosts; result.constraintDualVariables = constraintDuals; return result; } else if (optimstatus == GRB_INFEASIBLE) { result.status = SolverStatus::INFEASIBLE; result.objectiveValue = INF; // compute and write out IIS // model.computeIIS(); // model.write("problem.lp"); return result; } else if (optimstatus == GRB_UNBOUNDED) { result.status = SolverStatus::UNBOUNDED; result.objectiveValue = -INF; return result; } else { result.status = SolverStatus::ERROR; result.objectiveValue = INF; return result; } } catch(GRBException e) { cout << "SolverGurobi: Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; return SolverResult(SolverStatus::ERROR, INF, std::vector<double>(constraints->getNumVariables(),0)); } catch (...) { cout << "SolverGurobi: Error during optimization!" << endl; return SolverResult(SolverStatus::ERROR, INF, std::vector<double>(constraints->getNumVariables(),0)); } }
void solve(const Instance &inst, bool print_inst = false, bool pyout = false) { Arcflow afg(inst); char vtype = inst.vtype; GRBEnv* env = new GRBEnv(); GRBModel model = GRBModel(*env); model.set(GRB_StringAttr_ModelName, "flow"); model.getEnv().set(GRB_IntParam_OutputFlag, 1); model.getEnv().set(GRB_IntParam_Threads, 1); model.getEnv().set(GRB_IntParam_Presolve, 1); // model.getEnv().set(GRB_IntParam_Method, 0); model.getEnv().set(GRB_IntParam_Method, 2); model.getEnv().set(GRB_IntParam_MIPFocus, 1); // model.getEnv().set(GRB_IntParam_RINS, 1); model.getEnv().set(GRB_DoubleParam_Heuristics, 1); model.getEnv().set(GRB_DoubleParam_MIPGap, 0); model.getEnv().set(GRB_DoubleParam_MIPGapAbs, 1-1e-5); // model.getEnv().set(GRB_DoubleParam_ImproveStartTime, 60); // model.getEnv().set(GRB_DoubleParam_ImproveStartGap, 1); vector<Arc> As(afg.A); sort(all(As)); map<Arc, GRBVar> va; int lastv = afg.Ts[0]-1; for (int i = 0; i < inst.nbtypes; i++) { lastv = min(lastv, afg.Ts[i]-1); } for (int i = 0; i < 3; i++) { for (const Arc &a : As) { if (i == 1 && a.u != afg.S) { continue; } if (i == 2 && a.v <= lastv) { continue; } if (i == 0 && (a.u == afg.S || a.v > lastv)) { continue; } if (a.label == afg.LOSS || inst.relax_domains) { va[a] = model.addVar( 0.0, inst.n, 0, vtype); } else { va[a] = model.addVar( 0.0, inst.items[a.label].demand, 0, vtype); } } } model.update(); for (int i = 0; i < inst.nbtypes; i++) { GRBVar &feedback = va[Arc(afg.Ts[i], afg.S, afg.LOSS)]; feedback.set(GRB_DoubleAttr_Obj, inst.Cs[i]); if (inst.Qs[i] >= 0) { feedback.set(GRB_DoubleAttr_UB, inst.Qs[i]); } } vector<vector<Arc>> Al(inst.nsizes); vector<vector<Arc>> in(afg.NV); vector<vector<Arc>> out(afg.NV); for (const Arc &a : As) { if (a.label != afg.LOSS) { Al[a.label].push_back(a); } out[a.u].push_back(a); in[a.v].push_back(a); } for (int i = 0; i < inst.m; i++) { GRBLinExpr lin = 0; for (int it = 0; it < inst.nsizes; it++) { if (inst.items[it].type == i) { for (const Arc &a : Al[it]) { lin += va[a]; } } } if (inst.ctypes[i] == '>' || inst.relax_domains) { model.addConstr(lin >= inst.demands[i]); } else { model.addConstr(lin == inst.demands[i]); } } for (int u = 0; u < afg.NV; u++) { GRBLinExpr lin = 0; for (const Arc &a : in[u]) { lin += va[a]; } for (const Arc &a : out[u]) { lin -= va[a]; } model.addConstr(lin == 0); } Al.clear(); in.clear(); out.clear(); double pre = TIMEDIF(afg.tstart); model.optimize(); printf("Preprocessing time: %.2f seconds\n", pre); double tg = model.get(GRB_DoubleAttr_Runtime); printf("Gurobi run time: %.2f seconds\n", tg); printf("Total run time: %.2f seconds\n", tg+pre); if (inst.vtype == 'I') { map<Arc, int> flow; for (const auto &a : va) { double x = a.second.get(GRB_DoubleAttr_X); int rx = static_cast<int>(round(x)); assert(x - rx <= EPS); if (rx > 0) { int u = a.first.u; int v = a.first.v; int lbl = a.first.label; Arc a(u, v, lbl); flow[a] = rx; } } ArcflowSol solution(inst, flow, afg.S, afg.Ts, afg.LOSS, true); solution.print_solution(print_inst, pyout); } free(env); }
void solve(Instance inst, bool hsol = false){ clock_t t0 = CURTIME; ArcflowMKP graph(inst); const vector<Item> &items = inst.items; GRBEnv* env = new GRBEnv(); GRBModel master = GRBModel(*env); master.set(GRB_StringAttr_ModelName, "GG"); master.getEnv().set(GRB_IntParam_OutputFlag, 0); master.getEnv().set(GRB_IntParam_Threads, 1); master.getEnv().set(GRB_IntParam_Method, 0); GRBConstr rows[inst.m]; for(int i = 0; i < inst.m; i++){ GRBLinExpr lin = 0; rows[i] = master.addConstr(lin >= items[i].demand); } master.update(); vector<GRBVar> vars; for(int i = 0; i < inst.m; i++){ GRBColumn col = GRBColumn(); col.addTerm(1, rows[i]); vars.push_back(master.addVar(0, GRB_INFINITY, 1, GRB_CONTINUOUS, col)); } printf("m: %d\n", inst.m); vector<double> values(inst.m); for(int itr = inst.m; ; itr++){ master.optimize(); printf("%d: %.6f (%.2fs)\n", itr, master.get(GRB_DoubleAttr_ObjVal), TIMEDIF(t0)); for(int i = 0; i < inst.m; i++) values[i] = rows[i].get(GRB_DoubleAttr_Pi); vector<int_pair> sol = graph.knapsack(values, 1+EPSILON); if(sol.empty()) break; GRBColumn col = GRBColumn(); ForEach(itr, sol) col.addTerm(itr->second, rows[itr->first]); vars.push_back(master.addVar(0, GRB_INFINITY, 1, GRB_CONTINUOUS, col)); master.update(); } printf("zlp: %.6f\n", master.get(GRB_DoubleAttr_ObjVal)); printf("nvars: %d\n", master.get(GRB_IntAttr_NumVars)); printf("time: %.2fs\n", TIMEDIF(t0)); if(hsol){ // find an heuristic solution if hsol = true ForEach(itr, vars) itr->set(GRB_CharAttr_VType, GRB_INTEGER); master.getEnv().set(GRB_IntParam_OutputFlag, 1); master.getEnv().set(GRB_IntParam_Threads, 1); //master.getEnv().set(GRB_IntParam_Presolve, 1); //master.getEnv().set(GRB_IntParam_Method, 2); master.getEnv().set(GRB_IntParam_MIPFocus, 1); master.getEnv().set(GRB_DoubleParam_Heuristics, 1); master.getEnv().set(GRB_DoubleParam_MIPGap, 0); master.getEnv().set(GRB_DoubleParam_MIPGapAbs, 1-1e-5); master.optimize(); printf("Total run time: %.2f seconds\n", TIMEDIF(t0)); } free(env); }
int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: lpmod_c++ filename" << endl; return 1; } GRBEnv* env = 0; GRBVar* v = 0; try { // Read model and determine whether it is an LP env = new GRBEnv(); GRBModel model = GRBModel(*env, argv[1]); if (model.get(GRB_IntAttr_IsMIP) != 0) { cout << "The model is not a linear program" << endl; return 1; } model.optimize(); int status = model.get(GRB_IntAttr_Status); if ((status == GRB_INF_OR_UNBD) || (status == GRB_INFEASIBLE) || (status == GRB_UNBOUNDED)) { cout << "The model cannot be solved because it is " << "infeasible or unbounded" << endl; return 1; } if (status != GRB_OPTIMAL) { cout << "Optimization was stopped with status " << status << endl; return 0; } // Find the smallest variable value double minVal = GRB_INFINITY; int minVar = 0; v = model.getVars(); for (int j = 0; j < model.get(GRB_IntAttr_NumVars); ++j) { double sol = v[j].get(GRB_DoubleAttr_X); if ((sol > 0.0001) && (sol < minVal) && (v[j].get(GRB_DoubleAttr_LB) == 0.0)) { minVal = sol; minVar = j; } } cout << "\n*** Setting " << v[minVar].get(GRB_StringAttr_VarName) << " from " << minVal << " to zero ***" << endl << endl; v[minVar].set(GRB_DoubleAttr_UB, 0.0); // Solve from this starting point model.optimize(); // Save iteration & time info double warmCount = model.get(GRB_DoubleAttr_IterCount); double warmTime = model.get(GRB_DoubleAttr_Runtime); // Reset the model and resolve cout << "\n*** Resetting and solving " << "without an advanced start ***\n" << endl; model.reset(); model.optimize(); // Save iteration & time info double coldCount = model.get(GRB_DoubleAttr_IterCount); double coldTime = model.get(GRB_DoubleAttr_Runtime); cout << "\n*** Warm start: " << warmCount << " iterations, " << warmTime << " seconds" << endl; cout << "*** Cold start: " << coldCount << " iterations, " << coldTime << " seconds" << endl; } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch (...) { cout << "Error during optimization" << endl; } delete[] v; delete env; return 0; }
int main(int argc, char *argv[]) { int time_limit; char name[1000]; double cutoff=0.0; ListGraph g; EdgeWeight lpvar(g); EdgeWeight weight(g); NodeName vname(g); ListGraph::NodeMap<double> posx(g),posy(g); string filename; int seed=1; // uncomment one of these lines to change default pdf reader, or insert new one //set_pdfreader("open"); // pdf reader for Mac OS X //set_pdfreader("xpdf"); // pdf reader for Linux //set_pdfreader("evince"); // pdf reader for Linux srand48(seed); time_limit = 3600; // solution must be obtained within time_limit seconds if (argc!=2) {cout<< endl << "Usage: "<< argv[0]<<" <graph_filename>"<<endl << endl << "Example: " << argv[0] << " gr_berlin52" << endl << " " << argv[0] << " gr_att48" << endl << endl; exit(0);} else if (!FileExists(argv[1])) {cout<<"File "<<argv[1]<<" does not exist."<<endl; exit(0);} filename = argv[1]; // Read the graph if (!ReadListGraph(filename,g,vname,weight,posx,posy)) {cout<<"Error reading graph file "<<argv[1]<<"."<<endl;exit(0);} TSP_Data tsp(g,vname,posx,posy,weight); ListGraph::EdgeMap<GRBVar> x(g); GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); #if GUROBI_NEWVERSION model.getEnv().set(GRB_IntParam_LazyConstraints, 1); model.getEnv().set(GRB_IntParam_Seed, seed); #else model.getEnv().set(GRB_IntParam_DualReductions, 0); // Dual reductions must be disabled when using lazy constraints #endif model.set(GRB_StringAttr_ModelName, "Undirected TSP with GUROBI"); // name to the problem model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem // Add one binary variable for each edge and also sets its cost in the objective function for (ListGraph::EdgeIt e(g); e!=INVALID; ++e) { sprintf(name,"x_%s_%s",vname[g.u(e)].c_str(),vname[g.v(e)].c_str()); x[e] = model.addVar(0.0, 1.0, weight[e],GRB_BINARY,name); } model.update(); // run update to use model inserted variables // Add degree constraint for each node (sum of solution edges incident to a node is 2) for (ListGraph::NodeIt v(g); v!=INVALID; ++v) { GRBLinExpr expr; for (ListGraph::IncEdgeIt e(g,v); e!=INVALID; ++e) expr += x[e]; //aqui model.addConstr(expr == 2 ); what? ignorou! model.addConstr(expr == 2 ); } try { model.update(); // Process any pending model modifications. if (time_limit >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,time_limit); subtourelim cb = subtourelim(tsp , x); model.setCallback(&cb); tsp.max_perturb2opt_it = 200; //1000; // number of iterations used in heuristic TSP_Perturb2OPT TSP_Perturb2OPT(tsp); if (tsp.BestCircuitValue < DBL_MAX) cutoff = tsp.BestCircuitValue-BC_EPS; // // optimum value for gr_a280=2579, gr_xqf131=566.422, gr_drilling198=15808.652 if (cutoff > 0) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff ); model.update(); // Process any pending model modifications. model.optimize(); double soma=0.0; for (ListGraph::EdgeIt e(g); e!=INVALID; ++e) { lpvar[e] = x[e].get(GRB_DoubleAttr_X); if (lpvar[e] > 1-BC_EPS ) { soma += weight[e]; if ( (vname[g.u(e)] == "243")||(vname[g.v(e)] == "243") || (vname[g.u(e)] == "242")||(vname[g.v(e)] == "242") ) { cout << "Achei, x("<< vname[g.u(e)] << " , " << vname[g.v(e)] << " = " << lpvar[e] <<"\n"; } } } cout << "Solution cost = "<< soma << endl; Update_Circuit(tsp,x); // Update the circuit in x to tsp circuit variable (if better) ViewTspCircuit(tsp); }catch (...) { if (tsp.BestCircuitValue < DBL_MAX) { cout << "Heuristic obtained optimum solution" << endl; ViewTspCircuit(tsp); return 0; }else { cout << "Graph is infeasible" << endl; return 1; } } }
int main(int argc, char *argv[]) { int nt; Digraph g; // graph declaration string digraph_steiner_filename; DiNodeName vname(g); // name of graph nodes Digraph::NodeMap<double> px(g),py(g); // xy-coodinates for each node Digraph::NodeMap<int> vcolor(g);// color of nodes Digraph::ArcMap<int> ecolor(g); // color of edges ArcWeight lpvar(g); // used to obtain the contents of the LP variables ArcWeight weight(g); // edge weights Digraph::ArcMap<GRBVar> x(g); // binary variables for each arc vector <DiNode> V; int seed=0; srand48(1); // uncomment one of these lines to change default pdf reader, or insert new one //set_pdfreader("open"); // pdf reader for Mac OS X //set_pdfreader("xpdf"); // pdf reader for Linux //set_pdfreader("evince"); // pdf reader for Linux // double cutoff; // used to prune non promissing branches (of the B&B tree) if (argc!=2) {cout<< endl << "Usage: "<< argv[0]<<" <digraph_steiner_filename>"<< endl << endl; cout << "Examples: " << argv[0] << " gr_berlin52.steiner" << endl; cout << " " << argv[0] << " gr_usa48.steiner" << endl << endl; exit(0);} digraph_steiner_filename = argv[1]; //int time_limit = 3600; // solution must be obtained within time_limit seconds GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.getEnv().set(GRB_IntParam_LazyConstraints, 1); model.getEnv().set(GRB_IntParam_Seed, seed); model.set(GRB_StringAttr_ModelName, "Oriented Steiner Tree with GUROBI"); // prob. name model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem ReadListDigraphSteiner(digraph_steiner_filename,g,vname,weight,px,py,1,nt,V); Steiner_Instance T(g,vname,px,py,weight,nt,V); //for (DiNodeIt v(g);v!=INVALID;++v){ if(v==T.V[0])vcolor[v]=RED; else vcolor[v]=BLUE;} //for (int i=1;i<T.nt;i++) vcolor[T.V[i]] = MAGENTA; //for (ArcIt e(g); e != INVALID; ++e) ecolor[e] = BLUE; //ViewListDigraph(g,vname,px,py,vcolor,ecolor,"Triangulated graph"); // Generate the binary variables and the objective function // Add one binary variable for each edge and set its cost in the objective function for (Digraph::ArcIt e(g); e != INVALID; ++e) { char name[100]; sprintf(name,"X_%s_%s",vname[g.source(e)].c_str(),vname[g.target(e)].c_str()); x[e] = model.addVar(0.0, 1.0, weight[e],GRB_BINARY,name); } model.update(); // run update to use model inserted variables try { //if (time_limit >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,time_limit); //model.getEnv().set(GRB_DoubleParam_ImproveStartTime,10); //try better sol. aft. 10s // if (cutoff > 0) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff ); ConnectivityCuts cb = ConnectivityCuts(T , x); model.setCallback(&cb); model.update(); //model.write("model.lp"); system("cat model.lp"); model.optimize(); double soma=0.0; for (DiNodeIt v(g);v!=INVALID;++v) vcolor[v]=BLUE; // all nodes BLUE for (int i=0;i<T.nt;i++) vcolor[T.V[i]]=MAGENTA; // change terminals to MAGENTA vcolor[T.V[0]]=RED; // change root to RED for (Digraph::ArcIt e(g); e!=INVALID; ++e) { lpvar[e] = x[e].get(GRB_DoubleAttr_X); if (lpvar[e] > 1.0 - EPS) { soma += weight[e]; ecolor[e] = RED; } else ecolor[e] = NOCOLOR; } cout << "Steiner Tree Value = " << soma << endl; ViewListDigraph(g,vname,px,py,vcolor,ecolor, "Steiner Tree cost in graph with "+IntToString(T.nnodes)+ " nodes and "+IntToString(T.nt)+" terminals: "+DoubleToString(soma)); } catch (...) {cout << "Error during callback..." << endl; } return 0; }
int main(){ float peso1, peso2, peso3,peso4; int origem, destino; // vértices para cada aresta; int id = 0; // id das arestas que leremos do arquivo para criar o grafo cin>>n; // quantidade de vértices do grafo; arestas = new short*[n]; coeficienteObjetv = new double*[n]; matrix_peso1 = new double*[n]; matrix_peso2 = new double*[n]; matrix_peso3 = new double*[n]; matrix_peso4 = new double*[n]; for (int i=0; i<n; i++){ arestas[i] = new short[n]; coeficienteObjetv[i] = new double[n]; matrix_peso1[i] = new double[n]; matrix_peso2[i] = new double[n]; matrix_peso3[i] = new double[n]; matrix_peso4[i] = new double[n]; } GRBEnv env = GRBEnv();; env.set("OutputFlag","0"); GRBModel model = GRBModel(env);; GRBVar **y, **x; float epslon = 0.0001; //cin>>epslon; y = new GRBVar*[n]; x = new GRBVar*[n]; for (int i=0; i<n;i++){ y[i] = new GRBVar[n]; x[i] = new GRBVar[n]; } int constrCont=0; // Create variables for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ arestas[i][j] = 0; } } while (cin>>origem){ cin>>destino; cin>>peso1; cin>>peso2; cin>>peso3; cin>>peso4; coeficienteObjetv[origem][destino] = (peso1*epslon + peso2*epslon + peso3*epslon + peso4)*(-1); // o problema é de maximizacao x[origem][destino] = model.addVar(0.0, 100000, 0.0, GRB_CONTINUOUS, "x"+to_string(origem)+to_string(destino)); x[destino][origem] = model.addVar(0.0, 100000, 0.0, GRB_CONTINUOUS, "x"+to_string(destino)+to_string(origem)); y[origem][destino] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y"+to_string(origem)+to_string(destino)); arestas[origem][destino] = 1; arestas[destino][origem] = 1; matrix_peso1[origem][destino] = peso1*(-1); matrix_peso2[origem][destino] = peso2*(-1); matrix_peso3[origem][destino] = peso3*(-1); matrix_peso4[origem][destino] = peso4*(-1); id++; } int nA = id; // quantidade de arestas do grafo int m = 1;// por default, o m falado por Lokman and Koksalan sera igual a 1 model.update(); // Set objective: GRBLinExpr exprObjet; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1) exprObjet.addTerms(&coeficienteObjetv[i][j], &y[i][j],1); } } model.setObjective(exprObjet,GRB_MAXIMIZE); // constraint 3.9 (FERNANDES, 2016) GRBLinExpr constr5 ; double coefff = 1; for (int j=0+1; j<n; j++){ if (arestas[0][j] == 1) constr5.addTerms(&coefff,&x[0][j],1); } model.addConstr(constr5, GRB_EQUAL, n-1,to_string(constrCont++)); // // Add constraint 3.10 (FERNANDES, 2016) double com = -1; for (int j=1; j<n; j++){ GRBLinExpr constr2 = 0; for (int i=0; i<n; i++){ if (arestas[i][j] == 1){ constr2.addTerms(&coefff,&x[i][j],1); constr2.addTerms(&com,&x[j][i],1); } } model.addConstr(constr2, GRB_EQUAL, 1,to_string(constrCont++)); } double coef = (double) n - 1; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1){ GRBLinExpr constr8; GRBLinExpr constr9; constr8.addTerms(&coef,&y[i][j],1); constr9.addTerms(&coefff ,&x[i][j],1); constr9.addTerms(&coefff ,&x[j][i],1); model.addConstr(constr8, GRB_GREATER_EQUAL, constr9,to_string(constrCont++)); } } } //cout<<"Modelo carregado"<<endl; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1){ GRBLinExpr constr22; GRBLinExpr constr33; constr22.addTerms(&coefff ,&y[i][j],1); constr33.addTerms(&coefff ,&x[i][j],1); constr33.addTerms(&coefff ,&x[j][i],1); // cout<<constr22<<GRB_LESS_EQUAL<<constr33<<endl; model.addConstr(constr22, GRB_LESS_EQUAL, constr33,to_string(constrCont++)); } } } int nn = 0; // o 'n' do algoritmo de Lokman and Koksalan //int kk_estrela = 0; // o 'k*' do algoritmo 2 de Lokman and Koksalan int MM = 100000000; // o 'M' do algoritmo 2 de Lokman and Koksalan int z4_k_estrela; // pra guardar o Z_p^(P^(b^(k*,n))) do algoritmo 2 de Lokman and Koksalan /* * Algoritmo 2 de Lokman and Koksalan */ try { times(&tempsInit); // para medir o tempo em caso limite pthread_t thread_time; pthread_attr_t attr; int nnnnnnnn=0; if(pthread_create(&thread_time, NULL, &tempo, (void*)nnnnnnnn)){ // on criee efectivement la thread de rechaufage cout<<"Error to create the thread"<<endl; exit(EXIT_FAILURE); } // bool auxbol = false; // vira true (e o será pra sempre) quando resolvemos um modelo diferente do SIMPLES int optimstatus; short **result = new short*[n]; for (int ii=0; ii<n; ii++){ result[ii] = new short[n]; } model.optimize(); // P0,4 --> n=0 (modelo SIMPLES) optimstatus = model.get(GRB_IntAttr_Status); int z1=0,z2=0,z3=0,z4=0; if (optimstatus != GRB_INFEASIBLE){ for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1){ result[i][j] = y[i][j].get(GRB_DoubleAttr_X); // GUARDA O RESULTADO z1+=result[i][j]*matrix_peso1[i][j]; // calcula os pesos z2+=result[i][j]*matrix_peso2[i][j]; z3+=result[i][j]*matrix_peso3[i][j]; z4+=result[i][j]*matrix_peso4[i][j]; } } } S.push_back(result); Pesos ppp = (Pesos){z1,z2,z3,z4}; Z.push_back(ppp); nn++; } do{ // esse loop para quando z4_k_estrela==-MM z4_k_estrela =(-1)*MM; // guarda o maximo short **z_n_plus_1 = new short*[n]; for (int ii=0; ii<n; ii++){ z_n_plus_1[ii] = new short[n]; } int z1_estrela, z2_estrela, z3_estrela; for (int ki=-1; ki<nn; ki++){ // no algoritmo original, ki deve variar de 0 à n (existindo solucoes de 1 à n). //aqui, portanto, fazemos k de -1 à n-1, porque as solucoes vao de 0 à n-1 for (int kj=-1; kj<nn; kj++){ // como i de ver menor que j, a unica possibilidade é i=1 e j=2, pois p-2=2 //cout<<ki<<" "<<kj<<endl; //cout<< Z[ki].peso1<<" "<< Z[kj].peso2<<endl; //if (kj!=-1 && ki!=-1 && (Z[ki].peso1 + 1 <= Z[kj].peso1)){ //Primeiramente, prepara o b1 e b2 e b3 int b1,b2,b3; //b1 if (ki==-1) b1=(-1)*MM; // -M else { b1 = Z[ki].peso1 + 1; } //b2 if (kj==-1) b2=(-1)*MM; // -M else { if (Z[kj].peso1 >= b1){ b2 = Z[kj].peso2 + 1; } else b2=(-1)*MM; // -M } //b3 b3 = (-1)*MM; // Snk = vazio for (int ii=0; ii<S.size(); ii++){ if (Z[ii].peso1>=b1 && Z[ii].peso2>=b2) { if (Z[ii].peso3 > b3){ b3 = Z[ii].peso3; } } } if (b3!=(-1)*MM) b3=b3+1; // max + 1 //cout <<"b1= "<<b1<<" b2= "<<" "<<b2<<" b3= "<<b3<<endl; if (auxbol == true){ // remove as restricoes de z2>b2 e adiciona novas GRBConstr cb1 = model.getConstrByName("cb1"); GRBConstr cb2 = model.getConstrByName("cb2"); GRBConstr cb3 = model.getConstrByName("cb3"); model.remove(cb1); model.remove(cb2); model.remove(cb3); } GRBLinExpr cb1; GRBLinExpr cb2; GRBLinExpr cb3; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1){ cb1.addTerms(&matrix_peso1[i][j], &y[i][j],1); cb2.addTerms(&matrix_peso2[i][j], &y[i][j],1); cb3.addTerms(&matrix_peso3[i][j], &y[i][j],1); } } } model.addConstr(cb1, GRB_GREATER_EQUAL, b1,"cb1"); model.addConstr(cb2, GRB_GREATER_EQUAL, b2,"cb2"); model.addConstr(cb3, GRB_GREATER_EQUAL, b3,"cb3"); // AGORA RESOLVE-SE O MODELO auxbol=true; model.optimize(); optimstatus = model.get(GRB_IntAttr_Status); if (optimstatus != GRB_INFEASIBLE){ short **result = new short*[n]; for (int ii=0; ii<n; ii++){ result[ii] = new short[n]; } z1=0,z2=0,z3=0,z4=0; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ if (arestas[i][j] == 1){ result[i][j] = y[i][j].get(GRB_DoubleAttr_X); // GUARDA O RESULTADO z1+=result[i][j]*matrix_peso1[i][j]; // calcula os pesos z2+=result[i][j]*matrix_peso2[i][j]; z3+=result[i][j]*matrix_peso3[i][j]; z4+=result[i][j]*matrix_peso4[i][j]; } } } if (z4>z4_k_estrela){ z1_estrela = z1; z2_estrela = z2; z3_estrela = z3; z4_k_estrela = z4; for (int i=0; i<n; i++){ for (int j=i+1; j<n; j++){ z_n_plus_1[i][j] = result[i][j]; } } } } //} } } if (z4_k_estrela!=(-1)*MM){ S.push_back(z_n_plus_1); Pesos pppp = (Pesos){z1_estrela,z2_estrela,z3_estrela,z4_k_estrela}; Z.push_back(pppp); nn++; //cout<<"nn = "<<nn<<endl; } } while (z4_k_estrela!=(-1)*MM); times(&tempsFinal1); /* current time */ // clock final clock_t user_time1 = (tempsFinal1.tms_utime - tempsInit.tms_utime); cout<<user_time1<<endl; cout<<(float) user_time1 / (float) sysconf(_SC_CLK_TCK)<<endl;//"Tempo do usuario por segundo : " cout<<"RESULTADO FINAL..."<<endl; printResultado(); } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } return 0; }
void AlignmentLP::ConstructLP(SpeechSolution *proposal) { try { GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); //model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // Create all the variables. s.resize(cs_.problems_size()); s2.resize(cs_.problems_size()); //position_var.resize(cs_.problems_size()); // Create the hmms. for (uint u = 0; u < s.size(); ++u) { const ClusterProblem &problem = cs_.problem(u); int M = problem.num_steps; s[u].resize(M); s2[u].resize(M); //position_var.resize(M); for (int m = 0; m < M; ++m) { s[u][m].resize(problem.num_states); s2[u][m].resize(problem.num_states); for (uint n = 0; n < s[u][m].size(); ++n) { s[u][m][n].resize(cs_.num_hidden(0)); { stringstream buf; buf << "s2_" << u << "_" << m << "_" << n; s2[u][m][n] = model.addVar(0.0, 1.0, 0.0, VAR_TYPE, buf.str()); } for (uint o = 0; o < s[u][m][n].size(); ++o) { s[u][m][n][o].resize(3); for (uint f = 0; f <= 2; ++f) { stringstream buf; buf << "s_" << u << "_" << m << "_" << n << "_" << o << "_" << f; double score = distances_[u]->get_distance(m, o); s[u][m][n][o][f] = model.addVar(0.0, 1.0, score, VAR_TYPE, buf.str()); } } } // position_var[u][m].resize(cs_.num_types()); // for (uint l = 0; l < position_var[u][m].size(); ++l) { // position_var[u][m][l].resize(cs_.num_hidden(0)); // for (uint o = 0; o < position_var[u][m][l].size(); ++o) { // stringstream buf; // buf << "position_" << u << "_" << m << "_" << l << "_" << o; // position_var[u][m][l][o] = // model.addVar(0.0, 1.0, 0.0, // VAR_TYPE, buf.str()); // } // } } } r.resize(cs_.num_types()); for (uint l = 0; l < r.size(); ++l) { r[l].resize(cs_.num_hidden(0)); for (uint o = 0; o < r[l].size(); ++o) { stringstream buf; buf << "r_" << l << "_" << o; r[l][o] = model.addVar(0.0, 1.0, 0.0, VAR_TYPE, buf.str()); } } t.resize(cs_.problems_size()); for (uint u = 0; u < t.size(); ++u) { const ClusterProblem &problem = cs_.problem(u); t[u].resize(problem.num_states); for (uint n = 0; n < t[u].size(); ++n) { t[u][n].resize(cs_.num_hidden(0)); for (uint o = 0; o < t[u][n].size(); ++o) { stringstream buf; buf << "t_" << u << "_" << n << "_" << o; t[u][n][o] = model.addVar(0.0, 1.0, 0.0, VAR_TYPE, buf.str()); } } } model.update(); // Make constraints. // Constraint for M // for all l, sum_q r(l,o) = 1 for (int l = 0; l < cs_.num_types(); ++l) { // Sum out o's. GRBLinExpr sum; for (int o = 0; o < cs_.num_hidden(0); ++o) { sum += r[l][o]; } // Equal to 1. model.addConstr(sum == 1); } cerr << "Done Constraint 1" << endl; // Constraint for N // for all m,n,o, i, s(u, m, n, o) = s(m-1, n, o, 0) + // s(m-1, n, o, 1) for (int u = 0; u < cs_.problems_size(); ++u) { const ClusterProblem &problem = cs_.problem(u); int M = problem.num_steps; int N = problem.num_states; for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { for (int o = 0; o < cs_.num_hidden(0); ++o) { if (m != 0 && n != 0) { GRBLinExpr sum; model.addConstr(s[u][m][n][o][0] + s[u][m][n][o][2] == s[u][m - 1][n][o][0] + s[u][m - 1][n][o][1], "incoming"); } if (m != M - 1 && n != N - 1) { model.addConstr(s[u][m][n][o][0] + s[u][m][n][o][1] == s[u][m + 1][n][o][0] + s[u][m + 1][n][o][2], "Outgoing"); } GRBLinExpr sum, sum2; for (int o2 = 0; o2 < cs_.num_hidden(0); ++o2) { sum += s[u][m][n][o2][1]; sum2 += s[u][m][n][o2][2]; } model.addConstr(s2[u][m][n] == sum, "Outgoing"); if (m != M - 1 && n != N - 1) { model.addConstr(sum2 == s2[u][m + 1][n + 1], "Outgoing"); } } } } { GRBLinExpr sum; for (int o = 0; o < cs_.num_hidden(0); ++o) { sum += s[u][0][0][o][1]; } model.addConstr(sum == 1, "Starting"); } { GRBLinExpr sum; for (int o = 0; o < cs_.num_hidden(0); ++o) { sum += s[u][problem.num_steps - 1][problem.num_states - 1][o][0]; } model.addConstr(sum == 1); } } // Tying constraints 3. // forall n, o, r(y_n, o) = t(n,o) for (int u = 0; u < cs_.problems_size(); ++u) { const ClusterProblem &problem = cs_.problem(u); for (int n = 0; n < problem.num_states; ++n) { GRBLinExpr sum; for (int o = 0; o < cs_.num_hidden(0); ++o) { sum += t[u][n][o]; } model.addConstr(sum == 1); } for (int n = 0; n < problem.num_states; ++n) { int l = problem.MapState(n); for (int o = 0; o < cs_.num_hidden(0); ++o) { model.addConstr(r[l][o] == t[u][n][o]); GRBLinExpr sum; for (int m = 0; m < problem.num_steps; ++m) { sum += s[u][m][n][o][1]; } model.addConstr(sum == t[u][n][o]); } } } cerr << "Done Constraint 3" << endl; if (true) { for (int u = 0; u < cs_.problems_size(); ++u) { const ClusterProblem &problem = cs_.problem(u); int M = problem.num_steps; int N = problem.num_states; for (int m = 0; m < M; ++m) { for (uint l = 0; l < r.size(); ++l) { vector <int> locations; for (int n = 0; n < N; ++n) { if ((int)l == problem.MapState(n)) { locations.push_back(n); } } for (int o = 0; o < cs_.num_hidden(0); ++o) { GRBLinExpr sum; for (uint occ_index = 0; occ_index < locations.size(); ++occ_index) { int n = locations[occ_index]; sum += s[u][m][n][o][0] + s[u][m][n][o][1] + s[u][m][n][o][2]; } //model.addConstr(position_var[u][m][l][o] == sum); model.addConstr(sum <= r[l][o]); } } } } } // model.addConstr(r[7][2] == 0.5); // model.addConstr(r[7][3] == 0.5); // model.addConstr(r[0][1] == 0.5); // model.addConstr(r[0][6] == 0.5); // model.addConstr(r[13][4] == 0.5); // model.addConstr(r[13][9] == 0.5); model.update(); //model.write("temp.lp"); // // // Done! model.optimize(); if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) { model.computeIIS(); model.write("temp.ilp"); } vector<double> costs; for (uint u = 0; u < s.size(); ++u) { SpeechAlignment *align = proposal->mutable_alignment(u); const ClusterProblem &problem = cs_.problem(u); vector<int> *state_hidden = align->mutable_hidden_alignment(); vector<int> *state_align = align->mutable_alignment(); state_hidden->resize(problem.num_steps); state_align->resize(problem.num_steps); int M = problem.num_steps; int N = 0; for (int m = 0; m < M; ++m) { N = s[u][m].size(); costs.resize(s[u][m].size()); for (uint n = 0; n < s[u][m].size(); ++n) { for (uint o = 0; o < s[u][m][n].size(); ++o) { for (uint f = 0; f <= 2; ++f) { if (s[u][m][n][o][f].get(GRB_DoubleAttr_X) != 0) { (*state_hidden)[m] = o; (*state_align)[m] = problem.MapState(n); string position; if (f == 0) { position = "I"; } else if (f == 1) { position = "B"; } else { position = "O"; } cerr << "s " << m << " " << n << " " << o << " " << position << " " << s[u][m][n][o][f].get(GRB_DoubleAttr_X) << " " << s[u][m][n][o][f].get(GRB_DoubleAttr_Obj) << " " << problem.MapState(n) << endl; costs[n] += s[u][m][n][o][f].get(GRB_DoubleAttr_X) * s[u][m][n][o][f].get(GRB_DoubleAttr_Obj); } } } } } for (int n = 0; n < N; ++n) { cerr << n << " " << costs[n] << endl; } } for (uint u = 0; u < t.size(); ++u) { for (uint n = 0; n < t[u].size(); ++n) { for (uint o = 0; o < t[u][n].size(); ++o) { if (t[u][n][o].get(GRB_DoubleAttr_X) != 0) { cerr << "t " << n << " " << o << " " << t[u][n][o].get(GRB_DoubleAttr_X) << endl; } } } } for (uint l = 0; l < r.size(); ++l) { for (uint o = 0; o < r[l].size(); ++o) { if (r[l][o].get(GRB_DoubleAttr_X) != 0) { cerr << "r " << l << " " << o << " " << r[l][o].get(GRB_DoubleAttr_X) << endl; } } } } catch(GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } catch(...) { cout << "Exception during optimization" << endl; } }
int main(int argc,char *argv[]) { srand48(1); if (argc!=2) {cout<<endl<<"Usage: "<< argv[0]<<" <filename>"<< endl << endl; cout << "Example: " << argv[0] << " arq1.in" << endl << endl; exit(0);} ifstream ifile; ifile.open(argv[1]); if (!ifile) return(false); int n, m; ifile >> n; ifile >> m; vector<double> jobsize(n); vector<double> machinespeed(m); for(int i=0; i<n; i++) ifile >> jobsize[i]; for(int i=0; i<m; i++) ifile >> machinespeed[i]; ifile.close(); cout << endl; cout << "Numero de tarefas: " << n << endl; cout << "Tamanho das tarefas" << endl; for(int i=0; i<n; i++) cout << "T_" << i << ": " << jobsize[i] << endl; cout << "Velocidade das maquinas" << endl; for(int i=0; i<m; i++) cout << "M_" << i << ": " << machinespeed[i] << endl; cout << endl << endl; try { /*--------------------------------------------------------------------------------- */ /*--------------------------- ALTERE DAQUI PARA ABAIXO ---------------------------- */ /*--------------------------------------------------------------------------------- */ // Voce deve atualizar esta variavel com valores 0 e 1 de tal forma que // tarefa_maquina[i][j] = 1 SE_E_SOMENTE_SE a tarefa i foi escalonada na maq. j vector<vector<int> > tarefa_maquina(n, vector<int>(m)); // mais abaixo eh feito um escalonamento aleatorio. // Nao se esqueca de alterar a geracao do escalonamento aleatorio para o // escalonamento obtido pelo seu programa linear inteiro // cabecalho comum para os programas lineares (descomente) int seed=0; GRBEnv env = GRBEnv(); GRBModel model = GRBModel(env); model.getEnv().set(GRB_IntParam_Seed, seed); model.set(GRB_StringAttr_ModelName, "Escalonamento de Tarefas"); // prob. name model.set(GRB_IntAttr_ModelSense, GRB_MINIMIZE); // is a minimization problem // Lembretes e sintaxes de alguns comandos: // GRBVar = nome do tipo das variaveis do Gurobi // //vector<GRBVar> x[m]; double TP[m][n]; GRBVar max = model.addVar(0,10000,1,GRB_CONTINUOUS,"max"); // Para inserir uma variavel: // <variavel_do_tipo_GRBVar> = // model.addVar(<lower_bound>, // <upper_bound> , // <valor_na_funcao_objetivo>, // <GRB_CONTINUOUS ou GRB_BINARY GRB_INTEGER> // <string_do_nome_da_variavel>); // // Este comando deve ser executado depois de inserir todas as variaveis e antes de // inserir as restricoes. model.update(); // // Para declarar variavel que armazena expressoes // for(int i = 0; i<n; i++) { //para cada maquina // GRBLinExpr expr; // for(int j = 0; j<m;j++) { //para cada tarefa // expr+=x[m*i+j]; // } // model.addConstr(expr >= 0); // } // // Para inserir uma restricao linear: // model.addConstr( <restricao_linear> ); // // Comando para escrever o modelo produzido em um arquivo texto (apenas para debugar) model.update(); //Tem que dar update de novo pra ver as restricoes model.write("model.lp"); system("cat model.lp"); // // Depois que construiu seu programa linear / linear inteiro, execute este comando // para resolver o programa linear model.optimize(); // // Verifica se obteve solucao otima if (model.get(GRB_IntAttr_Status) != GRB_OPTIMAL) { cout << "Erro, sistema impossivel" << endl; exit(1); } // // Para obter o valor da variavel do programa linear/linear inteiro // deve ser executado apos o model.optimize() // <variavel_do_tipo_GRBVar>.get(GRB_DoubleAttr_X) // Faz um escalonamento aleatorio. // Remova esta parte e coloque o escalonamento // gerado pelo seu programa linear inteiro. for (int i=0; i<n; i++) for (int j=0; j<m; j++) tarefa_maquina[i][j] = 0; // deixa cada maq. sem atribuicao // for (int i=0; i<n; i++) // for (int j=0; j<m; j++) // //if(x[j][i].get(GRB_DoubleAttr_X) >= 0.999) { // // cout << j << " " << i << endl; // tarefa_maquina[i][j] = 1; // } /*--------------------------------------------------------------------------------- */ /*--------------------------- ALTERE DAQUI PARA CIMA ------------------------------ */ /*--------------------------------------------------------------------------------- */ cout << "\n\n"; double makespan=0; cout << "Escalonamento obtido das tarefas nas maquinas\n"; cout << "Notacao para tarefas : T_id(tamanho_original)\n"; cout << "Notacao para maquinas: M_id(velocidade)\n\n"; for (int j=0; j<m; j++) { double tmaq=0.0; cout << "M_"<<j<<"("<< machinespeed[j] << ") : "; for(int i=0; i<n; i++) { if (tarefa_maquina[i][j] == 1) { cout << "T_" << i << "(" << jobsize[i] << ") "; tmaq += jobsize[i] / machinespeed[j]; } } cout << endl << "Tempo gasto pela maquina M_" <<j << ": " << tmaq << endl<<endl; if (tmaq>makespan) makespan = tmaq; } cout << "\nTempo para completar todas as tarefas: " << makespan << "s\n\n"; // } else cout << "No solution" << "\n"; } // catch (GRBException e) { // cout << "Error code = " << e.getErrorCode() << endl; // cout << e.getMessage() << endl; // } catch (...) { cout << "Exception during optimization" << endl; } return 0; }