1+ from pprint import pprint
12from concurrent .futures import as_completed , Future , ProcessPoolExecutor
23from cytoolz .itertoolz import last
34from itertools import pairwise , product as CartesianProduct
1415import csv
1516import uuid
1617
17- def count (state : EliminationState ) -> int :
18+ def count (state : EliminationState ) -> EliminationState :
1819 model = cp_model .CpModel ()
1920
2021 listLeavesInPileOrder : list [cp_model .IntVar ] = [model .new_int_var (pileOrigin , state .pileLast , f"leafInPile[{ pile } ]" ) for pile in range (state .leavesTotal )]
@@ -107,22 +108,17 @@ class FoldingCollector(cp_model.CpSolverSolutionCallback):
107108 def __init__ (self , _listOfIndicesLeafInPilingsOrder : list [cp_model .IntVar ]) -> None :
108109 super ().__init__ ()
109110 self ._listOfIndicesLeafInPilingsOrder : list [cp_model .IntVar ] = _listOfIndicesLeafInPilingsOrder
110- self .listFoldings : list [list [Leaf ]] = []
111+ self .listFolding : list [list [Leaf ]] = []
111112
112113 def on_solution_callback (self ) -> None :
113- self .listFoldings .append ([self .value (leaf ) for leaf in self ._listOfIndicesLeafInPilingsOrder ])
114+ self .listFolding .append ([self .value (leaf ) for leaf in self ._listOfIndicesLeafInPilingsOrder ])
114115
115116 foldingCollector = FoldingCollector (listLeavesInPileOrder )
116117 solver .solve (model , foldingCollector )
117118
118- # TODO NOTE temporary data collection for p2d7
119- if (state .dimensionsTotal == 7 ) and (foldingCollector .listFoldings ):
120- pathFilename : Path = packageSettings .pathPackage / "_e" / "dataRaw" / f"p2d7_{ uuid .uuid4 ()} .csv"
121- with Path .open (pathFilename , mode = "w" , newline = "" ) as fileCSV :
122- csvWriter = csv .writer (fileCSV )
123- csvWriter .writerows (foldingCollector .listFoldings )
119+ state .listFolding = list (map (tuple , foldingCollector .listFolding ))
124120
125- return len ( foldingCollector . listFoldings ) * state . Theorem2Multiplier * state . Theorem4Multiplier
121+ return state
126122
127123def doTheNeedful (state : EliminationState , workersMaximum : int ) -> EliminationState :
128124 """Do the things necessary so that `count` operates efficiently."""
@@ -134,15 +130,29 @@ def doTheNeedful(state: EliminationState, workersMaximum: int) -> EliminationSta
134130 pileForConcurrency : int = state .pileLast // 2
135131 state .listPermutationSpace = [{pileForConcurrency : leaf } for leaf in range (state .leavesTotal )]
136132
137- listClaimTickets : list [Future [int ]] = [
133+ listClaimTickets : list [Future [EliminationState ]] = [
138134 concurrencyManager .submit (count , EliminationState (state .mapShape , permutationSpace = permutationSpace ))
139135 for permutationSpace in state .listPermutationSpace
140136 ]
141137
142138 for claimTicket in tqdm (as_completed (listClaimTickets ), total = len (listClaimTickets ), disable = False ):
143- state .groupsOfFolds += claimTicket .result ()
139+ sherpa : EliminationState = claimTicket .result ()
140+
141+ # TODO NOTE temporary data collection for p2d7
142+ if (sherpa .dimensionsTotal == 7 ) and (sherpa .listFolding ):
143+ pathFilename : Path = packageSettings .pathPackage / "_e" / "dataRaw" / f"p2d7_{ uuid .uuid4 ()} .csv"
144+ with Path .open (pathFilename , mode = "w" , newline = "" ) as fileCSV :
145+ csvWriter = csv .writer (fileCSV )
146+ csvWriter .writerows (sherpa .listFolding )
147+
148+ state .groupsOfFolds += len (sherpa .listFolding )
149+ state .Theorem2aMultiplier = sherpa .Theorem2aMultiplier
150+ state .Theorem2Multiplier = sherpa .Theorem2Multiplier
151+ state .Theorem3Multiplier = sherpa .Theorem3Multiplier
152+ state .Theorem4Multiplier = sherpa .Theorem4Multiplier
144153
145154 else :
146- state .groupsOfFolds = count (state )
155+ state = count (state )
156+ state .groupsOfFolds = len (state .listFolding )
147157
148158 return state
0 commit comments