1

我是在 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二进制?

4

1 回答 1

1

Hendregen 教授的回答

我无法重现您的问题,因为它需要输入您的功能。错误消息是您的不等式表达式之一在表达式中使用列表值 [45.4545] 而不是数字 45.4545。您可以通过使用 value(x[0])而不是 list来解决此问题(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)
于 2019-06-18T05:27:29.117 回答