1

以下代码为我提供了保持低成本的最佳去处:

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 以下。请帮忙

谢谢!!!!

4

1 回答 1

0

如果您想添加条件 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

于 2020-04-03T20:32:47.953 回答