Project: Моделирование труднорешаемых задач
Views: 22Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204Kernel: .venv
MINIMUM EXACT COVER / Покрытие множеств минимальным объемом
In [1]:
Постановка задачи
Представим, что мы имеем конечное множество «U» (universum) и «S» — список подмножеств множества «U».
Покрытием называют семейство наименьшей мощности, объединением которых является . В случае постановки вопроса о разрешении на вход подаётся пара и целое число k; вопросом является существование покрывающего множества мощности k (или менее).
Оптимизационная версия задачи, '''минимальное покрытие множеств''', задаёт вопрос о минимальном объеме покрытия, измеряемого не в числе множеств из «S» покрывающих «U», а в числе элементов в этих подмножествах
In [2]:
In [3]:
Реализация в Pyomo
In [4]:
In [5]:
['01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19']
In [6]:
[{'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17'},
{'01', '02', '04', '06', '08', '10', '12', '14', '16', '18'},
{'10'},
{'11', '14'},
{'02', '08', '18'},
{'16', '17', '18', '19'},
{'01', '07', '08', '11', '17'},
{'01', '05', '07', '12', '18', '19'},
{'04', '06', '12', '14', '18', '19'},
{'02', '04', '05', '09', '10', '15', '18'},
{'04', '06', '07', '09', '11', '17', '18', '19'}]
In [7]:
1 Var Declarations
x : Size=11, Index={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
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
8 : 0 : None : 1 : False : True : Binary
9 : 0 : None : 1 : False : True : Binary
10 : 0 : None : 1 : False : True : Binary
1 Objective Declarations
set_count : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 17*x[0] + 10*x[1] + x[2] + 2*x[3] + 3*x[4] + 4*x[5] + 5*x[6] + 6*x[7] + 6*x[8] + 7*x[9] + 8*x[10]
1 Constraint Declarations
каждый_элемент_покрыт_хотябы_одним_множеством : Size=19, Index={01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, Active=True
Key : Lower : Body : Upper : Active
01 : 1.0 : x[0] + x[1] + x[6] + x[7] : +Inf : True
02 : 1.0 : x[0] + x[1] + x[4] + x[9] : +Inf : True
03 : 1.0 : x[0] : +Inf : True
04 : 1.0 : x[0] + x[1] + x[8] + x[9] + x[10] : +Inf : True
05 : 1.0 : x[0] + x[7] + x[9] : +Inf : True
06 : 1.0 : x[0] + x[1] + x[8] + x[10] : +Inf : True
07 : 1.0 : x[0] + x[6] + x[7] + x[10] : +Inf : True
08 : 1.0 : x[0] + x[1] + x[4] + x[6] : +Inf : True
09 : 1.0 : x[0] + x[9] + x[10] : +Inf : True
10 : 1.0 : x[0] + x[1] + x[2] + x[9] : +Inf : True
11 : 1.0 : x[0] + x[3] + x[6] + x[10] : +Inf : True
12 : 1.0 : x[0] + x[1] + x[7] + x[8] : +Inf : True
13 : 1.0 : x[0] : +Inf : True
14 : 1.0 : x[0] + x[1] + x[3] + x[8] : +Inf : True
15 : 1.0 : x[0] + x[9] : +Inf : True
16 : 1.0 : x[0] + x[1] + x[5] : +Inf : True
17 : 1.0 : x[0] + x[5] + x[6] + x[10] : +Inf : True
18 : 1.0 : x[1] + x[4] + x[5] + x[7] + x[8] + x[9] + x[10] : +Inf : True
19 : 1.0 : x[5] + x[7] + x[8] + x[10] : +Inf : True
3 Declarations: x set_count каждый_элемент_покрыт_хотябы_одним_множеством
In [8]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 21.0
Upper bound: 21.0
Number of objectives: 1
Number of constraints: 2
Number of variables: 7
Number of binary variables: 11
Number of integer variables: 11
Number of nonzeros: 7
Sense: minimize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.0
Wallclock time: 0.0
Termination condition: optimal
Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Black box:
Number of iterations: 0
Error rc: 0
Time: 0.019840240478515625
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[0] 1.0
x[5] 1.0
In [9]:
x[0] 1.0
x[5] 1.0
In [10]:
[0, 5]
In [11]:
In [12]:
[(-2, -3, 1)]
In [13]:
True
In [14]:
[-1, -2, -3]
In [15]:
[(-2, -3, 1)]
In [16]:
In [17]:
[(2, 3, 1)]
In [18]:
{'G': ['G0', 'G1'],
'x_{1}=0': ['x_{1}', 'G1'],
'x_{1}=1': ['x_{1}', 'C_{0}'],
'x_{2}=0': ['x_{2}', 'G1'],
'x_{2}=1': ['x_{2}', 'C_{0}'],
'x_{3}=0': ['x_{3}', 'G1'],
'x_{3}=1': ['x_{3}', 'C_{0}']}
In [19]:
In [20]:
(['C_{0}', 'G0', 'G1', 'x_{1}', 'x_{2}', 'x_{3}'],
[{'G1', 'x_{1}'},
{'C_{0}', 'x_{1}'},
{'G1', 'x_{2}'},
{'C_{0}', 'x_{2}'},
{'G1', 'x_{3}'},
{'C_{0}', 'x_{3}'},
{'G0', 'G1'}])
In [21]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 8.0
Upper bound: 8.0
Number of objectives: 1
Number of constraints: 4
Number of variables: 6
Number of binary variables: 7
Number of integer variables: 7
Number of nonzeros: 6
Sense: minimize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.0
Wallclock time: 0.0
Termination condition: optimal
Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Black box:
Number of iterations: 0
Error rc: 0
Time: 0.026528120040893555
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[1] 1.0
x[2] 1.0
x[4] 1.0
x[6] 1.0
In [22]:
{0: 0.0, 1: 1.0, 2: 1.0, 3: 0.0, 4: 1.0, 5: 0.0, 6: 1.0}
In [23]:
[1, 2, 4, 6]
In [24]:
[(2, 3, 1)]
In [25]:
{'G': ['G0', 'G1'],
'x_{1}=0': ['x_{1}', 'G1'],
'x_{1}=1': ['x_{1}', 'C_{0}'],
'x_{2}=0': ['x_{2}', 'G1'],
'x_{2}=1': ['x_{2}', 'C_{0}'],
'x_{3}=0': ['x_{3}', 'G1'],
'x_{3}=1': ['x_{3}', 'C_{0}']}
In [26]:
In [27]:
[(-2, -3, -1), (-3, 1, 2)]
In [28]:
{'G': ['G0', 'G1', 'G2'],
'x_{1}=0': ['x_{1}', 'C_{0}', 'G1'],
'x_{1}=1': ['x_{1}', 'C_{1}', 'G1'],
'x_{2}=0': ['x_{2}', 'C_{0}', 'G1'],
'x_{2}=1': ['x_{2}', 'C_{1}', 'G1'],
'x_{3}=0': ['x_{3}', 'C_{0}', 'C_{1}'],
'x_{3}=1': ['x_{3}', 'G1', 'G2']}
In [29]:
In [30]:
(['C_{0}', 'C_{1}', 'G0', 'G1', 'G2', 'x_{1}', 'x_{2}', 'x_{3}'],
[{'C_{0}', 'G1', 'x_{1}'},
{'C_{1}', 'G1', 'x_{1}'},
{'C_{0}', 'G1', 'x_{2}'},
{'C_{1}', 'G1', 'x_{2}'},
{'C_{0}', 'C_{1}', 'x_{3}'},
{'G1', 'G2', 'x_{3}'},
{'G0', 'G1', 'G2'}])
In [31]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 12.0
Upper bound: 12.0
Number of objectives: 1
Number of constraints: 5
Number of variables: 6
Number of binary variables: 7
Number of integer variables: 7
Number of nonzeros: 6
Sense: minimize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.0
Wallclock time: 0.0
Termination condition: optimal
Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Black box:
Number of iterations: 0
Error rc: 0
Time: 0.01924443244934082
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[1] 1.0
x[2] 1.0
x[5] 1.0
x[6] 1.0
In [32]:
[1, 2, 5, 6]
In [33]:
[(-2, -3, -1), (-3, 1, 2)]
In [34]:
{'G': ['G0', 'G1', 'G2'],
'x_{1}=0': ['x_{1}', 'C_{0}', 'G1'],
'x_{1}=1': ['x_{1}', 'C_{1}', 'G1'],
'x_{2}=0': ['x_{2}', 'C_{0}', 'G1'],
'x_{2}=1': ['x_{2}', 'C_{1}', 'G1'],
'x_{3}=0': ['x_{3}', 'C_{0}', 'C_{1}'],
'x_{3}=1': ['x_{3}', 'G1', 'G2']}
In [35]:
In [36]:
1 Var Declarations
x : Size=7, Index={0, 1, 2, 3, 4, 5, 6}
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 0.0 : 1 : False : False : Binary
1 : 0 : 1.0 : 1 : False : False : Binary
2 : 0 : 1.0 : 1 : False : False : Binary
3 : 0 : 0.0 : 1 : False : False : Binary
4 : 0 : 0.0 : 1 : False : False : Binary
5 : 0 : 1.0 : 1 : False : False : Binary
6 : 0 : 1.0 : 1 : False : False : Binary
1 Objective Declarations
set_count : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 3*x[0] + 3*x[1] + 3*x[2] + 3*x[3] + 3*x[4] + 3*x[5] + 3*x[6]
1 Constraint Declarations
каждый_элемент_покрыт_хотябы_одним_множеством : Size=8, Index={C_{0}, C_{1}, G0, G1, G2, x_{1}, x_{2}, x_{3}}, Active=True
Key : Lower : Body : Upper : Active
C_{0} : 1.0 : x[0] + x[2] + x[4] : +Inf : True
C_{1} : 1.0 : x[1] + x[3] + x[4] : +Inf : True
G0 : 1.0 : x[6] : +Inf : True
G1 : 1.0 : x[0] + x[1] + x[2] + x[3] + x[5] + x[6] : +Inf : True
G2 : 1.0 : x[5] + x[6] : +Inf : True
x_{1} : 1.0 : x[0] + x[1] : +Inf : True
x_{2} : 1.0 : x[2] + x[3] : +Inf : True
x_{3} : 1.0 : x[4] + x[5] : +Inf : True
3 Declarations: x set_count каждый_элемент_покрыт_хотябы_одним_множеством
['C_{0}', 'C_{1}', 'G0', 'G1', 'G2', 'x_{1}', 'x_{2}', 'x_{3}']
Генерация теcтов
In [37]:
In [38]:
In [39]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/data/cocalc/4dea19db-f2e2-4175-bc0b-f84bc59c2366/hard-problems-formulations/minimum-exact-cover.ipynb Cell 43 line 4
<a href='vscode-notebook-cell://xn--22-6kce2c.xn--80apqgfe.xn--p1ai/var/data/cocalc/4dea19db-f2e2-4175-bc0b-f84bc59c2366/hard-problems-formulations/minimum-exact-cover.ipynb#X60sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a> for clauses_amount_index in range(2, 20):
<a href='vscode-notebook-cell://xn--22-6kce2c.xn--80apqgfe.xn--p1ai/var/data/cocalc/4dea19db-f2e2-4175-bc0b-f84bc59c2366/hard-problems-formulations/minimum-exact-cover.ipynb#X60sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> for _ in range(100):
----> <a href='vscode-notebook-cell://xn--22-6kce2c.xn--80apqgfe.xn--p1ai/var/data/cocalc/4dea19db-f2e2-4175-bc0b-f84bc59c2366/hard-problems-formulations/minimum-exact-cover.ipynb#X60sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> result, got_sets, ilp_model, py3sat_model = generate_tests(clauses_amount_index)
TypeError: cannot unpack non-iterable bool object
In [ ]:
[1, 3, 4, 5, 6]
In [ ]:
In [ ]: