適当スクリプト 合計352270を探せ(答えがないと長時間掛かる)

住民訴訟の立証書面のデータで特定の合計値になる組合せ確認を行いたい、特定パターンを探したいと思って試行錯誤している。

このスクリプトでは、比較的短時間で複数パターンの存在が確認できるため、特定パターンは抽出不可能なので作業に見切りを付けることに使用しています。
(入力ファイルのソート順を変えると違うパターンを見つけることも出来ます。)

Pythonのセットアップをしていれば動く。
データは昇順などソートする方が早い、特定の組み合わせの変動を集めるので網羅性は低い、解なしの場合にとても時間がかかるなど、不完全なスクリプトではあるが存在の有無はある程度判定しやすい(2,3分の時間が掛かったら解なしと推定する前提なので確定では無いが実用の範囲内だと思う)。
組合せパターンを見つけるとパターン合計使用要素数を1増やして次のパターンを探しに行きます。


必要なもの

  1. Python環境

  2. 入力となるソートされたファイル "05medic02PCR.xlsx"
    (確認に使うファイルを指定してください)

  3. 入力ファイルのデータ並びとカラム名は調整してください。
    このスクリプトではZENI列の合計値を探索しています。

    1. No.

    2. Date

    3. zeni

    4. Details

  4. 中段にある352270が合計目標となる数値です。
    (694010など他の合計値を指定するとその合計値で探します)

結果出力
合致するパターンを見つけると、逐次コマンドプロンプトに吐き出します

注)
リダイレクト等して受け取るなら 受取処理をコマンドで行ってください。例>> なんちゃってスクリプト.py > OUTPUT.txt


以下はスクリプトです。

なお、チューニングが甘いのは
code interpreterを使って、統計的な基本と制約条件や期待する出力結果等どこまで出来るかと言うテストも兼ねているためです。



import pandas as pd
import concurrent
import concurrent.futures
import itertools
from concurrent.futures import ProcessPoolExecutor

# Load the Excel file
# change to your file name
df = pd.read_excel("05medic02PCR.xlsx")

# Extract the data from the 'zeni' column and your column name below
data_zeni = df['zeni'].tolist()

# Define a function to find combinations that sum to the target value 352270
def find_combinations(r):
for combination in itertools.combinations(data_zeni, r):
if sum(combination) == 352270:
return combination
def main():

# Use ProcessPoolExecutor to speed up the calculation
with ProcessPoolExecutor() as executor:
for r in range(len(data_zeni)):
combinations = list(executor.map(find_combinations, [r]))
for combination in combinations:
if combination:
print(f"Combination: {combination}")
if name == "main":
main()