"""Implicit conversion of Pyomo NumericValue type `%s' to a float is
disabled. This error is often the result of using Pyomo components as
arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math
functions."""
from coopr.pyomo import *
model = ConcreteModel()
# Objective function calls external neural network function
def objective_function(model):
performance = neural_network_prediction(model.x_1, model.x_2)
return performance
# Variables
model.x_1 = Var(within=NonNegativeReals)
model.x_2 = Var(within=NonNegativeReals)
# Objective function
model.obj = Objective(rule=objective_function, sense=maximize)
# Constraints
model.con1 = Constraint(expr=model.x_1 + model.x_2 <= 10.0)
opt = SolverFactory("glpk")
results = opt.solve(model, load_solutions=False)
results.write()
Scott,
The short answer is “not easily”. It is possible to use compiled external functions in nonlinear models, but those functions must also be able to provide first and second derivative information. This works through the ASL (AMPL Solver Library) external function mechanism, so your functions would need to be compiled against the ASL headers / specifications. Obviously, it would require using a nonlinear solver interfaced through the “NL” solver interface. For external *python* functions, it is technically possible, but not readily supported (I have some pre-alpha code written by Hans Pirnay that shows the concept, but have never had the chance to generalize it and get it into Pyomo).
That said, ANNs are algebraic systems at their core. If you can extract the coefficients and the form of the activation function from your “neural_network_prediction” function, they should be directly representable in Pyomo (e.g., the activation function is likely to be something like tanh or some RBF … all of which are representable in Pyomo).
Finally, for completeness, I should point out that AMPL publishes the “Extended Function Library”, which makes about 300 additional functions from the GNU Scientific Library available through AMPL (freely available under the GPL license). This library is fully compatible with Pyomo’s external function objects.
john
--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You can use external functions in either objectives or constraints. However, be aware that the AMPL external function interface (which is how the Pyomo ExternalFunction component communicates with the solver) requires you to implement the function in a compiled callable library (DLL / .so / .dynlib) and provide not just function evaluations, but also first (and depending on the solver, second) derivative evaluations as well.
John
--
I am working on a similar problem but the objective function and the constraints are in fortran. The actual problem with reproducible example is posted on stackoverflow (https://stackoverflow.com/questions/55176181/optimizing-fortran-function-in-pyomo).
It would be immensely helpful if you could guide me through the problem.
Many thanks.