[英]Adding a constraint in Linear Programming Optimization using PULP
以下代碼為我提供了保持低成本的最佳度假地點:
from pulp import *
import numpy as np
import pandas as pd
import re
#write a scaper before hand
data = pd.read_csv('clymb_adventures.csv')
problem_name = 'GoingOnVacation'
aval_vacation_days = 10
def optimize_vacation_schedule(aval_vacation_days):
# create the LP object, set up as a minimization problem --> since we
want to minimize the costs
prob = pulp.LpProblem(problem_name, pulp.LpMinimize)
#create decision variables
decision_variables = []
for rownum, row in data.iterrows():
variable = str('x' + str(rownum))
variable = pulp.LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Integer') #make variables binary
decision_variables.append(variable)
print ("Total number of decision_variables: " + str(len(decision_variables)))
#create objective Function -minimize the costs for the trip
total_cost = ""
for rownum, row in data.iterrows():
for i, schedule in enumerate(decision_variables):
if rownum == i:
formula = row['cost']*schedule
total_cost += formula
prob += total_cost
print ("Optimization function: " + str(total_cost))
#create constrains - total vacation days should be no more than 14
total_vacation_days = ""
for rownum, row in data.iterrows():
for i, schedule in enumerate(decision_variables):
if rownum == i:
formula = row['duration']*schedule
total_vacation_days += formula
prob += (total_vacation_days == aval_vacation_days)
#now run optimization
optimization_result = prob.solve()
assert optimization_result == pulp.LpStatusOptimal
prob.writeLP(problem_name + ".lp" )
print("Status:", LpStatus[prob.status])
print("Optimal Solution to the problem: ", value(prob.objective))
print ("Individual decision_variables: ")
for v in prob.variables():
print(v.name, "=", v.varValue)
if __name__ == "__main__":
optimize_vacation_schedule(aval_vacation_days)
樣本數據集:
destination duration cost description location
0 Baja 7 899 Hike Bike [31223,23123]
1 Nepal 11 899 culture of the Himalayas [91223,28123]
2 Spain 8 568 Sport climb [66223,23123]
3 Yosemite 3 150 Guided hiking [0223,23123]
4 Utah 6 156 Hike. [35523,23123]
5 Okla 1 136 Hike. [25523,23123]
我在數據集中添加了一個額外的字段“位置”。
我想要實現的是,如果求解器給了我三個 3 個位置作為最佳解決方案,那么它必須使用位置坐標確保兩個連續建議的引用之間的最大曼哈頓距離不大於 3000?
示例:如果求解器建議了優勝美地、猶他州和俄克拉荷馬州,那么在建議它們之前,必須檢查優勝美地到猶他州的距離低於 3000,猶他州到俄克拉荷馬州的距離低於 3000。
這也使它成為路由問題。
那么如何添加一個約束,使用位置坐標將兩個連續建議城市之間的距離保持在 3000 以下。 請幫忙
謝謝!!!!
如果您想添加條件 x(i,j) == 1 作為約束,那么您將創建第二組決策變量。 鍵是元組(i,j),值是一個帶有 cat='Binary' 的 LpVariable。 然后你必須設置一些額外的約束。
注意:我假設 x 是一個字典,其中鍵是一個位置,值是一個決策變量。 我不確定你為什么在這里使用列表。 需要進行一些修改以匹配您的結構。
import itertools
locations = ['Baja', 'Nepal', 'Spain', ...]
x = LpVariable.dicts('x', destinations, cat='Binary'
prob = LpProblem('Vacation', pulp.LpMinimize)
# non-duplicative cominations
destination_pairs = itertools.combinations(locations, 2)
# new decision variables
# i is destination1, j is destination2
y = {(i, j): LpVariable('y', cat='Binary') for i, j in destination_pairs}
# new constraints
# if x[i] or x[j] is 0, then y[(i,j)] has to be zero
# if y[(i, j)] is 1, then x[i] and x[j] both have to be one
for i, j in destination_pairs:
prob += y[(i, j)] <= x[i]
prob += y[(i, j)] <= x[j]
prob += y[(i,j] >= x[i] + x[j] - 1
# this ensures that a combination is chosen
prob += lpSum(y.values()) >= 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.