# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.7 ('base')
#     language: python
#     name: python3
# ---

# +
import xpress as xp
xp.controls.outputlog = 0 # turn off output log

import numpy as np
import pylab as pl
# -

assignment = xp.problem()
n_machines = 4
n_tasks = 4
costs = np.array([[13,	4,	7, 6],
                  [1,	11,	5, 4],
                  [6,	7,	2, 8],
                  [1,	3,	5, 9]]) # Cost of assigning machine i to task j

X = np.array([[xp.var(vartype=xp.binary) for i in range(n_machines)] for j in range(n_tasks)])
assignment.addVariable(X)

# Every task must be performed
[assignment.addConstraint(xp.Sum(X[:, j]) == 1) for j in range(n_tasks)]

# A machine can only be used for one task
[assignment.addConstraint(xp.Sum(X[i]) <= 1) for i in range(n_machines)]

assignment.setObjective(xp.Sum(X*costs))

assignment.solve()

assignment.getSolution(X)

# +
set_covering = xp.problem()
n_sets = 7

costs = np.array([3,5,1,9,2,4,1])

A = np.array([[1, 0, 1, 0, 0, 0, 1],
              [0, 1, 0, 0, 1, 0, 0],
              [1, 1, 0, 1, 0, 0, 0],
              [0, 0, 1, 1, 0, 0, 0],
              [1, 0, 0, 1, 0, 1, 1]])

b = np.ones(5)
# -

x = np.array([xp.var(vartype=xp.binary) for _ in range(n_sets)])
set_covering.addVariable(x)

set_covering.addConstraint(xp.Dot(A, x) >= b)

set_covering.setObjective(xp.Dot(costs, x), sense=xp.minimize)

set_covering.solve()

set_covering.getProbStatusString()

set_covering.getSolution(x), set_covering.getObjVal()

StarcoderdataJupyterScriptsDedupFiltered