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
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!