【发布时间】:2020-07-17 09:06:56
【问题描述】:
我有 6 个公司可能的订单列表(a、b、c、d、e、f),以美元为单位。价格每周(13 周)变化如下。
a1_list = [1075000, 1075000, 1072500, 1072500, 1070000, 1070000, 1050000, 1535000, 1535000, 1015000, 1015000, 1000000, 1000000]
b1_list = [1275000, 1275000, 1278000, 1278000, 1242000, 1242000, 1266000, 1806000, 1806000, 1191000, 1191000, 1191000, 1191000]
c1_list = [1530000, 1530000, 1822500, 1822500, 1813500, 1813500, 1804500, 1773000, 1773000, 1746000, 1746000, 1710000, 1710000]
d1_list = [1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 1005000, 960000, 960000, 960000, 960000,
960000, 960000]
e1_list = [2436000, 2436000, 2905000, 2905000, 2877000, 2877000, 2849000, 2814000, 2814000, 2779000, 2779000, 2730000, 2730000]
f1_list = [1881000, 1881000, 1890000, 1890000, 1863000, 1863000, 1854000, 1822500, 1822500, 1804500, 1804500, 1786500, 1786500]
我每次只能从一家公司选择一份订单。也就是说,如果我选择第一周的订单是comp A,那么公司B,C,D,E和F一定不能在第1周。列表中的每个数字都对应于特定的一周。
例如,A公司的订单,第1周和第2周的价格为1075000。如果订单不是在第1周或第2周下的,而是在第3周和第4周下的,则价格为1072500。
这是一个优化问题。
我写了一个手动计算来计算给我带来最大利润的最佳组合。
以下是我的代码:
import numpy as np
from scipy import optimize
price = np.array([
[430, 425, 340, 402, 348, 418],
[429, 426, 405, 402, 415, 420],
[428, 414, 403, 402, 411, 414],
[420, 422, 401, 402, 407, 412],
[614, 602, 394, 384, 402, 405],
[406, 397, 388, 384, 397, 401],
[400, 397, 380, 384, 390, 397],
])
a1Q = 2500
a2Q = 4000
b1Q = 3000
b2Q = 3500
c1Q = 4500
c2Q = 5500
c3Q = 4000
d1Q = 2500
d2Q = 4000
e1Q = 7000
e2Q = 2000
f1Q = 4500
f2Q = 1000
a1_price = price[:,0]*a1Q
a2_price = price[:,0]*a2Q
b1_price = price[:,1]*b1Q
b2_price = price[:,1]*b2Q
c1_price = price[:,2]*c1Q
c2_price = price[:,2]*c2Q
c3_price = price[:,2]*c3Q
d1_price = price[:,3]*d1Q
d2_price = price[:,3]*d2Q
e1_price = price[:,4]*e1Q
e2_price = price[:,4]*e2Q
f1_price = price[:,5]*f1Q
f2_price = price[:,5]*f2Q
a1_list = [a1_price[0]]*2 + [a1_price[1]]*2 + [a1_price[2]]*2 + [a1_price[3]] + [a1_price[4]]*2 + [a1_price[5]]*2 + [a1_price[6]]*2
a2_list = [a2_price[0]]*2 + [a2_price[1]]*2 + [a2_price[2]]*2 + [a2_price[3]] + [a2_price[4]]*2 + [a2_price[5]]*2 + [a2_price[6]]*2
b1_list = [b1_price[0]]*2 + [b1_price[1]]*2 + [b1_price[2]]*2 + [b1_price[3]] + [b1_price[4]]*2 + [b1_price[5]]*2 + [b1_price[6]]*2
b2_list = [b2_price[0]]*2 + [b2_price[1]]*2 + [b2_price[2]]*2 + [b2_price[3]] + [b2_price[4]]*2 + [b2_price[5]]*2 + [b2_price[6]]*2
c1_list = [c1_price[0]]*2 + [c1_price[1]]*2 + [c1_price[2]]*2 + [c1_price[3]] + [c1_price[4]]*2 + [c1_price[5]]*2 + [c1_price[6]]*2
c2_list = [c2_price[0]]*2 + [c2_price[1]]*2 + [c2_price[2]]*2 + [c2_price[3]] + [c2_price[4]]*2 + [c2_price[5]]*2 + [c2_price[6]]*2
c3_list = [c3_price[0]]*2 + [c3_price[1]]*2 + [c3_price[2]]*2 + [c3_price[3]] + [c3_price[4]]*2 + [c3_price[5]]*2 + [c3_price[6]]*2
d1_list = [d1_price[0]]*2 + [d1_price[1]]*2 + [d1_price[2]]*2 + [d1_price[3]] + [d1_price[4]]*2 + [d1_price[5]]*2 + [d1_price[6]]*2
d2_list = [d2_price[0]]*2 + [d2_price[1]]*2 + [d2_price[2]]*2 + [d2_price[3]] + [d2_price[4]]*2 + [d2_price[5]]*2 + [d2_price[6]]*2
e1_list = [e1_price[0]]*2 + [e1_price[1]]*2 + [e1_price[2]]*2 + [e1_price[3]] + [e1_price[4]]*2 + [e1_price[5]]*2 + [e1_price[6]]*2
e2_list = [e2_price[0]]*2 + [e2_price[1]]*2 + [e2_price[2]]*2 + [e2_price[3]] + [e2_price[4]]*2 + [e2_price[5]]*2 + [e2_price[6]]*2
f1_list = [f1_price[0]]*2 + [f1_price[1]]*2 + [f1_price[2]]*2 + [f1_price[3]] + [f1_price[4]]*2 + [f1_price[5]]*2 + [f1_price[6]]*2
f2_list = [f2_price[0]]*2 + [f2_price[1]]*2 + [f2_price[2]]*2 + [f2_price[3]] + [f2_price[4]]*2 + [f2_price[5]]*2 + [f2_price[6]]*2
best = 0
ind_a1,ind_b1,ind_c1,ind_d1,ind_e1,ind_f1 = 0,0,0,0,0,0
for a1 in a1_list:
# print(f'A: {ind_a}')
# ind_a+=1
ind_b1 = 0
# ind_c=0
for b1 in b1_list:
if ind_b1 != ind_a1:
# print(ind_b)
# total = a + b
ind_c1 = 0
for c1 in c1_list:
if ind_c1 != ind_a1 and ind_c1 != ind_b1:
ind_d1 = 0
for d1 in d1_list:
if ind_d1 != ind_a1 \
and ind_d1 != ind_b1 \
and ind_d1!= ind_c1:
ind_e1 = 0
for e1 in e1_list:
if ind_e1 != ind_a1 \
and ind_e1 != ind_b1 \
and ind_e1!= ind_c1 \
and ind_e1!= ind_d1:
ind_f1 = 0
for f1 in f1_list:
if ind_f1 != ind_a1 \
and ind_f1 != ind_b1 \
and ind_f1!= ind_c1 \
and ind_f1!= ind_d1 \
and ind_f1!= ind_e1:
total = a1 + b1 + c1 + d1 + e1 + f1
if total >best:
best = total
print(a1,b1,c1,d1,e1,f1, ind_a1,ind_b1,ind_c1,ind_d1,ind_e1,ind_f1)
print(best)
ind_f1 +=1
ind_e1 +=1
ind_d1 += 1
ind_c1+=1
ind_b1+=1
ind_a1+=1
我想知道是否有更简单、更简洁的方法来解决这个问题,或者使用内置的 scipy 优化函数,如 scipy.optimize.minimize 或 scipy.optimize.brute?
这实际上是我正在解决的一个巨大问题优化功能的一部分。
只是给你一个想法(但可能与这个问题无关),下面是一些情节。我正在努力使收入最大化。
任何帮助将不胜感激。
【问题讨论】:
标签: search optimization scipy maximize maximum-profit-problem