HLPL / VerTeX Modelling Language Documentation

← lessons

Capstone — a project portfolio

Two constraints, one binary objective.

Chapter: XV — Capstone · lesson id capstone-portfolio · practice lesson


You have five candidate projects. Each has a value v_p, a cost c_p, and a team-size demand t_p. You have a budget of 15 and a team capacity of 7. Pick projects to maximise total value.

project 1: v=10, c=5, t=2 project 2: v= 7, c=3, t=1 project 3: v= 8, c=4, t=1 project 4: v= 5, c=2, t=1 project 5: v=12, c=6, t=3

Write the model yourself. The check below confirms your optimum equals 32.

Predict before running

Five projects, budget and team constraint. About how many will the solver pick?

Starter code

model portfolio
set P = {1, 2, 3, 4, 5}
const v = {10, 7, 8, 5, 12}, forall p in P
const c = {5, 3, 4, 2, 6}, forall p in P
const t = {2, 1, 1, 1, 3}, forall p in P
# TODO: declare bin x indexed over P, the budget constraint (sum c*x <= 15),
#       the team constraint (sum t*x <= 7), and the max sum v*x objective.
end

import elytica
def main():
  elytica.init_model("portfolio")
  elytica.run_model("portfolio")
  for i in [1,2,3,4,5]:
    print("x"+str(i), "=", elytica.get_variable_value("portfolio", "x"+str(i)))
  print("obj =", elytica.get_best_primal_bound("portfolio"))
  return 0

Hint

Five binary x_p variables. Two ≤ constraints (budget and team). One max sum objective.

What counts as correct

Solution

model portfolio
set P = {1, 2, 3, 4, 5}
const v = {10, 7, 8, 5, 12}, forall p in P
const c = {5, 3, 4, 2, 6}, forall p in P
const t = {2, 1, 1, 1, 3}, forall p in P
bin x, forall p in P
constr sum_{p in P}{c_{p} * x_{p}} <= 15
constr sum_{p in P}{t_{p} * x_{p}} <= 7
max sum_{p in P}{v_{p} * x_{p}}
end

import elytica
def main():
  elytica.init_model("portfolio")
  elytica.run_model("portfolio")
  for i in [1,2,3,4,5]:
    print("x"+str(i), "=", elytica.get_variable_value("portfolio", "x"+str(i)))
  print("obj =", elytica.get_best_primal_bound("portfolio"))
  return 0

Mastery variant

Five new projects with different values, costs, and teams. Pick the best portfolio.

model portfolio_v
set P = {1, 2, 3, 4, 5}
const v = {8, 6, 9, 4, 11}, forall p in P
const c = {4, 3, 5, 2, 5}, forall p in P
const t = {2, 2, 2, 1, 3}, forall p in P
# TODO: bin x indexed over P, budget (sum c*x <= 13), team (sum t*x <= 8),
#       and max sum v*x.
end

import elytica
def main():
  elytica.init_model("portfolio_v")
  elytica.run_model("portfolio_v")
  for i in [1,2,3,4,5]:
    print("x"+str(i), "=", elytica.get_variable_value("portfolio_v", "x"+str(i)))
  print("obj =", elytica.get_best_primal_bound("portfolio_v"))
  return 0

Read this through MCP: read with path: "lessons/capstone-portfolio", or resource hlpl://lessons/capstone-portfolio.