from gekko import GEKKO
sample = [[[0, 0.5, 0, 0, 0.5], [0, 0.08, 0.92, 0, 0], [0, 0.44, 0.56, 0, 0], [0, 0, 0.84, 0.16, 0], [0.34, 0.66, 0, 0, 0], [0, 0.53, 0.47, 0, 0], [0, 0, 0.8, 0.2, 0]], [0.89, 1, 1, 1, 1, 1, 1], [[1], [1], [1], [1], [1], [1], [1]], [0, 0.5, 1, 1.5, 2, 2.3, 3]]
# Initialize Model
m = GEKKO()
#define parameter
eq = m.Param(value=1)
for i in range(0,len(sample[0])):
for j in range(0,len(sample[0][i])):
if sample[0][i][j] != None:
sample[0][i][j] = None
for j in range(0,len(sample[1])):
if sample[1][j] != None:
sample[1][j] = None
for i in range(0,len(sample[2])):
for j in range(0,len(sample[2][i])):
if sample[2][i][j] != None:
sample[2][i][j] = None
for i in range(3, len(sample)):
for j in range(0,len(sample[i])):
if sample[i][j] != None:
sample[i][j] = None
x = sample
#print(x)
for i in range(0,len(x[0])):
for j in range(0,len(x[0][i])):
x[0][i][j] = m.Var(lb=0.0,ub=1.0)
for j in range(0,len(x[1])):
x[1][j] =m.Var(lb=0.0,ub=1.0)
for i in range(0,len(x[2])):
for j in range(0,len(x[2][i])):
x[2][i][j] = m.Var(lb=0.0,ub=1.0)
for j in range(1,len(x[3])-1):
x[3][j] = m.Var(lb=0.0,ub=3.00)
x[3][0] = m.Var(lb=0.0,ub=0.000000001)
x[3][len(x[3])-1] = m.Var(lb=3.0,ub=3.0001)
#Initial value
ini = [[[0, 0.5, 0, 0, 1], [0, 0.08, 0.92, 0, 0], [0, 0.44, 0.56, 0, 0], [0, 0, 0.84, 0.16, 0], [0.34, 0.66, 0, 0, 0], [0, 0.53, 0.47, 0, 0], [0, 0, 0.8, 0.2, 0]], [0.89, 1, 1, 1, 1, 1, 1], [[1], [1], [1], [1], [1], [1], [1]], [0, 0.5, 1, 1.5, 2, 2.3, 3.0]]
for i in range(0,len(x[0])):
for j in range(0,len(x[0][i])):
x[0][i][j].value = ini[0][i][j]
for j in range(0,len(x[1])):
x[1][j].value = ini[1][j]
for i in range(0,len(x[2])):
for j in range(0,len(x[2][i])):
x[2][i][j].value = ini[2][i][j]
for j in range(0,len(x[3])):
x[3][j].value = ini[3][j]
#Constraint1 sum of all belief degree is 1
for i in range(0,len(x[0])):
m.Equation(sum(x[0][i])==eq)
#Constraint2 RV[i+1] > RV[i]
for j in range(1,len(x[3])-1):
m.Equation(x[3][j+1] - x[3][j] > 0)
#Objective
def gekko_obj(x):
belief_consequence = x[0]
rl_weight = x[1]
att_weight = x[2]
RV = x[3]
Data_Transformation(RV)
List_of_Rule()
Rule_Activation_Weight(rl_weight,att_weight)
Aggregated_Belief_and_Output(belief_consequence)
exp_output()
objective_minimize()
return objective_value
m.Obj(gekko_obj(x))
#Set global options
m.options.IMODE = 3 #steady state optimization
#Solve simulation
m.solve(remote=True) # solve on public server
#Results
print('')
print('Results')
print(x)
函数'def gekko_obj(x)'适用于x的任何值。
然而,当m被称为Gekko目标函数时,它失败了。 Obj(gekko_obj(x))。
文件 “/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py” 第710行,在runfile中 execfile(filename,namespace)
文件 “/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py” 第101行,在execfile中 exec(compile(f.read(),filename,'exec'),namespace)
文件“/ Users / swatisachan / Desktop / Python文件/优化/ BRB 通过GEKKO1.py“进行优化”,第451行,in m.Obj(gekko_obj(X))
文件“/ Users / swatisachan / Desktop / Python文件/优化/ BRB 通过gekko_obj中的GEKKO1.py“,第444行进行优化 Data_Transformation(RV)
文件“/ Users / swatisachan / Desktop / Python文件/优化/ BRB 在Data_Transformation中通过GEKKO1.py“,第53行进行优化 如果L1 [i] [j]> RV [K]:
文件“/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”, 第25行,在 len 中 return len(self.value)
文件“/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”, 第122行, len return len(self.value)
TypeError:'int'类型的对象没有len()
数据转换功能是:
###Generate input data uniformaly
from random import randrange, uniform
global L1
L1 = []
data = []
for i in range(10):
frand = uniform(0, 3)
data.append(frand)
L1.append(sorted(data)) #Input data
print('data: '+str(L1))
header =['x']
global RV
def Data_Transformation(RV):
#print('Referential value: ' +str(RV))
global Rule_RV_list
global L3
Rule_RV_list = []
L3 = []
for i in range(0,len(header)):
global L2
L2 = []
if all(isinstance(x, (int,float)) for x in L1[i]):
global num_RV
global RV_index
Rule_RV_list.append(RV)
for j in range(0,len(L1[i])):
match = [0 for col in range(len(RV))]
intial_bl_dis = list(zip(RV, match))
print(RV)
for k in range(0,len(RV)):
if L1[i][j] > RV[k]:
a_plus = RV[k+1]
print('a plus:' +str(a_plus))
if RV.index(a_plus) > 0:
a_minus = RV[RV.index(a_plus)-1]
else:
a_minus = RV[RV.index(a_plus)+1]
alpha_minus = abs((a_plus - L1[i][j])/(a_plus - a_minus))
alpha_plus = 1 - alpha_minus
RV_index = RV.index(a_plus)
intial_bl_dis[RV_index] = (intial_bl_dis[RV_index][0],alpha_plus)
intial_bl_dis[RV_index-1] = (intial_bl_dis[RV_index-1][0],alpha_minus)
L2.append(intial_bl_dis)
L3.append(L2)
我已发布了部分代码。
答案 0 :(得分:4)
RV的元素是GEKKO Variable类的实例
>>> type(RV[k])
<class 'gekko.gk_variable.GKVariable'>
打印这些对象时显示属性value
,>
运算符不会比较对象的value
(它返回比较的字符串表示)。您可以通过将行更改为:
if L1[i][j] > RV[k].value:
看起来之后还有另一个错误,它也与GEKKO变量以有趣的方式重载运算符有关。