Skip to content

Set Cover Problem

View on GitHub Experiment in the IDE

Introduction

The set cover problem [1] represents a well-known problem in the fields of combinatorics, computer science, and complexity theory. It is an NP-complete problems.

The problem presents us with a universal set, \(\displaystyle U\), and a collection \(\displaystyle S\) of subsets of \(\displaystyle U\). The goal is to find the smallest possible subfamily, \(\displaystyle C \subseteq S\), whose union equals the universal set.

Formally, let's consider a universal set \(\displaystyle U = {1, 2, ..., n}\) and a collection \(\displaystyle S\) containing \(m\) subsets of \(\displaystyle U\), \(\displaystyle S = {S_1, ..., S_m}\) with \(\displaystyle S_i \subseteq U\). The challenge of the set cover problem is to find a subset \(\displaystyle C\) of \(\displaystyle S\) of minimal size such that \(\displaystyle \bigcup_{S_i \in C} S_i = U\).

Solving with the Classiq platform

We go through the steps of solving the problem with the Classiq platform, using QAOA algorithm [2]. The solution is based on defining a pyomo model for the optimization problem we would like to solve.

from typing import cast

import networkx as nx
import numpy as np
import pyomo.core as pyo
from IPython.display import Markdown, display
from matplotlib import pyplot as plt

Building the Pyomo model from a graph input

We proceed by defining the pyomo model that will be used on the Classiq platform, using the mathematical formulation defined above:

import itertools
from typing import List

import pyomo.core as pyo


def set_cover(sub_sets: List[List[int]]) -> pyo.ConcreteModel:
    entire_set = set(itertools.chain(*sub_sets))
    n = max(entire_set)
    num_sets = len(sub_sets)
    assert entire_set == set(
        range(1, n + 1)
    ), f"the union of the subsets is {entire_set} not equal to range(1, {n + 1})"

    model = pyo.ConcreteModel()
    model.x = pyo.Var(range(num_sets), domain=pyo.Binary)

    @model.Constraint(entire_set)
    def independent_rule(model, num):
        return sum(model.x[idx] for idx in range(num_sets) if num in sub_sets[idx]) >= 1

    model.cost = pyo.Objective(expr=sum(model.x.values()), sense=pyo.minimize)

    return model

The model contains:

  • Binary variable for each subset (model.x) indicating if it is included in the sub-collection.

  • Objective rule – the size of the sub-collection.

  • Constraint – the sub-collection covers the original set.

sub_sets = sub_sets = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [6, 7],
    [8, 9, 10],
    [1, 6, 8],
    [3, 7, 9],
    [4, 7, 10],
    [2, 5, 8],
]

set_cover_model = set_cover(sub_sets)
set_cover_model.pprint()
2 Set Declarations
    independent_rule_index : Size=1, Index=None, Ordered=False
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :   10 : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    x_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    8 : {0, 1, 2, 3, 4, 5, 6, 7}

1 Var Declarations
    x : Size=8, Index=x_index
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          0 :     0 :  None :     1 : False :  True : Binary
          1 :     0 :  None :     1 : False :  True : Binary
          2 :     0 :  None :     1 : False :  True : Binary
          3 :     0 :  None :     1 : False :  True : Binary
          4 :     0 :  None :     1 : False :  True : Binary
          5 :     0 :  None :     1 : False :  True : Binary
          6 :     0 :  None :     1 : False :  True : Binary
          7 :     0 :  None :     1 : False :  True : Binary

1 Objective Declarations
    cost : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : minimize : x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7]

1 Constraint Declarations
    independent_rule : Size=10, Index=independent_rule_index, Active=True
        Key : Lower : Body               : Upper : Active
          1 :   1.0 :        x[0] + x[4] :  +Inf :   True
          2 :   1.0 : x[0] + x[1] + x[7] :  +Inf :   True
          3 :   1.0 : x[0] + x[1] + x[5] :  +Inf :   True
          4 :   1.0 : x[0] + x[1] + x[6] :  +Inf :   True
          5 :   1.0 :        x[1] + x[7] :  +Inf :   True
          6 :   1.0 :        x[2] + x[4] :  +Inf :   True
          7 :   1.0 : x[2] + x[5] + x[6] :  +Inf :   True
          8 :   1.0 : x[3] + x[4] + x[7] :  +Inf :   True
          9 :   1.0 :        x[3] + x[5] :  +Inf :   True
         10 :   1.0 :        x[3] + x[6] :  +Inf :   True

5 Declarations: x_index x independent_rule_index independent_rule cost

Setting Up the Classiq Problem Instance

In order to solve the Pyomo model defined above, we use the Classiq combinatorial optimization engine. For the quantum part of the QAOA algorithm (QAOAConfig) - define the number of repetitions (num_layers):

from classiq import construct_combinatorial_optimization_model
from classiq.applications.combinatorial_optimization import OptimizerConfig, QAOAConfig

qaoa_config = QAOAConfig(num_layers=3, penalty_energy=10)

For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (max_iteration) and the \(\alpha\)-parameter (alpha_cvar) for running CVaR-QAOA, an improved variation of the QAOA algorithm [3]:

optimizer_config = OptimizerConfig(max_iteration=60, alpha_cvar=0.7)

Lastly, we load the model, based on the problem and algorithm parameters, which we can use to solve the problem:

qmod = construct_combinatorial_optimization_model(
    pyo_model=set_cover_model,
    qaoa_config=qaoa_config,
    optimizer_config=optimizer_config,
)

We also set the quantum backend we want to execute on:

from classiq import set_execution_preferences
from classiq.execution import ClassiqBackendPreferences, ExecutionPreferences

backend_preferences = ExecutionPreferences(
    backend_preferences=ClassiqBackendPreferences(backend_name="simulator")
)

qmod = set_execution_preferences(qmod, backend_preferences)
from classiq import write_qmod

write_qmod(qmod, "set_cover")

Synthesizing the QAOA Circuit and Solving the Problem

We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:

from classiq import show, synthesize

qprog = synthesize(qmod)
show(qprog)
Opening: https://platform.classiq.io/circuit/11fd03ea-49d7-4ea9-a772-fca49f53953e?version=0.41.0.dev39%2B79c8fd0855

We now solve the problem by calling the execute function on the quantum program we have generated:

from classiq import execute

res = execute(qprog).result()

We can check the convergence of the run:

from classiq.execution import VQESolverResult

vqe_result = res[0].value
vqe_result.convergence_graph

png

Optimization Results

We can also examine the statistics of the algorithm:

import pandas as pd

from classiq.applications.combinatorial_optimization import (
    get_optimization_solution_from_pyo,
)

solution = get_optimization_solution_from_pyo(
    set_cover_model, vqe_result=vqe_result, penalty_energy=qaoa_config.penalty_energy
)
optimization_result = pd.DataFrame.from_records(solution)
optimization_result.sort_values(by="cost", ascending=True).head(5)
probability cost solution count
118 0.001 4.0 [0, 0, 0, 0, 1, 1, 1, 1] 1
986 0.001 14.0 [0, 0, 0, 0, 1, 1, 1, 1] 1
524 0.001 14.0 [0, 0, 0, 0, 1, 1, 1, 1] 1
142 0.001 15.0 [0, 0, 0, 1, 1, 1, 1, 1] 1
37 0.001 15.0 [1, 1, 1, 1, 1, 0, 0, 0] 1

And the histogram:

optimization_result.hist("cost", weights=optimization_result["probability"])
array([[<Axes: title={'center': 'cost'}>]], dtype=object)

png

Let us plot the solution:

best_solution = optimization_result.solution[optimization_result.cost.idxmin()]
print(
    f"Quantum Solution: num_sets={int(sum(best_solution))}, sets={[sub_sets[i] for i in range(len(best_solution)) if best_solution[i]]}"
)
Quantum Solution: num_sets=4, sets=[[1, 6, 8], [3, 7, 9], [4, 7, 10], [2, 5, 8]]

Lastly, we can compare to the classical solution of the problem:

from pyomo.opt import SolverFactory

solver = SolverFactory("couenne")
solver.solve(set_cover_model)

set_cover_model.display()
Model unknown

  Variables:
    x : Size=8, Index=x_index
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          0 :     0 :   1.0 :     1 : False : False : Binary
          1 :     0 :   1.0 :     1 : False : False : Binary
          2 :     0 :   1.0 :     1 : False : False : Binary
          3 :     0 :   1.0 :     1 : False : False : Binary
          4 :     0 :   0.0 :     1 : False : False : Binary
          5 :     0 :   0.0 :     1 : False : False : Binary
          6 :     0 :   0.0 :     1 : False : False : Binary
          7 :     0 :   0.0 :     1 : False : False : Binary

  Objectives:
    cost : Size=1, Index=None, Active=True
        Key  : Active : Value
        None :   True :   4.0

  Constraints:
    independent_rule : Size=10
        Key : Lower : Body : Upper
          1 :   1.0 :  1.0 :  None
          2 :   1.0 :  2.0 :  None
          3 :   1.0 :  2.0 :  None
          4 :   1.0 :  2.0 :  None
          5 :   1.0 :  1.0 :  None
          6 :   1.0 :  1.0 :  None
          7 :   1.0 :  1.0 :  None
          8 :   1.0 :  1.0 :  None
          9 :   1.0 :  1.0 :  None
         10 :   1.0 :  1.0 :  None
classical_solution = [
    pyo.value(set_cover_model.x[i]) for i in range(len(set_cover_model.x))
]
print(
    f"Classical Solution: num_sets={int(sum(classical_solution))}, sets={[sub_sets[i] for i in range(len(classical_solution)) if classical_solution[i]]}"
)
Classical Solution: num_sets=4, sets=[[1, 2, 3, 4], [2, 3, 4, 5], [6, 7], [8, 9, 10]]

References

[1]: Set Cover Problem (Wikipedia)

[2]: Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. "A quantum approximate optimization algorithm." arXiv preprint arXiv:1411.4028 (2014).

[3]: Barkoutsos, Panagiotis Kl, et al. "Improving variational quantum optimization using CVaR." Quantum 4 (2020): 256.