2017-08-27 74 views
1

我想用鴨嘴獸只用2個目標,3個變量和無限制的整數(而不是浮點數)執行多目標優化,我需要最大化目標值。我定義它是這樣的:使用整數的鴨嘴獸優化

problem = Problem(3, 2) 
problem.directions[:] = Problem.MAXIMIZE 
problem.types[:] = [Integer(-50, 50), Integer(-50, 50), Integer(-50, 50)] 

algorithm = NSGAII(problem) 
algorithm.run(10000) 

for solution in algorithm.result: 
    print solution 

但我一直得到的結果是這樣的:

Solution[[False, True, False, True, False, True, True],[False, True, False, True, False, True, False],[True, True, True, False, True, False, True]|-12.2,629.8|0] 
Solution[[False, True, False, True, False, True, True],[True, True, False, True, False, True, False],[True, False, True, False, True, True, False]|-28.0,1240.0|0] 

你能幫幫我嗎?

在此先感謝。

回答

1

試試這個:

from platypus import Problem, Integer, NSGAII 

def my_function(x): 
    """ Some objective function""" 
    return -x[0] ** 2 - x[2] ** 2 # we expect the result x[0] = 0, x[1] = whatever, and x[2] = 0 

problem = Problem(3, 1) # define 3 inputs and 1 objective (and no constraints) 
problem.directions[:] = Problem.MAXIMIZE 
int1 = Integer(-50, 50) 
int2 = Integer(-50, 50) 
int3 = Integer(-50, 50) 
problem.types[:] = [int1, int2, int3] 
problem.function = my_function 
algorithm = NSGAII(problem) 
algorithm.run(10000) 

# grab the variables (note: we are just taking the ones in the location result[0]) 
first_variable = algorithm.result[0].variables[0] 
second_variable = algorithm.result[0].variables[1] 
third_variable = algorithm.result[0].variables[2] 

print(int1.decode(first_variable)) 
print(int2.decode(second_variable)) 
print(int3.decode(third_variable)) 

基本上,在這種情況下,由於範圍是所有的輸入(INT1,INT2,和INT3具有相同的範圍),我們可以也做「int1.decode(同second_variable)「等等,但是如果你想把每個整數的範圍改變成不同的東西,我就把它留在這裏。

+0

這樣做。謝謝你的幫助! :) – Elian