Optimizing Fortran function in pyomo

后端 未结 1 1484
梦如初夏
梦如初夏 2021-01-25 14:56

I wish to optimize a Fortran function using Pyomo. Both the objective function and the constraints are are written in Fortran. Based on the answer given here, we can use E

相关标签:
1条回答
  • 2021-01-25 15:09

    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!

    0 讨论(0)
提交回复
热议问题