This template demonstrates how to solve a Mixed Integer Programming problem using the commercial solver FICO Xpress.
To solve a Mixed Integer Problem (MIP) is to optimize a linear objective function of many variables, subject to linear constraints. We demonstrate this by solving the knapsack problem using the Python interface.
Knapsack is a classic combinatorial optimization problem. Given a collection of items with a value and weight, our objective is to maximize the total value without exceeding the weight capacity of the knapsack.
The input defines a number of items which have an id to identify the item, a weight and a value. Additionally there is a weight capacity.
The most important files created are main.py
and input.json
.
main.py
implements a MIP knapsack solver.input.json
is a sample input file.
Follow these steps to run locally.
-
The packages listed in the
requirements.txt
will get bundled with the app as defined in theapp.yaml
manifest. When working locally, make sure that these are installed as well:pip3 install -r requirements.txt
-
Run the command below to check that everything works as expected:
python3 main.py -input input.json -output output.json -duration 30
-
A file
output.json
should have been created with the optimal knapsack solution.
Pre-requisites: Docker needs to be installed.
To run the application locally in the same docker image as the one used on the Nextmv Cloud, you can use the following command:
cat input.json | docker run -i --rm \
-v $(pwd):/app ghcr.io/nextmv-io/runtime/python:3.11 \
sh -c 'pip install -r requirements.txt > /dev/null && python3 /app/main.py'
You can also debug the application by running it in a Dev Container. This
workspace recommends to install the Dev Container extension for VSCode. If you
have the extension installed, you can open the workspace in a container by using
the command Dev Containers: Reopen in Container
.
- Open
main.py
and read through the comments to understand the model. - Further documentation, guides, and API references about custom modeling and deployment can also be found on our blog and on our documentation site.
- Need more assistance? Send us an email!