Xpress Python examples
Examples of using Xpress from Python
|
| Using NumPy arrays to create variables: Using NumPy arrays
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Use NumPy arrays for creating a 3-dimensional array of variables, then use it to create a model. |
| File(s): |
example_array.py |
|
|
| Visualize the BB tree: Using the newnode callback
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Shows how to visualize the BB tree of a problem after (partially) solving it. |
| File(s): |
example_bbtree.py |
|
|
| Irreducible Infeasible Sets: Using Irreducible Infeasible Sets
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Shows how to analyze an infeasible problem by retrieving an irreducible infeasible subset |
| File(s): |
example_infeasible.py |
|
|
| Loading a problem: Loading a problem directly
|
| |
|
| Using Python model objects to build a problem: Modelling using Python objects
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Demonstrate how variables, or arrays thereof, and constraints, or arrays of constraints, can be added into a problem. Prints
the solution and all attributes/controls of the problem.
|
| File(s): |
example_modeling.py |
|
|
| Changing the optimization problem: Changes to a problem
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Changing an optimization problem using the Xpress Python interface. |
| File(s): |
small_change.py |
|
|
| Extending a problem: Extending a problem
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Construct a problem using addVariable and addConstraint, then use the Xpress API routines to amend the problem with rows and
quadratic terms.
|
| File(s): |
example_modify.py |
|
|
| Using NumPy and Xpress: Using NumPy and Xpress
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
An example of printing a matrix of random numbers and a problem formulation that uses the xpress.Dot() operator to formulate
constraints simply.
Note that the NumPy dot operator is not suitable here as the result is an expression in the Xpress variables.
|
| File(s): |
example_numpy1.py, example_numpy2.py |
|
|
| Finding an LP subsystem with as many constraints as possible:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Given an infeasible LP, find the feasible subsystem of constraints of maximum cardinality. |
| File(s): |
example_phase1.py |
|
|
| Basis and Stability: Basis handling and sensitivity methods
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
We solve a simple 2x2 LP problem to optimality,
which serves as a showcase for basis handling methods and
sensitivity analysis.
|
| File(s): |
example_basis_sensitivity.py |
|
|
| Solving a quadratically constrained problem: Building quadratic expressions
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Solve a quadratically constrained problem |
| File(s): |
example_qcqp.py |
|
|
| Solving a nonconvex quadratic problem: Building quadratic expressions
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Solve a nonconvex quadratic problem |
| File(s): |
example_quadnonconvex.py |
|
|
| Solving a quadratically problem: Building quadratic expressions
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Solve a quadratic problem |
| File(s): |
example_quadratic.py |
|
|
| Repeatedly solving a problem: Solving a problem multiple times
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Reads a problem, solves it, then adds a constraint and re-solves it |
| File(s): |
example_resolve.py |
|
|
| Using indicators: Model with indicators
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Construct a problem from scratch with variables of various types. Adds indicator constraints and
shows how to retrieve such data once it has been added to the problem using the API functions
|
| File(s): |
example_indicators.py |
|
|
| Using special ordered sets: Model with special ordered sets
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Construct a problem from scratch with variables of various types. Adds Special Ordered Sets (SOSs) and
shows how to retrieve such data once it has been added to the problem using the API functions
|
| File(s): |
example_sos.py |
|
|
| The travelling salesman problem: Using Xpress callbacks
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Retrieves an example fromn https://www.math.uwaterloo.ca/tsp/world/countries.html
and creates a corresponding TSP instance, then solves it using the Xpress Optimizer
library with the appropriate callback. Once the optimization is over
(i.e. the time limit is reached or we find an optimal solution) the
optimal tour is displayed using matplotlib.
|
| File(s): |
example_tsp.py |
|
|
| Solving a TSP using NumPy: Using Xpress callbacks
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
A randomly generated TSP problem is modeled using
NumPy vectors and matrices and solved using the Optimizer's
libraries and callback functions.
|
| File(s): |
example_tsp_numpy.py |
|
|
| Writing and reading problem files: Writing and reading a problem to disk
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a few variables, then build a problem and save it to a file.
Re-read that file into a new problem and solve it
|
| File(s): |
example_write_read.py |
|
|
| The feasiblity pump: Writing and reading a problem to disk
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Feasibility pump (prototype) using the Xpress Python interface. |
| File(s): |
feas_pump.py |
|
|
| Knapsack problem: MIP problem with binary variables
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Example of a knapsack problem formulated with the Xpress Python interface. |
| File(s): |
knapsack.py |
|
|
| The n-queens problem: Puzzle modeling
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
The n queens: place n queens on an nxn chessboard so that none of them can be eaten in one move. |
| File(s): |
n_queens.py |
|
|
| Min-cost-flow problem : Modelling a graph problem
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
solving a min-cost-flow problem using the Xpress Python interface. |
| File(s): |
netflow.py |
|
|
| Solving Sudoku: Puzzle modeling
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
Place numbers from 1 to 9 into a 9x9 grid such that no number repeats in any row, in any column, and in any 3x3 sub-grid. |
| File(s): |
sudoku.py |
|
|
| Comparing Matrices: Compare two optimization problems
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Load the coefficient matrices of two optimization problems saved in MPS or LP format using the Xpress Optimizer libraries
and compare each line by line using SciPy's matrix routines.
|
| File(s): |
compare_matrices.py |
|
|
| Multicommodity flow problem: Solve a multicommodity flow minimum cost optimization problem on a randomly created graph
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a random directed graph using NumPy's random functions and then model and solve a minimum-cost multicommodity flow
problem constructed on that graph.
|
| File(s): |
example_multicommodity_flow.py |
|
|
| Find largest-area inscribed polygon:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Given n, find the n-sided polygon of largest area inscribed in the unit circle. |
| File(s): |
polygon.py |
|
|
| Read problem data into matrix and vectors:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Obtain coefficient matrix, objective coefficients, and constraints' right-hand sides for a given problem. |
| File(s): |
example_getmatrix.py |
|
|
| Solve a nonconvex MIQCQP problem:
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
Reformulate a MIQCQP into a MILP and add callbacks to enforce quadratic feasibility of the nonconvex constraints. |
| File(s): |
miqcqp_solver.py |
|
|
| Solve a simple MIP using Benders decomposition:
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
Solve a simple MIP using Benders decomposition. Courtesy of Georgios Patsakis (UC Berkeley, Amazon) and Richard L.-Y. Chen
(Amazon).
|
| File(s): |
benders_decomp.py |
|
|
| Create a problem with piecewise linear functions:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the modelling construct xpress.pwl for creating piecewise linear functions.
|
| File(s): |
piecewise_linear.py |
|
|
| Use the API to create a model with piecewise linear functions:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the API function problem.addpwlcons to create piecewise linear functions. The resulting model is equivalent to piecewise_linear.py.
|
| File(s): |
piecewise_linear2.py |
|
|
| Create a problem with general constraints that use operator abs:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the modelling methods abs for creating general constraints.
|
| File(s): |
general_constraints_abs.py |
|
|
| Create a problem with general constraints with the operator abs by using the API:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the API function problem.addgencons for creating general constraints.
|
| File(s): |
general_constraints_abs_api.py |
|
|
| Create a problem with general constraints that use operator max:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the modelling methods max for creating general constraints.
|
| File(s): |
general_constraints_max.py |
|
|
| Create a problem with general constraints with operator max by using the API:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the API function problem.addgencons for creating max general constraints.
|
| File(s): |
general_constraints_max_api.py |
|
|
| Create a problem with logical constraints:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the modelling methods max for creating general constraints.
|
| File(s): |
general_constraints_logic.py |
|
|
| Create a problem with general constraints with logic operators by using the API:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a simple problem using the API function problem.addgencons for creating logical (AND and OR) general constraints.
|
| File(s): |
general_constraints_logic_api.py |
|
|
| Create an iterative algorithm cutting stock problem:
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
Use the modeling features to create an iterative solver for a cutting stock problem. |
| File(s): |
cuttingstock.py |
|
|
| Maximize the sum of logistic curves subject to linear and piecewise linear constraints:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Approximate the logistic curves using piecewise linear functions. |
| File(s): |
maxSumLogistic.py |
|
|
| Transportation problem with piecewise-linear costs:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Model a transportation problem where the cost are modeled using the xpress.problem.addpwlcons function.
|
| File(s): |
pwl_transp.py |
|
|
| Modeling Satisfiability (SAT) problems with MIP:
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
A rudimentary SAT solver that translates a Satisfiability (SAT) problem into a MIP and solves it. |
| File(s): |
example_gencons_sat.py |
|
|
| Modeling PseudoBoolean Optimization problems with MIP:
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
A solver of PseudoBoolean optimization problems (PBO) that translates a problem into a MIP and solves it. |
| File(s): |
example_gencons_pbo.py |
|
|
| Re-solving problem using the Barrier method's warm start:
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
Solve one problem and use its solution to warm-start the solve of a second, slightly different, problem. |
| File(s): |
example_barrier_warmstart.py |
|
|
| Using the tuner functions in the Python interface:
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Read a MIPLIB2010 problem instance, then apply the Optimizer's tuner both with default tuner method and with a user-defined
method file.
|
| File(s): |
tuner_example.py |
|
|
| Multi-objective knapsack problem: Multi-objective MIP problem with binary variables
|
| |
|
| Type: |
Production planning |
| Rating: |
2 (easy-medium) |
| Description: |
Example of a multi-objective knapsack problem formulated with the Xpress Python interface. |
| File(s): |
example_multiobjknapsack.py |
|
|
| Goal programming: Lexicographic goal programming using the Xpress multi-objective API
|
| |
|
| Type: |
Production planning |
| Rating: |
3 (intermediate) |
| Description: |
This example tries to construct a production plan which meets four different
goals in order of priority.
|
| File(s): |
example_goalprog.py |
|
|
| Markowitz portfolio optimization: Multi-objective quadratic optimization
|
| |
|
| Type: |
Portfolio optimization |
| Rating: |
3 (intermediate) |
| Description: |
In Markowitz portfolio optimization there are two objectives: to maximize reward
while minimizing risk (i.e. variance). This example plots several points on the
optimal frontier using a blended multi-objective approach, and shows that a point
computed using a lexicographic approach also lies on this frontier.
|
| File(s): |
example_markowitz.py |
|
|
| Basic LP tasks: problem statement and solving; solution analysis: LP solving, modeling variables and constraints, printing the solution
|
| |
|
| Type: |
Production planning |
| Rating: |
1 (simple) |
| Description: |
Small, introductory problems to modeling with Python. |
| File(s): |
chess.py, chess2.py |
|
|
| Network problem: transport from depots to customers: LP solving, modeling variables and constraints
|
| |
|
| Type: |
Transportation |
| Rating: |
1 (simple) |
| Description: |
Network problem: transport from depots to customers. |
| File(s): |
trans.py |
| Data file(s): |
trans_data.py |
|
|
| Blend: A model for mineral blending: simple LP problem, formulation of blending constraints
|
| |
|
| Type: |
Blending |
| Rating: |
1 (simple) |
| Description: |
Several ores are blended to a final product that must
have a certain quality ('grade'). We wish to determine
the quantity of every ore to be used in the blend with
the objective to maximize the total profit (calculated
as sales revenues - raw material cost).
|
| File(s): |
blend.py, blend2.py |
| Data file(s): |
blend_data.py |
|
|
| Basic MIP tasks: binary variables; logic constraints: MIP solving, binary variables, index set types, logic constraints
|
| |
|
| Coco: The Coco productional planning problem: LP problem, formulation of resource constraints and
material balance constraints, formatted solution printing
|
| |
|
| Type: |
Production planning |
| Rating: |
2 (easy-medium) |
| Description: |
The company Coco has two plants that can produce two types
of cocoa powder. The plant capacities are limited.
It is possible to store raw materials and finished product
from one time period to the next. Raw material prices,
sales revenues, and the maximum amount that may be sold
depend on the time period. Raw material storage capacity
is limited.
Storing product between time periods incurs storage costs.
Some product is held in stock at the beginning of the
planning period.
The objective function of maximizing the total profit is
to maximize the sales revenues, minus the cost of production,
buying raw material, and storing finished products and raw
material.
|
| File(s): |
coco.py |
|
|
| Catenary: Determine chain shape: QCQP problem
|
| |
|
| Type: |
Nonlinear |
| Rating: |
2 (easy-medium) |
| Description: |
Find the shape of a hanging chain by minimising its potential energy.
The problem is formulated as a QCQP problem (linear objective, convex quadratic constraints).
|
| File(s): |
catenary.py |
|
|
| Pplan: A project planning problem: Formulation of resource use profiles
|
| |
|
| Type: |
Project planning |
| Rating: |
3 (intermediate) |
| Description: |
Over the next 6 months we have three projects which can
be done. Each of these projects has a profile of manpower
requirements over its lifetime, and a benefit which accrues
each month when the project has been completed.
Our problem is to determine when each project is to start,
subject to the constraint that in no month can we try to
use more manpower than is available.
|
| File(s): |
pplan.py |
|
|
| Firestns: A set-covering model for emergency service provision: Solve a MIP problem
|
| |
|
| Type: |
Set covering |
| Rating: |
1 (simple) |
| Description: |
Minimize the total number of firestations that are required to service a set of towns within a specified permissible travel
time.
|
| File(s): |
fstns.py |
|
|
| Solving a quadratically constrained problem: Solve a nonlinear problem
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Define the well-known Rosenbrock function and minimize it. |
| File(s): |
rosenbrock.py |
|
|
| Solve a polynomial optimization problem: Modeling a polynomial optimization problem
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Create a random polynomial of degree k using the Dot operator and find its minimum using the Nonlinear solver. |
| File(s): |
polynomial_opt.py |
|
|
| Modeling with user functions: Modeling with user functions
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Shows how to create and solve a nonlinear problem whose constraints and objective functions are defined as user functions. |
| File(s): |
example_userfunc.py |
|
|
| Solve a nonconvex nonlinear problem from MINLPlib with a local or global solver: Solving with local or global solvers
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Shows how to model a general nonlinear problem and solve it with a local or global solver. |
| File(s): |
inscribedsquare.py |
|
|
| Implementing a branching rule using branch objects: Demonstrate the Xpress change branch object callback
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Shows how to set up a branching rule branching on the most violated Integer/Binary variable. |
| File(s): |
mostviolated.py |
|
|
|