Multi-objective Optimization Query
回答済みimport pandas as pd
import gurobipy as gp
from gurobipy import GRB
foods = {"Rice","Milk","Bread","Egg"}
store1={'Rice': 0.65,'Milk':1.0,'Bread': 0.5,'Egg': 0.25}
store2={'Rice': 0.40,'Milk':0.75,'Bread': 0.40,'Egg': 0.40}
store3={'Rice': 0.15,'Milk':0.50,'Bread': 0.30,'Egg': 0.30}
store4={'Rice': 0.90,'Milk':1.25,'Bread': 0.60,'Egg': 0.60}
store5={'Rice': 1.15,'Milk':1.50,'Bread': 0.70,'Egg': 0.70}
stores ={**store1, **store2, **store3, **store4, **store5}
# Model
m = gp.Model("diet")
sum_store1_1=sum(store1.values())
sum_store1=round(sum_store1_1, 2)
print("Store1: %g" %sum_store1)
sum_store2_1=sum(store2.values())
sum_store2=round(sum_store2_1, 2)
print("Store2: %g" %sum_store2)
sum_store3_1=sum(store3.values())
sum_store3=round(sum_store3_1, 2)
print("Store3: %g" %sum_store3)
sum_store4_1=sum(store4.values())
sum_store4=round(sum_store4_1, 2)
print("Store4: %g" %sum_store4)
sum_store5_1=sum(store5.values())
sum_store5=round(sum_store5_1, 2)
print("Store5: %g" %sum_store5)
sum_store=[sum_store1, sum_store2, sum_store3, sum_store4, sum_store5]
Results:
Store1: 2.4
Store2: 1.95
Store3: 1.25
Store4: 3.35
Store5: 4.05
import gurobipy as gpResults:
from gurobipy import GRB
nutrition={"Energy","Fibre","Zinc"}
foods={"Rice","Milk","Bread","Egg"}
nutrition, minNutrition, maxNutrition = gp.multidict({
'Energy': [1800, 2400],'Fibre': [25, 28],'Zinc': [8, 11]})
foods,f_min,f_max = gp.multidict({
'Rice':[0,10],'Milk': [0,10],'Bread':[0,14],'Egg': [0,13]})
fuel_cost= 1
foods, cost = gp.multidict({
'Rice': 0.65,
'Milk': 1.0,
'Bread': 0.5,
'Egg': 0.25})
# Nutrition values for the foods
nutritionValues = {
('Rice', 'Energy'): 242,
('Rice', 'Fibre'): 0.6,
('Rice', 'Zinc'): 0.4,
('Milk', 'Energy'): 149,
('Milk', 'Fibre'): 0,
('Milk', 'Zinc'): 0.4,
('Bread', 'Energy'): 80,
('Bread', 'Fibre'): 3,
('Bread', 'Zinc'): 0.7,
('Egg', 'Energy'): 12,
('Egg', 'Fibre'): 0,
('Egg', 'Zinc'): 1.2}
# Model
m = gp.Model("diet")
# Create decision variables for the foods to buy
buy = m.addVars(foods, vtype=GRB.CONTINUOUS, name="buy")
# The objective is to minimize the costs
m.setObjective(buy.prod(cost)+ fuel_cost, GRB.MINIMIZE)
# Nutrition constraints
m.addConstrs(((cost[f] > 0 for f in foods)), "Cost")
m.addConstrs((gp.quicksum(nutritionValues[f, c] * buy[f] for f in foods)
== [minNutrition[c], maxNutrition[c]]
for c in nutrition), "_")
m.addConstrs(((buy[f] <= f_max[f] for f in foods)), "Buying limit")
def printSolution():
if m.status == GRB.OPTIMAL:
print('\nCost: %g' % m.ObjVal)
print('\nBuy:')
for f in foods:
if buy[f].X > 0.0001:
print("Quantity of %s to buy %g $" % (f, buy[f].X))
else:
print('No solution')
# Solve
m.optimize()
printSolution()
Hello, my project involves two parts. The first part is choosing a store with optimal cost. In my case, the higher cost is for high nutrition content, and the lower cost is for low nutrition content. And the results are as shown above. I am trying to use the optimal cost (not too high or not too low) in the cost section of my second part.
In the second part, I am trying to minimize the cost of food items while satisfying the nutritional and quantity constraint.
I am trying to include a trade-off between cost and nutritional degradation(high nutrition means high cost and vice-versa) and plot a Pareto-optimal trade-off.
In my project, I want to include multiple objectives, i.e., minimizing the cost and minimizing the nutritional degradation (which increases as we minimize the cost).
I am trying to achieve the above-mentioned objective, but I need your guidance on how I can accomplish it.
Is there any other tradeoff or objective that can replace minimizing the nutritional degradation objective that I can incorporate into my project?
The aforementioned graph shows the trade-off I require in my project with nutritional degradation or any other objective which can generate a similar curve.
Best Regards,
Shivounsh
-
Hi Shivounsh,
If you are not interested in the Pareto front for your bi-objective model, then you can use Gurobi's Multi Objective feature. The multiobj.py example is a good starting point to understand how multiple objectives work within Gurobi.
If however, you are interested in the (almost) whole Pareto front for your bi-objective model, then it would be better to implement a method on your own. For example, you could implement the \(\epsilon\)-constraint method. It is a standard method to compute Pareto fronts. There is plenty of literature on the \(\epsilon\)-constraint method, one example would be by Yang et al.
Is there any other tradeoff or objective that can replace minimizing the nutritional degradation objective that I can incorporate into my project?
In my opinion, the nutritional degradation sounds like a good second objective. However, I am not expert on nutrition quality so maybe someone more knowledgeable can come up with something more advanced.
Best regards,
Jaromił0
サインインしてコメントを残してください。
コメント
1件のコメント