Skip to content

About

Repository of optimization functions for research projects, teaching and learning

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

Optimization-Functions

Repository of optimization functions for research projects, teaching and learning

Resources

  • Introduction to Optimization1.

Overview

This repository collects optimization examples for teaching and research. Example Optimization Code/ holds the course examples from Laurent Lessard's Introduction to Optimization course (linked above): Julia/JuMP scripts that the folder notes say were converted from Julia 0.6 to Julia 1.10, and Python ports that use PuLP, CVXPY and GEKKO. Optimization Functions/ holds a Jupyter notebook that introduces linear programming with Google OR-Tools. Cosimulation/ couples PowerWorld Simulator and Julia PowerModels: it exports a solved PowerWorld case to a MATPOWER file, solves an AC power flow on that file in PowerModels, and writes the solution back into PowerWorld.

How it works

flowchart LR
    A["Lessard course examples<br/>(originally Julia 0.6)"] --> B["Julia 1.10 scripts<br/>JuMP with Clp, Cbc, GLPK,<br/>Ipopt, Gurobi"]
    A --> C["Python ports<br/>PuLP, CVXPY, GEKKO"]
    N["Linear_Programming.ipynb<br/>OR-Tools GLOP"] --> R["Printed solutions<br/>and plots"]
    B --> R
    C --> R
    P["PowerWorld .pwb case"] --> M["Cosimulation/main.py<br/>SimAuto via esa"]
    M --> F["MATPOWER .m file"]
    F --> J["PowerModels solve_ac_pf<br/>(Julia via PyJulia)"]
    J --> O["JSON written back to PowerWorld;<br/>bus, gen, branch CSVs"]
    O -.->|"next pass (3 in total)"| M
Loading

The teaching examples are independent scripts. Each one builds a small model from data written in the file, solves it, and prints or plots the result. The Cosimulation loop runs three passes (while count < 3 in main.py).

Repository layout

Folder What it contains Main contributor (git history) Start here
Cosimulation/ PowerWorld to PowerModels loop: main.py and four helper modules Philipellon (5 commits, 2025) main.py
Example Optimization Code/ 20 Julia scripts in Julia 1 - 7, Julia 8 - 16 and Julia 17 - 25; 39 Python ports in Python/ (linear, convex, nonconvex and combinatorial) Philipellon (Python, 72 commits); EricKellerRE (Julia, 19 commits) Julia 1 - 7/Top Brass.jl or Python/Linear Programming/House.py
Optimization Functions/ Linear programming/: an OR-Tools linear programming notebook and a short Intro.md Jonathan Snodgrass (1 commit, 2022) Linear programming/Linear_Programming.ipynb

Root files and data:

Path What it is
README.md This file
requirements.txt Python packages imported by the code (added 2026-09-27)
LICENSE MIT License, copyright 2022 Research Group of Professor Tom Overbye
.gitignore, .idea/ Git ignore rules and JetBrains IDE project settings
Example Optimization Code/Julia 8 - 16/uy_data.csv 100 rows of (u, y) pairs for the moving-average examples; no header row

Setup

  1. Tools:
    • Julia examples: Julia 1.10 or later. There is no Project.toml, so install the packages in step 2 by hand.
    • Cosimulation/: Windows, PowerWorld Simulator with a SimAuto licence, and Julia (the folder ReadMe names Julia 1.11.4).
    • Gurobi, installed and licensed, for Hovercraft 1-D.jl, Hovercraft.jl, Norm of 1-D Datasets.jl and Polynomial Regression v0.jl (Julia 8 - 16). Data Norms.py and data Norms1.py (Python/Convex Programming) call Gurobi through CVXPY (solver=cp.GUROBI) and need only the gurobipy package (step 6); its bundled size-limited licence covers these small models.
  2. Julia packages, taken from the using lines. In the Julia REPL:
    using Pkg
    Pkg.add(["JuMP", "Clp", "Cbc", "GLPK", "HiGHS", "Ipopt", "ECOS", "SCS",
             "NonlinearSolve", "StaticArrays", "NamedArrays", "CSV", "DataFrames", "PyPlot"])
    Pkg.add("Gurobi")                 # needs Gurobi installed and licensed
    Pkg.add(["PowerModels", "JSON"])  # Cosimulation/ only
    LinearAlgebra, Random and Statistics are Julia standard libraries. PyPlot.jl draws with Python matplotlib through PyCall.
  3. Python 3.9 to 3.12 (the range the Cosimulation ReadMe gives; nothing else in the repository states a version).
  4. From the repository root:
    python -m venv .venv
    .venv\Scripts\activate
    pip install -r requirements.txt
    
    On macOS or Linux, delete the esa line first: esa depends on pywin32, which installs only on Windows (and Cosimulation/ needs Windows anyway).
  5. For Cosimulation/ only, set up PyJulia once: python -c "import julia; julia.install()".
  6. Optional: pip install gurobipy for Data Norms.py and data Norms1.py (see the commented line in requirements.txt).
  7. Edit the hard-coded paths before running:
    • Cosimulation/main.py lines 15-21 (case and output files) and line 31 (julia_bindir), and Cosimulation/PM_solver.py line 21 (JULIA_BINDIR).
    • Example Optimization Code/Python/Convex Programming/moving_average1.py, moving_average2.py and moving_average3.py (the pd.read_csv line) should point to Example Optimization Code/Julia 8 - 16/uy_data.csv.

Running

Julia examples, from the script's own folder (Moving Average.jl reads ./uy_data.csv):

cd "Example Optimization Code\Julia 1 - 7"
julia "SailCo Problem.jl"

Scripts that plot with PyPlot (Hovercraft 1-D.jl, Hovercraft.jl, Moving Average.jl, Polynomial Regression v0.jl, Regression.jl, Structural Optimization.jl, Uncapacitated Facility Location problem.jl) are best run in the Julia REPL so the figures are shown:

julia> cd("Example Optimization Code/Julia 8 - 16")
julia> include("Hovercraft.jl")

Python examples, from the script's own folder:

cd "Example Optimization Code\Python\Linear Programming"
python House.py

Notebook, from the repository root:

jupyter notebook "Optimization Functions/Linear programming/Linear_Programming.ipynb"

Cosimulation, after editing the paths (step 7):

cd Cosimulation
python main.py

Inputs and outputs

  • Teaching examples: the data are written in each script, except Moving Average.jl and moving_average1-3.py (read uy_data.csv) and portfolio2-4.py (read folio_mean.csv and folio_cov.csv, which are not in the repository). Results are printed or plotted. Uncapacitated Facility Location problem.jl also saves solver_comparison.pdf in the working folder.
  • Notebook: no input files; prints the solutions of two small LPs.
  • Cosimulation: input is a PowerWorld .pwb case (not in the repository). Outputs are the same .pwb case saved in place, a MATPOWER .m file, a JSON file with the PowerModels solution, gen_map.json, and branch_data.csv, bus_data.csv and gen_data.csv. All locations are set in main.py.

Known issues

  • Cosimulation/PM_solver.py runs solve_ac_pf (an AC power flow, after setting each generator's P and Q limits to its current output), not an OPF, although the Cosimulation ReadMe and the output name opf_results1.json say OPF. The paths in main.py and PM_solver.py are hard-coded. Details are in Cosimulation/ReadMe.md.
  • moving_average1.py, moving_average2.py and moving_average3.py read C:/Users/Owner/Downloads/uy_data.csv.
  • portfolio2.py, portfolio3.py and portfolio4.py need folio_mean.csv and folio_cov.csv, which are not in the repository.
  • Julia 8 - 16/Regression.jl uses uopt on line 34 before it is assigned (line 68), so running the whole file stops with an UndefVarError.
  • Julia 1 - 7/Chebyshev Center.jl writes its constraints as A[i,:]'.*x .+ r.*normalize!(A[i,:]) .<= b[i]. The element-wise product gives a 3 x 3 block of constraints per row, and normalize! returns a unit vector, not the norm, so the script does not solve the Chebyshev-centre LP. Python/Linear Programming/chebyshev center.py uses the standard form A[i, :] @ x + r * np.linalg.norm(A[i, :]) <= b[i].
  • Python/Nonconvex and combinatorial models/Cutting Pipe.py declares integer variables but keeps GEKKO's default solver setting, IPOPT (SOLVER = 3), which treats integer variables as continuous. Add m.options.SOLVER = 1 (APOPT) before m.solve for an integer solution. (GEKKO's local Linux build has no IPOPT and falls back to APOPT on its own.)

Related repositories

  • GMD-Extended-Team: its folder Mia/Nathan's code/ has byte-identical copies of Cosimulation/convert_to_Matpower_files.py and Cosimulation/gen_mapping.py, and different versions of main.py, PM_solver.py and PW_update.py.

Contributors

From the git history:

  • Philipellon: 79 commits
  • EricKellerRE: 19 commits
  • Jonathan Snodgrass: 3 commits (2 as "Jonathan Snodgrass", 1 as "jsnodgrass2")

Status

  • First commit 2022-08-30; last code commit 2025-12-03.
  • Optimization Functions/: one commit on 2022-08-30 ("Migrating from TAMU github server"); no changes since. The original README text dates from the same day.
  • Example Optimization Code/: Julia scripts committed 2024-09-16 to 2024-12-11; Python scripts 2024-09-10 to 2024-12-10 (the Python/ReadMe.txt note was last edited 2025-01-13).
  • Cosimulation/: five commits, all on 2025-12-03. This is the most recent work in the repository.

Footnotes

  1. https://laurentlessard.com/teaching/524-intro-to-optimization ↩

About

Repository of optimization functions for research projects, teaching and learning

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages