GithubHelp home page GithubHelp logo

ojdo / pyomo-example Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 6.0 29 KB

"Hello World!" example for combining Pyomo (Coopr 3.5 or Pyomo 4) and Pandas for mathematical optimisation

License: GNU General Public License v3.0

Python 68.79% AMPL 31.21%

pyomo-example's Introduction

Hello Pandas + Pyomo examples

This is a minimum example that demonstrates how Pandas and Pyomo (both version 3.5 [within Coopr] and version 4) are combined to yield a mathematical optimisation model.

The example is a Minimum-cost flow problem formulation of the task to find the Bacon number of a given actor/actress within the given social graph of Hollywood actors.

What is it about?

Technically, the example shows how to convert the conceptually simpler, self-contained bacon.mod (GNU MathProg problem) into a more complex triplet of input data (input.xlsx), model formulation and helper functions (bacon.py) and a run script (runme.py).

While for this example, the scaffolding code seems unnecessarily complex, it scales much better to larger (i.e. more variables, more constraints) models like urbs.

Requirements

  • Python 2.7 or 3.5 (Anaconda)
  • Pandas (included in Anaconda)
  • xlrd (included in Anaconda)
  • Either (old): Coopr 3.5.8787 (pip install coopr=3.5.8787) or (new) Pyomo 4 (pip install pyomo)
  • GLPK

How to run

Setup

git clone https://github.com/ojdo/pyomo-example.git
cd pyomo-example

MathProg example (bacon.mod)

glpsol -m bacon.mod

Pyomo example (everything else in the folder)

python runme.py

For a better learning experience, one can also launch ipython and execute the lines of runme.py step by step, inspecting the created objects with one eye on the Pyomo Online Documentation:

import coopr.environ
from coopr.opt.base import SolverFactory
import bacon

data = bacon.read_excel('input.xlsx')
model = bacon.create_model(data)
prob = model.create()
optim = SolverFactory('glpk')
result = optim.solve(prob, tee=True)
prob.load(result)

All the work happens in the functions defined within bacon.py.

Copyright

Copyright (C) 2015 ojdo

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/

pyomo-example's People

Contributors

gidden avatar ojdo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pyomo-example's Issues

Performance bottleneck

flow_balance += m.data['actor'].loc[a, 'sink-source']

As I had to learn the hard way in the larger model tum-ens/urbs#140, accessing single values using pandas .loc[...] in a loop is wasteful. Instead convert each parameter to a dictionary with fast access. It won't be noticeable here, but with millions of accesses, accessing DataFrames for single parameter values should be avoided.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.