Karthik Raman - 10 months ago 75

Python Question

I am using the PyGMO package for Python, for multi-objective optimisation. I am unable to fix the dimension of the fitness function in the constructor, and the documentation is not very descriptive either. I am wondering if anyone here has had experience with PyGMO in the past: this could be fairly simple.

I try to construct a minimum example below:

`from PyGMO.problem import base`

from PyGMO import algorithm, population

import numpy as np

import matplotlib.pyplot as plt

class my_problem(base):

def __init__(self, fdim=2):

NUM_PARAMS = 4

super(my_problem, self).__init__(NUM_PARAMS)

self.set_bounds(0.01, 100)

def _objfun_impl(self, K):

E1 = K[0] + K[2]

E2 = K[1] + K[3]

return (E1, E2, )

if __name__ == '__main__':

prob = my_problem() # Create the problem

print (prob)

algo = algorithm.sms_emoa(gen=100)

pop = population(prob, 50)

pop = algo.evolve(pop)

F = np.array([ind.cur_f for ind in pop]).T

plt.scatter(F[0], F[1])

plt.xlabel("$E_1$")

plt.ylabel("$E_2$")

plt.show()

`fdim=2`

`ValueError: ..\..\src\problem\base.cpp,584: fitness dimension was changed inside objfun_impl().`

I'd be grateful if someone can help figure this out. Thanks!

Answer

I modified their example and this seemed to work for me.

```
#(...)
def __init__(self, fdim=2):
NUM_PARAMS = 4
# We call the base constructor as 'dim' dimensional problem, with 0 integer parts and 2 objectives.
super(my_problem, self).__init__(NUM_PARAMS,0,fdim)
self.set_bounds(0.01, 100)
#(...)
```