Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Example Tour
Light LogoDark Logo
Gurobi
Back to top

lpmod_c++.cpp#

/* Copyright 2025, Gurobi Optimization, LLC *//* This example reads an LP model from a file and solves it.   If the model can be solved, then it finds the smallest positive variable,   sets its upper bound to zero, and resolves the model two ways:   first with an advanced start, then without an advanced start   (i.e. 'from scratch'). */#include"gurobi_c++.h"usingnamespacestd;intmain(intargc,char*argv[]){if(argc<2){cout<<"Usage: lpmod_c++ filename"<<endl;return1;}GRBEnv*env=0;GRBVar*v=0;try{// Read model and determine whether it is an LPenv=newGRBEnv();GRBModelmodel=GRBModel(*env,argv[1]);if(model.get(GRB_IntAttr_IsMIP)!=0){cout<<"The model is not a linear program"<<endl;return1;}model.optimize();intstatus=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;return1;}if(status!=GRB_OPTIMAL){cout<<"Optimization was stopped with status "<<status<<endl;return0;}// Find the smallest variable valuedoubleminVal=GRB_INFINITY;intminVar=0;v=model.getVars();for(intj=0;j<model.get(GRB_IntAttr_NumVars);++j){doublesol=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 pointmodel.optimize();// Save iteration & time infodoublewarmCount=model.get(GRB_DoubleAttr_IterCount);doublewarmTime=model.get(GRB_DoubleAttr_Runtime);// Reset the model and resolvecout<<"\n*** Resetting and solving "<<"without an advanced start ***\n"<<endl;model.reset();model.optimize();// Save iteration & time infodoublecoldCount=model.get(GRB_DoubleAttr_IterCount);doublecoldTime=model.get(GRB_DoubleAttr_Runtime);cout<<"\n*** Warm start: "<<warmCount<<" iterations, "<<warmTime<<" seconds"<<endl;cout<<"*** Cold start: "<<coldCount<<" iterations, "<<coldTime<<" seconds"<<endl;}catch(GRBExceptione){cout<<"Error code = "<<e.getErrorCode()<<endl;cout<<e.getMessage()<<endl;}catch(...){cout<<"Error during optimization"<<endl;}delete[]v;deleteenv;return0;}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp