Project: Моделирование труднорешаемых задач
Views: 22Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204Kernel: Python 3 (ipykernel)
MAXIMUM SET SPLITTING / Максимальное расщепление множеств
In [4]:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 4
2 from IPython.core.display import SVG
3 import pyomo.environ as pyo
----> 4 from pysat.solvers import Solver
5 from pysat.formula import CNF
6 import py_svg_combinatorics as psc
ModuleNotFoundError: No module named 'pysat.solvers'
Постановка задачи
Задача разрешения — для данного семейства «S» подмножеств конечного множества «U» решить, существует ли разбиение «U» на два подмножества элементов , элементы, что «расщепляется» этим разбиением, т. е. ни один из элементов S не находится целиком в или . Эту проблему иногда называют 2-раскрашиваемостью гиперграфа.
Оптимизационная версия этой задачи называется «расщеплением максимального набора» и требует найти разбиение, которое максимизирует количество расщепляемых элементов S.
In [0]:
In [0]:
In [0]:
Реализация в Pyomo
In [5]:
In [6]:
(['01',
'02',
'03',
'04',
'05',
'09',
'10',
'12',
'13',
'14',
'15',
'17',
'18',
'19'],
[{'03', '13'},
{'09', '18'},
{'12', '19'},
{'13', '14'},
{'10', '13'},
{'02', '17'},
{'09', '18'},
{'05', '17'},
{'01', '15'},
{'04', '10'},
{'05', '12', '19'},
{'04', '09', '18'},
{'02', '03', '04', '05', '12', '14', '18', '19'}])
In [7]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 13.0
Upper bound: 13.0
Number of objectives: 1
Number of constraints: 26
Number of variables: 27
Number of binary variables: 27
Number of integer variables: 27
Number of nonzeros: 13
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.01
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.09720849990844727
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[3] 1.0
x[8] 1.0
x[10] 1.0
x[11] 1.0
x[12] 1.0
x[13] 1.0
y[0] 1.0
y[1] 1.0
y[2] 1.0
y[3] 1.0
y[4] 1.0
y[5] 1.0
y[6] 1.0
y[7] 1.0
y[8] 1.0
y[9] 1.0
y[10] 1.0
y[11] 1.0
y[12] 1.0
In [8]:
['04', '13', '15', '17', '18', '19']
In [9]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
In [10]:
In [11]:
[{'03', '13'},
{'09', '18'},
{'12', '19'},
{'13', '14'},
{'10', '13'},
{'02', '17'},
{'09', '18'},
{'05', '17'},
{'01', '15'},
{'04', '10'},
{'05', '12', '19'},
{'04', '09', '18'},
{'02', '03', '04', '05', '12', '14', '18', '19'}]
| |
|
|
In [0]:
In [12]:
[[-2, -1, -4], [4, -3, -2]]
In [13]:
True
In [14]:
[-1, -2, -3, -4]
In [15]:
[[-1, -2, -3, -4], [-1, -2, 3, -4], [-1, -2, 3, 4], [1, -2, 3, 4], [1, -2, -3, 4], [-1, -2, -3, 4], [1, -2, -3, -4], [1, -2, 3, -4], [1, 2, -3, -4], [-1, 2, -3, -4], [-1, 2, -3, 4], [-1, 2, 3, 4]]
In [16]:
In [17]:
0
In [18]:
In [19]:
[[3, -2, 1]]
In [20]:
{'C_{0}': ['O', 'x^{0}_{3}', '\\overline{x^{0}_{2}}', 'x^{0}_{1}'],
'C_{0}(\\overline{x_{2}})': ['\\overline{x^{0}_{2}}', 'x_{2}'],
'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'],
'C_{0}(x_{3})': ['x^{0}_{3}', '\\overline{x_{3}}'],
'x_{1}': ['x_{1}', '\\overline{x_{1}}'],
'x_{2}': ['x_{2}', '\\overline{x_{2}}'],
'x_{3}': ['x_{3}', '\\overline{x_{3}}']}
In [21]:
In [22]:
(['O',
'\\overline{x^{0}_{2}}',
'\\overline{x_{1}}',
'\\overline{x_{2}}',
'\\overline{x_{3}}',
'x^{0}_{1}',
'x^{0}_{3}',
'x_{1}',
'x_{2}',
'x_{3}'],
[{'\\overline{x_{1}}', 'x_{1}'},
{'\\overline{x_{2}}', 'x_{2}'},
{'\\overline{x_{3}}', 'x_{3}'},
{'\\overline{x_{3}}', 'x^{0}_{3}'},
{'\\overline{x^{0}_{2}}', 'x_{2}'},
{'\\overline{x_{1}}', 'x^{0}_{1}'},
{'O', '\\overline{x^{0}_{2}}', 'x^{0}_{1}', 'x^{0}_{3}'}])
In [23]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 7.0
Upper bound: 7.0
Number of objectives: 1
Number of constraints: 14
Number of variables: 17
Number of binary variables: 17
Number of integer variables: 17
Number of nonzeros: 7
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.01
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.10454320907592773
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[2] 1.0
x[6] 1.0
x[8] 1.0
x[9] 1.0
y[0] 1.0
y[1] 1.0
y[2] 1.0
y[3] 1.0
y[4] 1.0
y[5] 1.0
y[6] 1.0
In [24]:
['\\overline{x_{1}}', 'x^{0}_{3}', 'x_{2}', 'x_{3}']
In [25]:
[0, 1, 2, 3, 4, 5, 6]
In [26]:
[[3, -2, 1]]
In [27]:
{'C_{0}': ['O', 'x^{0}_{3}', '\\overline{x^{0}_{2}}', 'x^{0}_{1}'],
'C_{0}(\\overline{x_{2}})': ['\\overline{x^{0}_{2}}', 'x_{2}'],
'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'],
'C_{0}(x_{3})': ['x^{0}_{3}', '\\overline{x_{3}}'],
'x_{1}': ['x_{1}', '\\overline{x_{1}}'],
'x_{2}': ['x_{2}', '\\overline{x_{2}}'],
'x_{3}': ['x_{3}', '\\overline{x_{3}}']}
In [28]:
In [29]:
[[-2, -3, 1], [-2, 3, -4]]
In [30]:
{'C_{0}': ['O', '\\overline{x^{0}_{2}}', '\\overline{x^{0}_{3}}', 'x^{0}_{1}'],
'C_{0}(\\overline{x_{2}})': ['\\overline{x^{0}_{2}}', 'x_{2}'],
'C_{0}(\\overline{x_{3}})': ['\\overline{x^{0}_{3}}', 'x_{3}'],
'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'],
'C_{1}': ['O', '\\overline{x^{1}_{2}}', 'x^{1}_{3}', '\\overline{x^{1}_{4}}'],
'C_{1}(\\overline{x_{2}})': ['\\overline{x^{1}_{2}}', 'x_{2}'],
'C_{1}(\\overline{x_{4}})': ['\\overline{x^{1}_{4}}', 'x_{4}'],
'C_{1}(x_{3})': ['x^{1}_{3}', '\\overline{x_{3}}'],
'x_{1}': ['x_{1}', '\\overline{x_{1}}'],
'x_{2}': ['x_{2}', '\\overline{x_{2}}'],
'x_{3}': ['x_{3}', '\\overline{x_{3}}'],
'x_{4}': ['x_{4}', '\\overline{x_{4}}']}
In [31]:
In [32]:
(['O',
'\\overline{x^{0}_{2}}',
'\\overline{x^{0}_{3}}',
'\\overline{x^{1}_{2}}',
'\\overline{x^{1}_{4}}',
'\\overline{x_{1}}',
'\\overline{x_{2}}',
'\\overline{x_{3}}',
'\\overline{x_{4}}',
'x^{0}_{1}',
'x^{1}_{3}',
'x_{1}',
'x_{2}',
'x_{3}',
'x_{4}'],
[{'\\overline{x_{1}}', 'x_{1}'},
{'\\overline{x_{2}}', 'x_{2}'},
{'\\overline{x_{3}}', 'x_{3}'},
{'\\overline{x_{4}}', 'x_{4}'},
{'\\overline{x^{0}_{2}}', 'x_{2}'},
{'\\overline{x^{0}_{3}}', 'x_{3}'},
{'\\overline{x_{1}}', 'x^{0}_{1}'},
{'O', '\\overline{x^{0}_{2}}', '\\overline{x^{0}_{3}}', 'x^{0}_{1}'},
{'\\overline{x^{1}_{2}}', 'x_{2}'},
{'\\overline{x_{3}}', 'x^{1}_{3}'},
{'\\overline{x^{1}_{4}}', 'x_{4}'},
{'O', '\\overline{x^{1}_{2}}', '\\overline{x^{1}_{4}}', 'x^{1}_{3}'}])
In [33]:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 12.0
Upper bound: 12.0
Number of objectives: 1
Number of constraints: 24
Number of variables: 27
Number of binary variables: 27
Number of integer variables: 27
Number of nonzeros: 12
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
User time: -1.0
System time: 0.01
Wallclock time: 0.02
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.16412687301635742
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
x[2] 1.0
x[4] 1.0
x[5] 1.0
x[7] 1.0
x[8] 1.0
x[12] 1.0
y[0] 1.0
y[1] 1.0
y[2] 1.0
y[3] 1.0
y[4] 1.0
y[5] 1.0
y[6] 1.0
y[7] 1.0
y[8] 1.0
y[9] 1.0
y[10] 1.0
y[11] 1.0
In [34]:
['\\overline{x^{0}_{3}}',
'\\overline{x^{1}_{4}}',
'\\overline{x_{1}}',
'\\overline{x_{3}}',
'\\overline{x_{4}}',
'x_{2}']
In [35]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
In [36]:
[[-2, -3, 1], [-2, 3, -4]]
In [37]:
{'C_{0}': ['O', '\\overline{x^{0}_{2}}', '\\overline{x^{0}_{3}}', 'x^{0}_{1}'],
'C_{0}(\\overline{x_{2}})': ['\\overline{x^{0}_{2}}', 'x_{2}'],
'C_{0}(\\overline{x_{3}})': ['\\overline{x^{0}_{3}}', 'x_{3}'],
'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'],
'C_{1}': ['O', '\\overline{x^{1}_{2}}', 'x^{1}_{3}', '\\overline{x^{1}_{4}}'],
'C_{1}(\\overline{x_{2}})': ['\\overline{x^{1}_{2}}', 'x_{2}'],
'C_{1}(\\overline{x_{4}})': ['\\overline{x^{1}_{4}}', 'x_{4}'],
'C_{1}(x_{3})': ['x^{1}_{3}', '\\overline{x_{3}}'],
'x_{1}': ['x_{1}', '\\overline{x_{1}}'],
'x_{2}': ['x_{2}', '\\overline{x_{2}}'],
'x_{3}': ['x_{3}', '\\overline{x_{3}}'],
'x_{4}': ['x_{4}', '\\overline{x_{4}}']}
In [38]:
In [0]:
In [39]:
In [40]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/var/data/cocalc/1d588dae-0d14-422a-88b4-c470ec2c8303/hard-problems-formulations/maximum-set-splitting.ipynb Cell 46 line 1
----> <a href='vscode-notebook-cell://xn--17-6kce2c.xn--80apqgfe.xn--p1ai/var/data/cocalc/1d588dae-0d14-422a-88b4-c470ec2c8303/hard-problems-formulations/maximum-set-splitting.ipynb#X63sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> cnf.clauses
NameError: name 'cnf' is not defined
In [0]:
{1: True, 2: True, 3: True}
In [0]:
In [0]:
([[-3, 1, -2], [-1, 2, 3], [-1, 3, 1], [2, 3, 1], [1, 2, -3], [2, 1, -1], [1, -2, 3]], {1: True, 2: True, 3: True})
Создание Тестов
In [0]:
In [0]:
In [0]:
SOLUTION FOUND num amount: 5 , answer: True
(True, {'x_{1}': ['x_{1}', '\\overline{x_{1}}'], 'x_{2}': ['x_{2}', '\\overline{x_{2}}'], 'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'], 'C_{0}(x_{2})': ['x^{0}_{2}', '\\overline{x_{2}}'], 'C_{0}': ['O', 'x^{0}_{1}', 'x^{0}_{1}', 'x^{0}_{2}'], 'C_{1}(\\overline{x_{1}})': ['\\overline{x^{1}_{1}}', 'x_{1}'], 'C_{1}(\\overline{x_{2}})': ['\\overline{x^{1}_{2}}', 'x_{2}'], 'C_{1}(x_{1})': ['x^{1}_{1}', '\\overline{x_{1}}'], 'C_{1}': ['O', '\\overline{x^{1}_{1}}', '\\overline{x^{1}_{2}}', 'x^{1}_{1}'], 'C_{2}(x_{1})': ['x^{2}_{1}', '\\overline{x_{1}}'], 'C_{2}(\\overline{x_{1}})': ['\\overline{x^{2}_{1}}', 'x_{1}'], 'C_{2}(\\overline{x_{2}})': ['\\overline{x^{2}_{2}}', 'x_{2}'], 'C_{2}': ['O', 'x^{2}_{1}', '\\overline{x^{2}_{1}}', '\\overline{x^{2}_{2}}'], 'C_{3}(x_{1})': ['x^{3}_{1}', '\\overline{x_{1}}'], 'C_{3}(\\overline{x_{1}})': ['\\overline{x^{3}_{1}}', 'x_{1}'], 'C_{3}(x_{2})': ['x^{3}_{2}', '\\overline{x_{2}}'], 'C_{3}': ['O', 'x^{3}_{1}', '\\overline{x^{3}_{1}}', 'x^{3}_{2}'], 'C_{4}(x_{1})': ['x^{4}_{1}', '\\overline{x_{1}}'], 'C_{4}(\\overline{x_{1}})': ['\\overline{x^{4}_{1}}', 'x_{1}'], 'C_{4}(\\overline{x_{2}})': ['\\overline{x^{4}_{2}}', 'x_{2}'], 'C_{4}': ['O', 'x^{4}_{1}', '\\overline{x^{4}_{1}}', '\\overline{x^{4}_{2}}']}, <pyomo.core.base.PyomoModel.ConcreteModel object at 0x7f7b19d77100>, [1, -2])
In [0]:
SOLUTION FOUND num amount: 1 , answer: True
SOLUTION FOUND num amount: 2 , answer: True
SOLUTION FOUND num amount: 3 , answer: True
SOLUTION FOUND num amount: 4 , answer: True
SOLUTION FOUND num amount: 5 , answer: True
SOLUTION FOUND num amount: 6 , answer: True
SOLUTION FOUND num amount: 7 , answer: True
SOLUTION FOUND num amount: 8 , answer: True
SOLUTION FOUND num amount: 9 , answer: True
SOLUTION FAILED num amount: 10 Pyomo: True , test no result: False , 3SAT: False
In [0]:
{'x_{1}': ['x_{1}', '\\overline{x_{1}}'], 'x_{2}': ['x_{2}', '\\overline{x_{2}}'], 'C_{0}(x_{1})': ['x^{0}_{1}', '\\overline{x_{1}}'], 'C_{0}(x_{2})': ['x^{0}_{2}', '\\overline{x_{2}}'], 'C_{0}': ['O', 'x^{0}_{1}', 'x^{0}_{1}', 'x^{0}_{2}'], 'C_{1}(x_{1})': ['x^{1}_{1}', '\\overline{x_{1}}'], 'C_{1}(\\overline{x_{2}})': ['\\overline{x^{1}_{2}}', 'x_{2}'], 'C_{1}(\\overline{x_{1}})': ['\\overline{x^{1}_{1}}', 'x_{1}'], 'C_{1}': ['O', 'x^{1}_{1}', '\\overline{x^{1}_{2}}', '\\overline{x^{1}_{1}}'], 'C_{2}(x_{1})': ['x^{2}_{1}', '\\overline{x_{1}}'], 'C_{2}(\\overline{x_{1}})': ['\\overline{x^{2}_{1}}', 'x_{1}'], 'C_{2}(x_{2})': ['x^{2}_{2}', '\\overline{x_{2}}'], 'C_{2}': ['O', 'x^{2}_{1}', '\\overline{x^{2}_{1}}', 'x^{2}_{2}'], 'C_{3}(x_{1})': ['x^{3}_{1}', '\\overline{x_{1}}'], 'C_{3}(x_{2})': ['x^{3}_{2}', '\\overline{x_{2}}'], 'C_{3}': ['O', 'x^{3}_{1}', 'x^{3}_{2}', 'x^{3}_{1}'], 'C_{4}(x_{1})': ['x^{4}_{1}', '\\overline{x_{1}}'], 'C_{4}(x_{2})': ['x^{4}_{2}', '\\overline{x_{2}}'], 'C_{4}': ['O', 'x^{4}_{1}', 'x^{4}_{2}', 'x^{4}_{1}'], 'C_{5}(\\overline{x_{1}})': ['\\overline{x^{5}_{1}}', 'x_{1}'], 'C_{5}(x_{2})': ['x^{5}_{2}', '\\overline{x_{2}}'], 'C_{5}': ['O', '\\overline{x^{5}_{1}}', '\\overline{x^{5}_{1}}', 'x^{5}_{2}'], 'C_{6}(\\overline{x_{1}})': ['\\overline{x^{6}_{1}}', 'x_{1}'], 'C_{6}(x_{2})': ['x^{6}_{2}', '\\overline{x_{2}}'], 'C_{6}(x_{1})': ['x^{6}_{1}', '\\overline{x_{1}}'], 'C_{6}': ['O', '\\overline{x^{6}_{1}}', 'x^{6}_{2}', 'x^{6}_{1}'], 'C_{7}(\\overline{x_{1}})': ['\\overline{x^{7}_{1}}', 'x_{1}'], 'C_{7}(\\overline{x_{2}})': ['\\overline{x^{7}_{2}}', 'x_{2}'], 'C_{7}': ['O', '\\overline{x^{7}_{1}}', '\\overline{x^{7}_{1}}', '\\overline{x^{7}_{2}}'], 'C_{8}(x_{1})': ['x^{8}_{1}', '\\overline{x_{1}}'], 'C_{8}(\\overline{x_{2}})': ['\\overline{x^{8}_{2}}', 'x_{2}'], 'C_{8}': ['O', 'x^{8}_{1}', '\\overline{x^{8}_{2}}', 'x^{8}_{1}'], 'C_{9}(x_{1})': ['x^{9}_{1}', '\\overline{x_{1}}'], 'C_{9}(\\overline{x_{1}})': ['\\overline{x^{9}_{1}}', 'x_{1}'], 'C_{9}(\\overline{x_{2}})': ['\\overline{x^{9}_{2}}', 'x_{2}'], 'C_{9}': ['O', 'x^{9}_{1}', '\\overline{x^{9}_{1}}', '\\overline{x^{9}_{2}}']}
-----
['O', '\\overline{x^{1}_{1}}', '\\overline{x^{2}_{1}}', '\\overline{x^{5}_{1}}', '\\overline{x^{6}_{1}}', '\\overline{x^{7}_{1}}', '\\overline{x^{9}_{1}}', '\\overline{x_{1}}', '\\overline{x_{2}}', 'x_{2}']
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
In [0]:
Как можем заметить, мы выявили плохой тест кейс
In [0]: