Skip to content

Instantly share code, notes, and snippets.

@Ogaday
Created March 24, 2025 15:22
Show Gist options
  • Save Ogaday/ee6b7c36d792c931b272512d01d79e89 to your computer and use it in GitHub Desktop.
Save Ogaday/ee6b7c36d792c931b272512d01d79e89 to your computer and use it in GitHub Desktop.
Installing Ipopt
# Example from https://jckantor.github.io/CBE30338/06.99-Pyomo-Examples.html
import pyomo.environ as pyo
V = 40 # liters
kA = 0.5 # 1/min
kB = 0.1 # l/min
CAf = 2.0 # moles/liter
# create a model instance
model = pyo.ConcreteModel()
# create x and y variables in the model
model.q = pyo.Var()
# add a model objective
model.objective = pyo.Objective(expr = model.q*V*kA*CAf/(model.q + V*kB)/(model.q + V*kA), sense=pyo.maximize)
# compute a solution using ipopt for nonlinear optimization
results =pyo.SolverFactory('ipopt').solve(model)
model.pprint()
# print solutions
qmax = model.q()
CBmax = model.objective()
print('\nFlowrate at maximum CB = ', qmax, 'liters per minute.')
print('\nMaximum CB =', CBmax, 'moles per liter.')
print('\nProductivity = ', qmax*CBmax, 'moles per minute.')
1 Var Declarations
q : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : None : 8.944271909985442 : None : False : False : Reals
1 Objective Declarations
objective : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : 40.0*q/(q + 4.0)/(q + 20.0)
2 Declarations: q objective
Flowrate at maximum CB = 8.944271909985442 liters per minute.
Maximum CB = 0.954915028125263 moles per liter.
Productivity = 8.541019662483748 moles per minute.

Installing Ipopt

https://coin-or.github.io/Ipopt/INSTALL.html

  • ASL is required to use Ipopt from Pyomo
  • I likely already had BLAS/LAPACK installed from other projects, I don't know if my libraries are optimised.
# Install dependencies
sudo apt-get install gcc g++ gfortran git patch wget pkg-config liblapack-dev libmetis-dev
# Install ASL
git clone https://github.com/coin-or-tools/ThirdParty-ASL.git
cd ThirdParty-ASL
./get.ASL
./configure
make
sudo make install
# Install MUMPS
git clone https://github.com/coin-or-tools/ThirdParty-Mumps.git
cd ThirdParty-Mumps
./get.Mumps
./configure
make
sudo make install

Download Ipopt source from here: https://github.com/coin-or/Ipopt/releases eg. Source code (tar.gz)

# Install Ipopt
gunzip Ipopt-x.y.z.tgz
tar xvf Ipopt-x.y.z.tar
mv Ipopt-x.y.z Ipopt
cd Ipopt
export IPOPTDIR=/path/to/currentdir

mkdir $IPOPTDIR/build
cd $IPOPTDIR/build
$IPOPTDIR/configure
make
make test
sudo make install

You may need to run sudo ldconfig after these steps - it's not specified anywhere, until I found this my solver failed with the following error message:

ERROR: Solver (ipopt) returned non-zero return code (127)
ERROR: Solver log: /usr/local/bin/ipopt: error while loading shared libraries:
libipoptamplinterface.so.3: cannot open shared object file: No such file or
directory

You can check the exit codes for any steps after running them by using echo $?. 0 means success, anything else is an error.

@Ogaday
Copy link
Author

Ogaday commented Mar 25, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment