Optimizing Fortran function in pyomo

本秂侑毒 提交于 2019-12-02 08:59:14

If you are not bound to Pyomo you could use the excellent Pygmo package which offers solvers for different kinds of problems including blackbox solvers.

Here's a small example on how to use it on a continuous constrained single objective test problem:

import pygmo as pg
import pandas as pd


class Rosenbrock():
    """Rosenbrock function constrained to a disk.

    See: https://en.wikipedia.org/wiki/Test_functions_for_optimization
    """

    def fitness(self, x):
        """Evaluate fitness.

        Instead of the Rosenbrock function you could call your Fortran
        code here e.g. by using F2PY: https://www.numfys.net/howto/F2PY/
        """
        obj = (1-x[0])**2+100*(x[1]-x[0]**2)**2
        ineq = x[0]**2+x[1]**2-2
        return [obj, ineq]

    def get_bounds(self):
        """Return boundaries."""
        return ([-1.5]*2, [1.5]*2)

    def get_nic(self):
        """Determine number of inequalities."""
        return 1


# set up and solve problem
pro = pg.problem(Rosenbrock())
pop = pg.population(pro, size=200)
# see: https://github.com/esa/pagmo2/blob/master/include/pagmo/algorithms/
algo = pg.algorithm(pg.ihs(gen=10000))
algo.set_verbosity(100)
pop = algo.evolve(pop)

# extract solutions
fits = pd.DataFrame(pop.get_f())
vectors = pd.DataFrame(pop.get_x())
best_idx = pop.best_idx()
best_vector = vectors.loc[best_idx].to_frame().T
best_fitness = fits.loc[best_idx].to_frame().T

print(best_vector)
print(best_fitness)

You would then just have to deal with "interfacing" your Fortran code within the fitness function.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!