[PYTHON] Combinatorial optimization --Typical problem-- Partition of a set problem

Typical problem and execution method

Partition of a set problem

$ N $ subsets of the set $ M = \ {1, \ dots, m \} $ $ S_j (\ subseteq M), j \ in N = \ {1, \ dots, n \} Suppose a cost of $ c_j $ is given to $. Find the division $ X (\ subseteq N) $ of $ M $ that minimizes the sum of costs. The split must not have the same elements in the subset.

Execution method

usage


Signature: set_partition(n, cand)
Docstring:
Partition of a set problem
input
    n:Element count
    cand: (weight,Subset)Candidate list
output
Number list of selected candidate list

python


#CSV data
import pandas as pd
from ortoolpy import set_partition
ss = pd.read_csv('data/subset.csv')
g = ss.groupby('id')
set_partition(len(g), [(r.weight.iloc[0], r.element.tolist()) for _, r in g])

result


[2, 3]

set.gif

python


# pandas.DataFrame
from ortoolpy.optimization import SetPartition
SetPartition('data/subset.csv')
id weight element
4 2 1.0 a
5 2 NaN d
6 3 3.0 b
7 3 NaN c

python


#Sample data
from ortoolpy import set_partition
set_partition(4, [(1, ('a', 'b')), (1, ('a', 'c')), (1, ('a', 'd')), (3, ('b', 'c'))])

result


[2, 3]

data

Recommended Posts

Combinatorial optimization --Typical problem-- Partition of a set problem
A very simple example of an ortoolpy optimization problem
[At Coder] Solve a typical problem of depth-first search (DFS)
Combinatorial optimization-Typical problem-Maximum stable set problem
The power of combinatorial optimization solvers
Design of experiments and combinatorial optimization
Combinatorial optimization-Typical problem-Transportation route (delivery optimization) problem
Try to solve a set problem of high school math with Python
I implemented NSGA-II, a multi-objective optimization problem.
A set of integers that satisfies ax + by = 1.
Combinatorial optimization to find the hand of "Millijan"
Solving the N Queen problem with combinatorial optimization
Solving the N Queens problem with combinatorial optimization
Minimize the number of polishings by combinatorial optimization
Judging the finish of mahjong by combinatorial optimization
Use combinatorial optimization
I implemented NSGA-Ⅲ, which is a multi-objective optimization problem.
(Python) Treat integer values as a set of flags
[GoLang] Set a space at the beginning of the comment
[At Coder] Solving a typical BFS problem [A --Darker and Darker]
Zip 4 Gbyte problem is a story of the past