Movatterモバイル変換


[0]ホーム

URL:


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

Diet.java#

/* Copyright 2025, Gurobi Optimization, LLC *//* Solve the classic diet model, showing how to add constraints   to an existing model. */importcom.gurobi.gurobi.*;publicclassDiet{publicstaticvoidmain(String[]args){try{// Nutrition guidelines, based on// USDA Dietary Guidelines for Americans, 2005// http://www.health.gov/DietaryGuidelines/dga2005/StringCategories[]=newString[]{"calories","protein","fat","sodium"};intnCategories=Categories.length;doubleminNutrition[]=newdouble[]{1800,91,0,0};doublemaxNutrition[]=newdouble[]{2200,GRB.INFINITY,65,1779};// Set of foodsStringFoods[]=newString[]{"hamburger","chicken","hot dog","fries","macaroni","pizza","salad","milk","ice cream"};intnFoods=Foods.length;doublecost[]=newdouble[]{2.49,2.89,1.50,1.89,2.09,1.99,2.49,0.89,1.59};// Nutrition values for the foodsdoublenutritionValues[][]=newdouble[][]{{410,24,26,730},// hamburger{420,32,10,1190},// chicken{560,20,32,1800},// hot dog{380,4,19,270},// fries{320,12,10,930},// macaroni{320,15,12,820},// pizza{320,31,12,1230},// salad{100,8,2.5,125},// milk{330,8,10,180}// ice cream};// ModelGRBEnvenv=newGRBEnv();GRBModelmodel=newGRBModel(env);model.set(GRB.StringAttr.ModelName,"diet");// Create decision variables for the nutrition information,// which we limit via boundsGRBVar[]nutrition=newGRBVar[nCategories];for(inti=0;i<nCategories;++i){nutrition[i]=model.addVar(minNutrition[i],maxNutrition[i],0,GRB.CONTINUOUS,Categories[i]);}// Create decision variables for the foods to buy//// Note: For each decision variable we add the objective coefficient//       with the creation of the variable.GRBVar[]buy=newGRBVar[nFoods];for(intj=0;j<nFoods;++j){buy[j]=model.addVar(0,GRB.INFINITY,cost[j],GRB.CONTINUOUS,Foods[j]);}// The objective is to minimize the costs//// Note: The objective coefficients are set during the creation of//       the decision variables above.model.set(GRB.IntAttr.ModelSense,GRB.MINIMIZE);// Nutrition constraintsfor(inti=0;i<nCategories;++i){GRBLinExprntot=newGRBLinExpr();for(intj=0;j<nFoods;++j){ntot.addTerm(nutritionValues[j][i],buy[j]);}model.addConstr(ntot,GRB.EQUAL,nutrition[i],Categories[i]);}// Solvemodel.optimize();printSolution(model,buy,nutrition);System.out.println("JSON solution:"+model.getJSONSolution());System.out.println("\nAdding constraint: at most 6 servings of dairy");GRBLinExprlhs=newGRBLinExpr();lhs.addTerm(1.0,buy[7]);lhs.addTerm(1.0,buy[8]);model.addConstr(lhs,GRB.LESS_EQUAL,6.0,"limit_dairy");// Solvemodel.optimize();printSolution(model,buy,nutrition);System.out.println("JSON solution:"+model.getJSONSolution());// Dispose of model and environmentmodel.dispose();env.dispose();}catch(GRBExceptione){System.out.println("Error code: "+e.getErrorCode()+". "+e.getMessage());}}privatestaticvoidprintSolution(GRBModelmodel,GRBVar[]buy,GRBVar[]nutrition)throwsGRBException{if(model.get(GRB.IntAttr.Status)==GRB.Status.OPTIMAL){System.out.println("\nCost: "+model.get(GRB.DoubleAttr.ObjVal));System.out.println("\nBuy:");for(intj=0;j<buy.length;++j){if(buy[j].get(GRB.DoubleAttr.X)>0.0001){System.out.println(buy[j].get(GRB.StringAttr.VarName)+" "+buy[j].get(GRB.DoubleAttr.X));}}System.out.println("\nNutrition:");for(inti=0;i<nutrition.length;++i){System.out.println(nutrition[i].get(GRB.StringAttr.VarName)+" "+nutrition[i].get(GRB.DoubleAttr.X));}}else{System.out.println("No solution");}}}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp