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)).
File
“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,
line 710, in runfile
execfile(filename, namespace)
File
“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,
line 101, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)
File “/Users/swatisachan/Desktop/Python files/Optimization /BRB
optimization by GEKKO1.py”, line 451, in
m.Obj(gekko_obj(x))
File “/Users/swatisachan/Desktop/Python files/Optimization /BRB
optimization by GEKKO1.py”, line 444, in gekko_obj
Data_Transformation(RV)
File “/Users/swatisachan/Desktop/Python files/Optimization /BRB
optimization by GEKKO1.py”, line 53, in Data_Transformation
if L1[i][j] > RV[k]:
File “/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”,
line 25, in len
return len(self.value)
File “/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”,
line 122, in len
return len(self.value)
TypeError: object of type ‘int’ has no 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)
我已经发布了部分代码.