调度优化问题不知道更好的方法来呈现解决方案 - Python / Gekko

问题描述 投票:0回答:1

所以我的音乐学校举办了一个音乐节,让所有学生乐队一起演奏。 由于有一些家庭成员跨越不同的乐队,我正在尝试创建一个解决方案来优化乐队的时间表 - 尽量减少有亲戚的乐队之间的时间总和。现在我想我已经找到了解决方案。不知道这是否是最好的,但看起来确实有效。现在我正在尝试找到更好的方式来呈现解决方案。这个“x”矩阵很大,也许最好只提取“1”并制作一个时间表,其中包含时段以及按时间顺序排列的乐队。但我不知道从哪里开始。希望有人能指点一下方向

相关信息:

  1. 英语不是我的母语;
  2. 我不是程序员,我在下面的代码中所做的一切都是我过去两天能够学到的关于Python的知识。

我正在使用下面的代码。有什么建议欢迎批评指正。谢谢!

from gekko import GEKKO
import numpy as np
m = GEKKO()

#variables and constrains
n = 20 #n of bands
s = 37 #n of slots to play
t = 25 #time in minutes one band spend on the stage

x = m.Array(m.Var,(n,s),value=0,lb=0,ub=1,integer=True) #matrix of all bands(rows) x slots (columns)
for j in range(s):
        m.Equation(m.sum([x[i,j] for i in range(n)])<=1) #since this is the decision i made it binary and sum=1 to 1 band only ocuppie one slot
for i in range(n):
        m.Equation(m.sum([x[i,j] for j in range(s)])==1) #since this is the decision i made it binary and sum=1 to 1 band only ocuppie one slot
       
z = [k for k in range(1,s+1)] #array with slot index to use to calc time 
w = z*x*t #time the band will perform ;; used in objetive function


#objective
#in this exemple the band (1,4) and the band (1,5) have a relationship, and so do (3,2) and (1,15), so the objetive function bellow will try to minimize the sum of the time between bands that have relationship 
y = m.abs2(w.item(1, 4)-w.item(1, 5))+ m.abs2(w.item(3, 2)-w.item(1, 15)) 
m.Minimize(y)


#solver
m.options.SOLVER = 1
m.solve()



#print(w.item((1, 4)))
print('y')
print(y)
print('x')
print(x)

python optimization linear-programming gekko
1个回答
0
投票

您需要提取

矩阵
中每个1的频段和插槽编号,并按插槽编号排序时间表:

schedule = [(i+1, j+1) for i in range(n) for j in range(s) if x[i,j][0] == 1]
schedule.sort(key=lambda x: x[1])
for band, slot in schedule:
    print(f"Band {band} is scheduled to play in slot {slot}")
© www.soinside.com 2019 - 2024. All rights reserved.