esa / pygmo2

A Python platform to perform parallel computations of optimisation tasks (global and local) via the asynchronous generalized island model.
https://esa.github.io/pygmo2/
Mozilla Public License 2.0
422 stars 57 forks source link

Can we specify a number of evaluations (fevals) in pygmo in each run? #67

Closed chuongask closed 3 years ago

chuongask commented 3 years ago

Dear All, I am using pygmo to solve an optimization problem. Could you please show me how to specify a number of evaluations (fevals) in each run. Thank you very much!

My code is as follows:

# Define pygmo classs
class alge_function:
   # def __init__(self):
    #    self.starting = np.random.randint(1,5,16)

    def fitness(self, x):
        x = np.array(x, dtype=int) # to make integers
       x = list(x)
        fsr = look_up_function(x)
        vec = [fsr[1], fsr[2], fsr[3], fsr[4], fsr[5], fsr[6], fsr[7], fsr[8], fsr[9], \
               fsr[10], fsr[11], fsr[12], fsr[13], fsr[14], fsr[15], fsr[16], fsr[17], fsr[18]]
        obj = -choquet_pwl_function(k, mu, vec)

return [obj]

    def get_nix(self):
        return 16

# bounds [1,4]
    def get_bounds(self):
        return ([1]*16, [4] * 16)

# Algorithm of gradient
    def gradient(self, x):
        return pg.estimate_gradient_h(lambda x: self.fitness(x), x)

# use "slsqp"- sequential least squares programming algorithm
start = time.time()
algo = pg.algorithm(uda = pg.mbh(pg.nlopt("slsqp"), stop = 20, perturb = .2))
algo.set_verbosity(1) # print dialog
# Formulate minimization problem
pop = pg.population(prob = alge_function(), size = 2)
pop.set_x(1,starting_point)
# Solve the problem
pop = algo.evolve(pop)

# Collect information
print("objective value: obj=", pop.champion_f[0])
darioizzo commented 3 years ago

Stopping criterions depend on the uda used. In your case mbh with nlopd inner. For nlop look here to the attribute max_fevals: https://esa.github.io/pygmo2/algorithms.html#pygmo.nlopt.maxeval, for mbh you cannot define a maximum number of fevals, but you can log them and crop later if thats what you need.

chuongask commented 3 years ago

Thank you for your message. I will study this.