Project: Моделирование труднорешаемых задач
Views: 22Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204Kernel: Python 3 (ipykernel)
MAXIMUM SET PACKING / Максимальная упаковка множеств
In [1]:
Постановка задачи
Представим, что мы имеем конечное множество «U» (universum) и «S» — список подмножеств множества «U». Задача упаковки задаётся вопросом, есть ли ''k'' подмножеств в списке, которые попарно не пересекаются. Оптимизационная версия задачи, '''максимальная упаковка множеств''', задаёт вопрос о максимальном числе попарно непересекающихся множеств из «S».
In [2]:
In [3]:
Реализация в Pyomo
In [4]:
In [5]:
(['00',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'12',
'13',
'14',
'17'],
[{'00', '02', '04', '06', '08', '10', '12', '14'},
{'00', '03', '06', '09', '12'},
{'06'},
{'09', '14'},
{'07', '13'},
{'03', '05', '10', '17'}])
In [6]:
2 Set Declarations
x_index : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 6 : {0, 1, 2, 3, 4, 5}
каждый_элемент_в_одном_множестве_index : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 14 : {'00', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '17'}
1 Var Declarations
x : Size=6, 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
1 Objective Declarations
set_count : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : x[0] + x[1] + x[2] + x[3] + x[4] + x[5]
1 Constraint Declarations
каждый_элемент_в_одном_множестве : Size=14, Index=каждый_элемент_в_одном_множестве_index, Active=True
Key : Lower : Body : Upper : Active
00 : -Inf : x[0] + x[1] : 1.0 : True
02 : -Inf : x[0] : 1.0 : True
03 : -Inf : x[1] + x[5] : 1.0 : True
04 : -Inf : x[0] : 1.0 : True
05 : -Inf : x[5] : 1.0 : True
06 : -Inf : x[0] + x[1] + x[2] : 1.0 : True
07 : -Inf : x[4] : 1.0 : True
08 : -Inf : x[0] : 1.0 : True
09 : -Inf : x[1] + x[3] : 1.0 : True
10 : -Inf : x[0] + x[5] : 1.0 : True
12 : -Inf : x[0] + x[1] : 1.0 : True
13 : -Inf : x[4] : 1.0 : True
14 : -Inf : x[0] + x[3] : 1.0 : True
17 : -Inf : x[5] : 1.0 : True
5 Declarations: x_index x set_count каждый_элемент_в_одном_множестве_index каждый_элемент_в_одном_множестве
In [7]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 4.0
Upper bound: 4.0
Number of objectives: 1
Number of constraints: 6
Number of variables: 5
Number of binary variables: 6
Number of integer variables: 6
Number of nonzeros: 5
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.0
Wallclock time: 0.01
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.07566499710083008
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[2] 1.0
x[3] 1.0
x[4] 1.0
x[5] 1.0
In [8]:
[2, 3, 4, 5]
In [9]:
In [ ]:
In [10]:
In [11]:
[(3, -1, 2), (1, -3, 2)]
In [12]:
[[1, 2], [1, 2], [1, 2]]
In [13]:
In [14]:
True
In [15]:
[-1, -2, -3]
In [16]:
[[-1, -2, -3], [1, -2, 3], [1, 2, 3], [1, 2, -3], [-1, 2, -3], [-1, 2, 3]]
In [17]:
In [18]:
[(3, -1, -2)]
In [19]:
[['C_{0}', 'x_{3}^{0}=0'],
['C_{0}', 'x_{1}^{0}=1'],
['C_{0}', 'x_{2}^{0}=1'],
['x_{1}', 'x_{1}^{0}=0'],
['x_{1}', 'x_{1}^{0}=1'],
['x_{2}', 'x_{2}^{0}=0'],
['x_{2}', 'x_{2}^{0}=1'],
['x_{3}', 'x_{3}^{0}=0'],
['x_{3}', 'x_{3}^{0}=1']]
In [20]:
[(3, -1, -2)]
In [21]:
In [22]:
(['C_{0}',
'x_{1}',
'x_{1}^{0}=0',
'x_{1}^{0}=1',
'x_{2}',
'x_{2}^{0}=0',
'x_{2}^{0}=1',
'x_{3}',
'x_{3}^{0}=0',
'x_{3}^{0}=1'],
[{'C_{0}', 'x_{3}^{0}=0'},
{'C_{0}', 'x_{1}^{0}=1'},
{'C_{0}', 'x_{2}^{0}=1'},
{'x_{1}', 'x_{1}^{0}=0'},
{'x_{1}', 'x_{1}^{0}=1'},
{'x_{2}', 'x_{2}^{0}=0'},
{'x_{2}', 'x_{2}^{0}=1'},
{'x_{3}', 'x_{3}^{0}=0'},
{'x_{3}', 'x_{3}^{0}=1'}])
In [23]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 4.0
Upper bound: 4.0
Number of objectives: 1
Number of constraints: 7
Number of variables: 9
Number of binary variables: 9
Number of integer variables: 9
Number of nonzeros: 9
Sense: maximize
# ----------------------------------------------------------
# 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.08013749122619629
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[2] 1.0
x[3] 1.0
x[5] 1.0
x[8] 1.0
In [24]:
[2, 3, 5, 8]
In [25]:
[(3, -1, -2)]
In [26]:
[['C_{0}', 'x_{3}^{0}=0'],
['C_{0}', 'x_{1}^{0}=1'],
['C_{0}', 'x_{2}^{0}=1'],
['x_{1}', 'x_{1}^{0}=0'],
['x_{1}', 'x_{1}^{0}=1'],
['x_{2}', 'x_{2}^{0}=0'],
['x_{2}', 'x_{2}^{0}=1'],
['x_{3}', 'x_{3}^{0}=0'],
['x_{3}', 'x_{3}^{0}=1']]
In [27]:
In [ ]:
In [28]:
[(2, 3, -1), (2, -1, 3)]
In [29]:
[['C_{0}', 'x_{2}^{0}=0'],
['C_{0}', 'x_{3}^{0}=0'],
['C_{0}', 'x_{1}^{0}=1'],
['C_{1}', 'x_{2}^{1}=0'],
['C_{1}', 'x_{1}^{1}=1'],
['C_{1}', 'x_{3}^{1}=0'],
['x_{1}', 'x_{1}^{0}=0', 'x_{1}^{1}=0'],
['x_{1}', 'x_{1}^{0}=1', 'x_{1}^{1}=1'],
['x_{2}', 'x_{2}^{0}=0', 'x_{2}^{1}=0'],
['x_{2}', 'x_{2}^{0}=1', 'x_{2}^{1}=1'],
['x_{3}', 'x_{3}^{0}=0', 'x_{3}^{1}=0'],
['x_{3}', 'x_{3}^{0}=1', 'x_{3}^{1}=1']]
In [30]:
In [31]:
(['C_{0}',
'C_{1}',
'x_{1}',
'x_{1}^{0}=0',
'x_{1}^{0}=1',
'x_{1}^{1}=0',
'x_{1}^{1}=1',
'x_{2}',
'x_{2}^{0}=0',
'x_{2}^{0}=1',
'x_{2}^{1}=0',
'x_{2}^{1}=1',
'x_{3}',
'x_{3}^{0}=0',
'x_{3}^{0}=1',
'x_{3}^{1}=0',
'x_{3}^{1}=1'],
[{'C_{0}', 'x_{2}^{0}=0'},
{'C_{0}', 'x_{3}^{0}=0'},
{'C_{0}', 'x_{1}^{0}=1'},
{'C_{1}', 'x_{2}^{1}=0'},
{'C_{1}', 'x_{1}^{1}=1'},
{'C_{1}', 'x_{3}^{1}=0'},
{'x_{1}', 'x_{1}^{0}=0', 'x_{1}^{1}=0'},
{'x_{1}', 'x_{1}^{0}=1', 'x_{1}^{1}=1'},
{'x_{2}', 'x_{2}^{0}=0', 'x_{2}^{1}=0'},
{'x_{2}', 'x_{2}^{0}=1', 'x_{2}^{1}=1'},
{'x_{3}', 'x_{3}^{0}=0', 'x_{3}^{1}=0'},
{'x_{3}', 'x_{3}^{0}=1', 'x_{3}^{1}=1'}])
In [32]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 5.0
Upper bound: 5.0
Number of objectives: 1
Number of constraints: 11
Number of variables: 12
Number of binary variables: 12
Number of integer variables: 12
Number of nonzeros: 12
Sense: maximize
# ----------------------------------------------------------
# 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.03926873207092285
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[2] 1.0
x[5] 1.0
x[6] 1.0
x[9] 1.0
x[11] 1.0
In [33]:
[2, 5, 6, 9, 11]
In [34]:
[(2, 3, -1), (2, -1, 3)]
In [35]:
[['C_{0}', 'x_{2}^{0}=0'],
['C_{0}', 'x_{3}^{0}=0'],
['C_{0}', 'x_{1}^{0}=1'],
['C_{1}', 'x_{2}^{1}=0'],
['C_{1}', 'x_{1}^{1}=1'],
['C_{1}', 'x_{3}^{1}=0'],
['x_{1}', 'x_{1}^{0}=0', 'x_{1}^{1}=0'],
['x_{1}', 'x_{1}^{0}=1', 'x_{1}^{1}=1'],
['x_{2}', 'x_{2}^{0}=0', 'x_{2}^{1}=0'],
['x_{2}', 'x_{2}^{0}=1', 'x_{2}^{1}=1'],
['x_{3}', 'x_{3}^{0}=0', 'x_{3}^{1}=0'],
['x_{3}', 'x_{3}^{0}=1', 'x_{3}^{1}=1']]
In [36]:
[(2, 3, -1), (2, -1, 3)]
In [37]:
In [ ]: