3
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 目标函数时,它会失败。对象 (gekko_obj (x)) 。

文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 710 行,在运行文件 execfile(文件名,命名空间)中

文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 101 行,在 execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件“/Users/swatisachan/Desktop/Python files/Optimization /BRB optimization by GEKKO1.py”,第 451 行,在 m.Obj(gekko_obj(x))

文件“/Users/swatisachan/Desktop/Python files/Optimization /BRB optimization by GEKKO1.py”,第 444 行,在 gekko_obj Data_Transformation(RV)

如果 L1[i][j] > RV[k],Data_Transformation 中的文件“/Users/swatisachan/Desktop/Python files/Optimization /BRB optimization by GEKKO1.py”,第 53 行:

文件“/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”,第 25 行,在len 返回 len(self.value)

文件“/anaconda3/lib/python3.6/site-packages/gekko/gk_operators.py”,第 122 行,在len 返回 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)

我已经发布了部分代码。

4

1 回答 1

4

RV 的元素是 GEKKO 变量类的实例

>>> type(RV[k])
<class 'gekko.gk_variable.GKVariable'>

在打印这些对象显示属性value时,>运算符不比较对象的value(它返回比较的字符串表示形式)。您可以通过将行更改为:

if L1[i][j] > RV[k].value:

之后似乎还有另一个错误,这也与 GEKKO 变量以有趣的方式重载运算符有关。

于 2018-05-16T23:44:23.787 回答