Can I use an external function as my objective function?

2,194 views
Skip to first unread message

Scott Lawson

unread,
May 31, 2016, 7:41:47 PM5/31/16
to Pyomo Forum
I am new to Pyomo and I've been struggling to understand whether it is possible to use an external function as my objective function. I have been reading "Pyomo - Optimization Modelling in Python", but I am still unsure if what I am trying to do is possible.

Here is a simplified description of what I am trying to do:

I have trained a neural network with two input parameters and one output. The output of the neural network is a estimate of the performance of a device that I am developing. I would like to optimize the performance of my device for a range of possible input values.

I want to use the neural network as my objective function, but I do not know how to define this kind of objective function in Pyomo. The neural network models a continuous function but it does not provide derivative information directly.

It should also be noted that the neural network function cannot be expressed in terms of the math functions provided by Pyomo alone. I need to use external functions which is causing the following error:

"""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."""

My questions are:
  • Is it possible to use external functions for the objective? If so, how?
  • Can external functions be used with any solver? Only some solvers?
  • Is Pyomo the right library for this?
I have tried the following code, but keep getting the aforementioned error message when I try to run it:

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()

Siirola, John D

unread,
Jun 1, 2016, 10:38:44 AM6/1/16
to pyomo...@googlegroups.com

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.

Antonio Liggieri

unread,
Nov 14, 2016, 10:45:24 AM11/14/16
to Pyomo Forum
Hi John,

I have very similar problem that I'd like to solve using pyomo in conjunction with ipopt.

However, my formulation will be slightly different:

I have a hydraulic network (e.g. water distribution network of a city), for which I can calculate all flow rates in every pipe using hydraulic-network-code. The objective can be to vary the diameters of all pipes in order to obtain equal flow rates in every single pipe. To achive this, the "variance equation" could be used, which needs to be minimized. Hence, the objective function is the variance equation, where the values for the varinace equation come from the hydraulic-network-code calculating the flow rates. The optimizer should then propose a new set of diameters, which is given  back to the hydraulic-network-code which in turn calculates the new flow rates based on the proposed diameters and so on...

- Is that principally possible with the most recent version of pyomo?
- If so, ist there any example that you could provide?

Thanks in advance for your effort.
Antonio

Harpreet

unread,
Dec 28, 2016, 3:10:27 PM12/28/16
to Pyomo Forum
Hi Antonio,

I am a new user to pyomo and my problem has a similar setting as yours in the sense that it's connected through nested functions that form the structure of the overall model I want to optimize. I was wondering if you found some answer to your questions? Probably that may help me get started as well. 

Cheers.

Siirola, John D

unread,
Jan 3, 2017, 10:48:15 AM1/3/17
to pyomo...@googlegroups.com

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

Steve O

unread,
Oct 16, 2018, 1:30:06 PM10/16/18
to Pyomo Forum
Dear John,


I have a similar problem. Basically my objective function is not explicitly defined but it involves calling an external program to get the system losses.

def obj_rule(model):
    PVgen.qgini = model.x
    sim.prepare_loadflow(ldf_mode='unbalanced')
    sim.run_loadflow()
    losses = np.array(grid[0].GetAttribute('c:LossP'))
    
    return losses

model = pyo.ConcreteModel()
model.x = pyo.Var(initialize=15, bounds=(0,200))
model.obj = pyo.Objective(rule=obj_rule, sense=minimize)

solver = SolverFactory('gurobi')

results = solver.solve(model, tee=True)
print(pyo.value(model.x)) 

When running I get the following error:  'SimpleVar' object is not a 'double' object.

David Woodruff

unread,
Oct 16, 2018, 6:11:52 PM10/16/18
to pyomo...@googlegroups.com
Hi Steve,
  Pyomo creates the objective function when the model is created but does not have a mechanism that I know of to pass a callback function to the solver. Pyomo expects to create an expression for the objective function that does not include external calls.
  Dave


--

Utkarsh Konge

unread,
Apr 26, 2019, 2:29:27 AM4/26/19
to Pyomo Forum
Hello everyone,

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.

Diego Lovison

unread,
Jan 23, 2023, 11:26:15 PM1/23/23
to Pyomo Forum
Hi, any idea about the original question?
Thanks
Reply all
Reply to author
Forward
0 new messages