Movatterモバイル変換


[0]ホーム

URL:


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

Feasopt.java#

/* Copyright 2025, Gurobi Optimization, LLC *//* This example reads a MIP model from a file, adds artificial   variables to each constraint, and then minimizes the sum of the   artificial variables.  A solution with objective zero corresponds   to a feasible solution to the input model.   We can also use FeasRelax feature to do it. In this example, we   use minrelax=1, i.e. optimizing the returned model finds a solution   that minimizes the original objective, but only from among those   solutions that minimize the sum of the artificial variables. */importcom.gurobi.gurobi.*;publicclassFeasopt{publicstaticvoidmain(String[]args){if(args.length<1){System.out.println("Usage: java Feasopt filename");System.exit(1);}try{GRBEnvenv=newGRBEnv();GRBModelfeasmodel=newGRBModel(env,args[0]);// Create a copy to use FeasRelax feature later */GRBModelfeasmodel1=newGRBModel(feasmodel);// Clear objectivefeasmodel.setObjective(newGRBLinExpr());// Add slack variablesGRBConstr[]c=feasmodel.getConstrs();for(inti=0;i<c.length;++i){charsense=c[i].get(GRB.CharAttr.Sense);if(sense!='>'){GRBConstr[]constrs=newGRBConstr[]{c[i]};double[]coeffs=newdouble[]{-1};feasmodel.addVar(0.0,GRB.INFINITY,1.0,GRB.CONTINUOUS,constrs,coeffs,"ArtN_"+c[i].get(GRB.StringAttr.ConstrName));}if(sense!='<'){GRBConstr[]constrs=newGRBConstr[]{c[i]};double[]coeffs=newdouble[]{1};feasmodel.addVar(0.0,GRB.INFINITY,1.0,GRB.CONTINUOUS,constrs,coeffs,"ArtP_"+c[i].get(GRB.StringAttr.ConstrName));}}// Optimize modified modelfeasmodel.optimize();feasmodel.write("feasopt.lp");// use FeasRelax feature */feasmodel1.feasRelax(GRB.FEASRELAX_LINEAR,true,false,true);feasmodel1.write("feasopt1.lp");feasmodel1.optimize();// Dispose of model and environmentfeasmodel1.dispose();feasmodel.dispose();env.dispose();}catch(GRBExceptione){System.out.println("Error code: "+e.getErrorCode()+". "+e.getMessage());}}}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp