Solved "Improved algorithm that automatically determines the hand of" Mirijan "" using Combinatorial optimization in Python. I tried it.
Use pattern 2 from the original article.
#Pattern 2
hands = ['Haruka', 'Chihaya', 'Miki', 'true', 'Takane', 'Yayoi', 'Festival', 'true美', 'Emily', 'Ami', 'Momoko', 'Iori', 'Raising']
hand_unit_list = [
{'member': {'Iori', 'Emily'}, 'name': 'little trip around the world'},
{'member': {'Emily', 'Festival'}, 'name': 'Charlotte Charlotte'},
{'member': {'Mami', 'Yayoi'}, 'name': 'Wants →'},
{'member': {'Chihaya', 'Haruka'}, 'name': 'CRIMSON LOVERS'},
{'member': {'Miki', 'Iori'}, 'name': "First Don't worry"},
{'member': {'Raising', 'Iori', 'Momoko'}, 'name': 'Kyun! Vampire girl'},
{'member': {'true', 'Iori', 'Yayoi'}, 'name': 'Stand-by prince'},
{'member': {'Miki', 'Iori', 'Takane'}, 'name': '99 Nights'},
{'member': {'true', 'true美', 'Haruka'}, 'name': 'Don't bloom! !! Maiden Juku'},
{'member': {'Miki', 'Chihaya', 'Haruka'}, 'name': 'Fate of the World'},
{'member': {'Mami', 'Ami', 'Yayoi'}, 'name': 'Funny Logic'},
{'member': {'Miki', 'Mami', 'Ami', 'Iori'}, 'name': 'Hoshisai Stepper'},
{'member': {'Miki', 'Haruka', 'true', 'Chihaya', 'Yayoi'}, 'name': 'Merry'},
{'member': {'Miki', 'Haruka', 'true', 'Chihaya', 'Emily'}, 'name': 'World changer'},
{'member': {'Iori', 'Haruka', 'true', 'true美', 'Ami'}, 'name': 'Miracle Night'},
{'member': {'Miki', 'Takane', 'true', 'true美', 'Yayoi', 'Ami'}, 'name': 'SHOW at The Live Revolution!'}
]
Let's solve it as Combination auction problem.
import pandas as pd
from pulp import lpSum
from ortoolpy import model_max, addbinvars, addvals
df = pd.DataFrame([hand_unit['name'] for hand_unit in hand_unit_list],
columns=['name'])
for hand in hands:
df[hand] = False
for i, hand_unit in enumerate(hand_unit_list):
for member in hand_unit['member']:
df.loc[i, member] = True
addbinvars(df)
m = model_max()
m += lpSum(df.Var)
for hand in hands:
m += lpSum(df[df[hand] == True].Var) <= 1
m.solve()
addvals(df)
print(df[df.Val > 0])
name | Haruka | Chihaya | Miki | true | Takane | Yayoi | Festival | Mami | Emily | Ami | Momoko | Iori | Raising | Var | Val | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Charlotte Charlotte | False | False | False | False | False | False | True | False | True | False | False | False | False | v000002 | 1 |
2 | Wants → | False | False | False | False | False | True | False | True | False | False | False | False | False | v000003 | 1 |
3 | CRIMSON LOVERS | True | True | False | False | False | False | False | False | False | False | False | False | False | v000004 | 1 |
5 | Kyun! Vampire girl | False | False | False | False | False | False | False | False | False | False | True | True | True | v000006 | 1 |
I got the same result as the original article.
Recommended Posts