我刚开始在Python中使用Gekko。
运行以下代码时,出现此错误:
Exception: @error: Vector Index
APM model error: problem with vector index
line number: 21
line string:
p2>((0+((0+((v1)*([54.54545455])))+((v3)*([45.45454545]))))+((0+((v2)*([54.5454
5455])))+((v4)*([45.45454545]))))
non-integer value: 45.4545454500000
STOPPING..
具有以下代码:
from gekko import GEKKO
def Optimization_Approach(Processing_Time_Remote,Total_Time_local,Total_Time_Remote,Total_Energy_Consumption_Local,Total_Energy_Consumption_Remote,parameters):
number_of_users=parameters.number_of_users
number_of_tasks=parameters.number_of_tasks
data_rate=parameters.available_data_rate
Input_Size_Task=parameters.input_size_for_task_users
Edge_Capability_Mobile=parameters.edge_capability_mobile_users
Bandwidth=data_rate*number_of_users # All available data rate
Edge_Server_Capability=parameters.edge_server_capability
Edge_Server_Caching_Storage=parameters.edge_server_storage_capability
#Create a python model object
m = GEKKO()
#define parameter
B = m.Param(value=Bandwidth)
F_c=m.Param(value=Edge_Server_Capability)
F_s=m.Param(value=Edge_Server_Caching_Storage)
#define Variables
x = m.Array(m.Var,(number_of_users,number_of_tasks))
y = m.Array(m.Var,(number_of_users,number_of_tasks))
#Constraint 1
m.Equation(B>=sum([sum([x[i][j]*data_rate for i in range(number_of_users)]) for j in range(number_of_tasks)]))
#Constraint 2
m.Equation(F_c>=sum([sum([x[i][j]*Edge_Capability_Mobile[i] for i in range(number_of_users)]) for j in range(number_of_tasks)]))
# Constraint 3
m.Equation(F_s>=sum([sum([x[i][j]*Input_Size_Task[i][j] for i in range(number_of_users)]) for j in range(number_of_tasks)]))
#define Objective
m.Obj(sum([sum([(y[i][j]*Processing_Time_Remote[i][j])+((1-y[i][j])*((1-x[i][j])*Total_Time_local+x[i][j]*Total_Time_Remote)) +((1-y[i][j])*((1-x[i][j])*Total_Energy_Consumption_Local+x[i][j]*Total_Energy_Consumption_Remote)) for i in range(number_of_users)]) for j in range(number_of_tasks)]))
#Solve Model
m.solve()
print('Results')
print(x)
print(y)
请,有人可以帮助我吗?
另外,如何使m.array
中的变量为二进制?
答案 0 :(得分:1)
亨德里根教授的答案
我无法重现您的问题,因为它需要功能的输入。错误消息是您的不等式表达式之一正在使用列表值[45.4545],而不是表达式中的数字45.4545。您可以通过使用值(x[0])
而不是列表(x)
来解决此问题。如果将[0]放在这些列表的末尾,它将仅使用该值。
如果在创建变量数组时需要传递其他参数,则可以使用以下内容:
选项1
使用numpy
函数创建一个m.Array
数组:
y = m.Array(m.Var,4,lb=0,ub=1,integer=True)
请参见https://gekko.readthedocs.io/en/latest/model_methods.html?highlight=array
选项2 创建具有列表理解的变量列表:
y = [m.Var(lb=0,ub=1,integer=True) for i in range(4)]
选项3 另外,您可以创建一个空列表(y)并将二进制值附加到该列表。
y = []
for i in range(3):
y.append(m.Var(lb=0,ub=1,integer=True)