graph-games-proto 0.3.1935__py3-none-any.whl → 0.3.1952__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
graph_games_proto/fns.py CHANGED
@@ -1932,7 +1932,7 @@ class BonusStatus(PClass):
1932
1932
  )
1933
1933
 
1934
1934
 
1935
- class StateKernal(PClass):
1935
+ class StateKernel(PClass):
1936
1936
  rng = field(type=random.Random)
1937
1937
  game_config = field(type=GameConfig)
1938
1938
  edges = field(type=list) # List[BiEdge]
@@ -1946,6 +1946,7 @@ class StateKernal(PClass):
1946
1946
  pieceuuid2piece = field(type=dict) # Dict[str, Piece]
1947
1947
  edgeuuid2idx = field(type=dict) # Dict[str, int]
1948
1948
  carduuid2card = field(type=dict) # Dict[str, Card]
1949
+ edgetuple2uuid = field(type=dict) # Dict[Tuple[int, int], str]
1949
1950
  def __todict__(self):
1950
1951
  return {
1951
1952
  "rng": rng2json(self.rng),
@@ -1961,10 +1962,11 @@ class StateKernal(PClass):
1961
1962
  "pieceuuid2piece": {k: v.__todict__() for k, v in self.pieceuuid2piece.items()},
1962
1963
  "edgeuuid2idx": self.edgeuuid2idx,
1963
1964
  "carduuid2card": {k: v.__todict__() for k, v in self.carduuid2card.items()},
1965
+ "edgetuple2uuid": [{"k": list(k), "v": v} for k, v in self.edgetuple2uuid.items()],
1964
1966
  }
1965
1967
  @staticmethod
1966
1968
  def __fromdict__(d):
1967
- return StateKernal(
1969
+ return StateKernel(
1968
1970
  rng=json2rng(d["rng"]),
1969
1971
  game_config=GameConfig.__fromdict__(d["game_config"]),
1970
1972
  edges=[BiEdge.__fromdict__(edge) for edge in d["edges"]],
@@ -1978,10 +1980,12 @@ class StateKernal(PClass):
1978
1980
  pieceuuid2piece={k: Piece.__fromdict(v) for k, v in d["pieceuuid2piece"].items()},
1979
1981
  edgeuuid2idx=d["edgeuuid2idx"],
1980
1982
  carduuid2card={k: Card.__fromdict(v) for k, v in d["carduuid2card"].items()},
1983
+ edgetuple2uuid={tuple(item["k"]): item["v"] for item in d["edgetuple2uuid"]},
1981
1984
  )
1982
1985
 
1983
1986
 
1984
1987
  def init_state_kernel(**kwargs):
1988
+ rng = kwargs.get('rng')
1985
1989
  game_config = kwargs.get('game_config')
1986
1990
  fig = game_config.fig
1987
1991
  board_config = fig.board_config
@@ -2003,9 +2007,18 @@ def init_state_kernel(**kwargs):
2003
2007
  cards = generate_cards(dek)
2004
2008
  for card in cards:
2005
2009
  carduuid2card[card.uuid] = card
2010
+
2011
+ nodeuuid2idx = {node.uuid: idx for idx, node in enumerate(board_config.points)}
2012
+ edges = get_edges(rng, board_config, nodeuuid2idx)
2013
+ edgetuple2uuid = {}
2014
+ for edge in edges:
2015
+ node_1_idx = nodeuuid2idx[edge.node_1_uuid]
2016
+ node_2_idx = nodeuuid2idx[edge.node_2_uuid]
2017
+ edge_tuple = (min(node_1_idx, node_2_idx), max(node_1_idx, node_2_idx))
2018
+ edgetuple2uuid[edge_tuple] = edge.uuid
2006
2019
 
2007
- return StateKernal(
2008
- rng=kwargs.get('rng'),
2020
+ return StateKernel(
2021
+ rng=rng,
2009
2022
  game_config=game_config,
2010
2023
  edges=edges,
2011
2024
  decks=kwargs.get('decks'),
@@ -2018,11 +2031,12 @@ def init_state_kernel(**kwargs):
2018
2031
  edgeuuid2idx=edgeuuid2idx,
2019
2032
  carduuid2card=carduuid2card,
2020
2033
  pieceuuid2piece=pieceuuid2piece,
2034
+ edgetuple2uuid=edgetuple2uuid,
2021
2035
  )
2022
2036
 
2023
2037
 
2024
2038
  class State(PClass):
2025
- kernel = field(type=StateKernal)
2039
+ kernel = field(type=StateKernel)
2026
2040
  idx2path = field(type=list) # List[Path2]
2027
2041
  pieceuuid2piece = field(type=dict) # Dict[str, Piece]
2028
2042
  bonus_statuses = field(type=list) # List[BonusStatus]
@@ -2037,7 +2051,6 @@ class State(PClass):
2037
2051
  nodes = field(type=list) # List[Node]
2038
2052
  nodeuuid2idx = field(type=dict) # Dict[str, int]
2039
2053
  edges = field(type=list) # List[BiEdge]
2040
- edgetuple2uuid = field(type=dict) # Dict[Tuple[int, int], str]
2041
2054
  regions = field(type=list) # List[Region]
2042
2055
  legal_actions_3 = field(type=list) # List[LegalAction]
2043
2056
  piles = field(type=list) # List[Pile]
@@ -2065,7 +2078,6 @@ class State(PClass):
2065
2078
  "nodes": [node.__todict__() for node in self.nodes],
2066
2079
  "nodeuuid2idx": self.nodeuuid2idx,
2067
2080
  "edges": [edge.__todict__() for edge in self.edges],
2068
- "edgetuple2uuid": [{"k": list(k), "v": v} for k, v in self.edgetuple2uuid.items()],
2069
2081
  "regions": [region.__todict__() for region in self.regions],
2070
2082
  "legal_actions_3": [x.__todict__() for x in self.legal_actions_3],
2071
2083
  "piles": [pile.__todict__() for pile in self.piles],
@@ -2080,7 +2092,7 @@ class State(PClass):
2080
2092
  @staticmethod
2081
2093
  def __fromdict__(d):
2082
2094
  return State(
2083
- kernel=StateKernal.__fromdict__(d["kernel"]),
2095
+ kernel=StateKernel.__fromdict__(d["kernel"]),
2084
2096
  idx2path=[Path2.__fromdict__(v) for v in d["idx2path"]],
2085
2097
  pieceuuid2piece={k: Piece.__fromdict__(v) for k, v in d["pieceuuid2piece"].items()},
2086
2098
  bonus_statuses=[BonusStatus.__fromdict__(x) for x in d["bonus_statuses"]],
@@ -2095,7 +2107,6 @@ class State(PClass):
2095
2107
  nodes=[Node.__fromdict__(n) for n in d["nodes"]],
2096
2108
  nodeuuid2idx=d["nodeuuid2idx"],
2097
2109
  edges=[BiEdge.__fromdict__(e) for e in d["edges"]],
2098
- edgetuple2uuid={tuple(item["k"]): item["v"] for item in d["edgetuple2uuid"]},
2099
2110
  regions=[Region.__fromdict__(r) for r in d["regions"]],
2100
2111
  legal_actions_3=[LegalAction.__fromdict__(x) for x in d["legal_actions_3"]],
2101
2112
  piles=[Pile.__fromdict__(p) for p in d["piles"]],
@@ -2603,12 +2614,6 @@ def getinitialstate(game_config):
2603
2614
 
2604
2615
  nodeuuid2idx = {node.uuid: idx for idx, node in enumerate(board_config.points)}
2605
2616
  edges = get_edges(rng, board_config, nodeuuid2idx)
2606
- edgetuple2uuid = {}
2607
- for edge in edges:
2608
- node_1_idx = nodeuuid2idx[edge.node_1_uuid]
2609
- node_2_idx = nodeuuid2idx[edge.node_2_uuid]
2610
- edge_tuple = (min(node_1_idx, node_2_idx), max(node_1_idx, node_2_idx))
2611
- edgetuple2uuid[edge_tuple] = edge.uuid
2612
2617
 
2613
2618
  idx2path = []
2614
2619
 
@@ -2658,7 +2663,6 @@ def getinitialstate(game_config):
2658
2663
  nodes = nodes,
2659
2664
  nodeuuid2idx = nodeuuid2idx,
2660
2665
  edges = edges,
2661
- edgetuple2uuid = edgetuple2uuid,
2662
2666
  regions = get_regions(board_config),
2663
2667
  legal_actions_3=[],
2664
2668
  piles=piles,
@@ -3188,15 +3192,16 @@ def find_player_with_longest_path(game):
3188
3192
  return game.players[longest_path_player_idx]
3189
3193
 
3190
3194
 
3191
- def calc_path_len_from_edges(game, edge_tuples):
3195
+ @dispatch(State, set)
3196
+ def calc_path_len_from_edges(state, edge_tuples):
3192
3197
  if edge_tuples is None:
3193
3198
  return 0
3194
3199
  edge_lens = []
3195
3200
  for edge_tuple in edge_tuples:
3196
- edge_uuid = game.edgetuple2uuid.get(edge_tuple)
3197
- edge_idx = game.edgeuuid2idx.get(edge_uuid)
3201
+ edge_uuid = state.kernel.edgetuple2uuid.get(edge_tuple)
3202
+ edge_idx = state.kernel.edgeuuid2idx.get(edge_uuid)
3198
3203
  if edge_idx is not None:
3199
- edge = game.edges[edge_idx]
3204
+ edge = state.edges[edge_idx]
3200
3205
  if edge and edge.paths:
3201
3206
  first_path = edge.paths[0]
3202
3207
  edge_len = len(first_path.segments)
@@ -3564,7 +3569,7 @@ def is_move_pieces_to_path_action_legal(game, action):
3564
3569
  # print("******************************1234 is_move_pieces_to_path_action_legal 1d: ", len(remaining_segments))
3565
3570
 
3566
3571
  card_fulfillment, piece_fulfillment = get_path_fulfillment_from_resources(
3567
- game,
3572
+ game.kernel,
3568
3573
  proposed_cards,
3569
3574
  proposed_pieces,
3570
3575
  remaining_segments,
@@ -3714,24 +3719,25 @@ HOOK_NAMESPACE = {
3714
3719
  'ActionMovePiecesToPath': ActionMovePiecesToPathOptional,
3715
3720
  }
3716
3721
 
3717
-
3718
- def get_wild_unit_uuids(game):
3722
+ @dispatch(StateKernel)
3723
+ def get_wild_unit_uuids(state_kernel):
3719
3724
  wild_unit_uuids = []
3720
- for card in game.carduuid2card.values():
3725
+ for card in state_kernel.carduuid2card.values():
3721
3726
  if card.is_wild:
3722
3727
  wild_unit_uuids.append(card.resource_uuid)
3723
3728
  return wild_unit_uuids
3724
3729
 
3725
3730
 
3726
- def match_strict_wild(game, fulfillment, cards, segments):
3731
+ @dispatch(StateKernel, list, list, list)
3732
+ def match_strict_wild(state_kernel, fulfillment, cards, segments):
3727
3733
  new_fulfillment = fulfillment.copy()
3728
3734
  new_cards = cards.copy()
3729
- wild_unit_uuids = get_wild_unit_uuids(game)
3735
+ wild_unit_uuids = get_wild_unit_uuids(state_kernel)
3730
3736
  new_segments = []
3731
3737
 
3732
3738
  for segment in segments:
3733
3739
  if segment.unit_uuid and segment.unit_uuid in wild_unit_uuids:
3734
- first_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if game.carduuid2card[card_uuid].resource_uuid == segment.unit_uuid), None)
3740
+ first_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if state_kernel.carduuid2card[card_uuid].resource_uuid == segment.unit_uuid), None)
3735
3741
  if first_matching_idx is not None:
3736
3742
  new_fulfillment.append(new_cards.pop(first_matching_idx))
3737
3743
  else:
@@ -3741,16 +3747,16 @@ def match_strict_wild(game, fulfillment, cards, segments):
3741
3747
 
3742
3748
  return new_fulfillment, new_cards, new_segments
3743
3749
 
3744
-
3745
- def match_non_wild_non_empty(game, fulfillment, cards, segments, log=False):
3750
+ @dispatch(StateKernel, list, list, list)
3751
+ def match_non_wild_non_empty(state_kernel, fulfillment, cards, segments):
3746
3752
  new_fulfillment = fulfillment.copy()
3747
3753
  new_cards = cards.copy()
3748
- wild_unit_uuids = get_wild_unit_uuids(game)
3754
+ wild_unit_uuids = get_wild_unit_uuids(state_kernel)
3749
3755
  new_segments = []
3750
3756
 
3751
3757
  for segment in segments:
3752
- first_strict_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if game.carduuid2card[card_uuid].resource_uuid == segment.unit_uuid), None)
3753
- first_wild_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if game.carduuid2card[card_uuid].resource_uuid in wild_unit_uuids), None)
3758
+ first_strict_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if state_kernel.carduuid2card[card_uuid].resource_uuid == segment.unit_uuid), None)
3759
+ first_wild_matching_idx = next((i for i, card_uuid in enumerate(new_cards) if state_kernel.carduuid2card[card_uuid].resource_uuid in wild_unit_uuids), None)
3754
3760
  first_matching_idx = first_strict_matching_idx if first_strict_matching_idx is not None else first_wild_matching_idx
3755
3761
  if first_matching_idx is not None:
3756
3762
  new_fulfillment.append(new_cards.pop(first_matching_idx))
@@ -3760,14 +3766,13 @@ def match_non_wild_non_empty(game, fulfillment, cards, segments, log=False):
3760
3766
  return new_fulfillment, new_cards, new_segments
3761
3767
 
3762
3768
 
3763
-
3764
-
3765
- def match_empty(game, fulfillment, cards, segments):
3769
+ @dispatch(StateKernel, list, list, list)
3770
+ def match_empty(state_kernel, fulfillment, cards, segments):
3766
3771
  num_empty_segments = sum(1 for segment in segments if segment.unit_uuid is None)
3767
3772
  if num_empty_segments == 0:
3768
3773
  return fulfillment, cards, segments
3769
3774
 
3770
- tuples = get_uniform_sets(game, cards, num_empty_segments)
3775
+ tuples = get_uniform_sets(state_kernel, cards, num_empty_segments)
3771
3776
  # print(f"****************************************** len(cards): {len(cards)}")
3772
3777
  # print(f"****************************************** num_empty_segments: {num_empty_segments}")
3773
3778
  # print(f"Found {len(tuples)} tuples for empty segments: {tuples}")
@@ -3788,15 +3793,16 @@ def match_empty(game, fulfillment, cards, segments):
3788
3793
  return new_fulfillment, new_cards, new_segments
3789
3794
 
3790
3795
 
3791
- def get_uniform_sets(game, cards, min_length):
3792
- wilds = [card_uuid for card_uuid in cards if game.carduuid2card[card_uuid].is_wild]
3793
- non_wilds = [card_uuid for card_uuid in cards if not game.carduuid2card[card_uuid].is_wild]
3796
+ @dispatch(StateKernel, list, int)
3797
+ def get_uniform_sets(state_kernel, cards, min_length):
3798
+ wilds = [card_uuid for card_uuid in cards if state_kernel.carduuid2card[card_uuid].is_wild]
3799
+ non_wilds = [card_uuid for card_uuid in cards if not state_kernel.carduuid2card[card_uuid].is_wild]
3794
3800
  # print("********************* cards: ", cards)
3795
3801
  # print("********************* wilds: ", wilds)
3796
3802
  # print("********************* non_wilds: ", non_wilds)
3797
3803
  unit_uuid_2_cards = {}
3798
3804
  for card_uuid in non_wilds:
3799
- card = game.carduuid2card[card_uuid]
3805
+ card = state_kernel.carduuid2card[card_uuid]
3800
3806
  if card.resource_uuid not in unit_uuid_2_cards:
3801
3807
  unit_uuid_2_cards[card.resource_uuid] = []
3802
3808
  unit_uuid_2_cards[card.resource_uuid].append(card_uuid)
@@ -3859,8 +3865,8 @@ def get_sample_path_fulfillment(game, player_idx, path_idx):
3859
3865
  remaining_segments,
3860
3866
  )
3861
3867
 
3862
-
3863
- def get_path_fulfillment_from_resources(game, remaining_card_uuids, remaining_pieces, remaining_segments, log=False):
3868
+ @dispatch(StateKernel, list, list, list)
3869
+ def get_path_fulfillment_from_resources(state_kernel, remaining_card_uuids, remaining_pieces, remaining_segments, log=False):
3864
3870
 
3865
3871
  if len(remaining_pieces) < len(remaining_segments):
3866
3872
  print("Not enough pieces to fulfill the path segments")
@@ -3869,7 +3875,7 @@ def get_path_fulfillment_from_resources(game, remaining_card_uuids, remaining_pi
3869
3875
  piece_fulfillment = remaining_pieces[:len(remaining_segments)]
3870
3876
 
3871
3877
  card_fulfillment = []
3872
- card_fulfillment, remaining_card_uuids, remaining_segments = match_strict_wild(game, card_fulfillment, remaining_card_uuids, remaining_segments)
3878
+ card_fulfillment, remaining_card_uuids, remaining_segments = match_strict_wild(state_kernel, card_fulfillment, remaining_card_uuids, remaining_segments)
3873
3879
 
3874
3880
  if len(remaining_segments) == 0:
3875
3881
  return card_fulfillment, piece_fulfillment
@@ -3877,14 +3883,14 @@ def get_path_fulfillment_from_resources(game, remaining_card_uuids, remaining_pi
3877
3883
  # elif len(get_wild_segments(remaining_segments)) > 0:
3878
3884
  # return None
3879
3885
 
3880
- card_fulfillment, remaining_card_uuids, remaining_segments = match_non_wild_non_empty(game, card_fulfillment, remaining_card_uuids, remaining_segments, log)
3886
+ card_fulfillment, remaining_card_uuids, remaining_segments = match_non_wild_non_empty(state_kernel, card_fulfillment, remaining_card_uuids, remaining_segments)
3881
3887
  if len(remaining_segments) == 0:
3882
3888
  return card_fulfillment, piece_fulfillment
3883
3889
  # Probably don't need this check, but we should unit test
3884
3890
  # elif len(get_non_wild_non_empty_segments(remaining_segments)) > 0:
3885
3891
  # return None
3886
3892
 
3887
- card_fulfillment, remaining_card_uuids, remaining_segments = match_empty(game, card_fulfillment, remaining_card_uuids, remaining_segments)
3893
+ card_fulfillment, remaining_card_uuids, remaining_segments = match_empty(state_kernel, card_fulfillment, remaining_card_uuids, remaining_segments)
3888
3894
  if len(remaining_segments) == 0:
3889
3895
  return card_fulfillment, piece_fulfillment
3890
3896
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: graph_games_proto
3
- Version: 0.3.1935
3
+ Version: 0.3.1952
4
4
  Requires-Dist: multipledispatch==1.0.0
5
5
  Requires-Dist: pyrsistent==0.20.0
6
6
  Requires-Dist: numpy==2.2.4
@@ -1,9 +1,9 @@
1
1
  graph_games_proto/__init__.py,sha256=_EVQR-51XehfH45XZlba1WPdx3omS3Gm1nTwrgGyn2Q,667
2
2
  graph_games_proto/all_types.py,sha256=IpbwftEcHS5Ewz-saFNk0lO9FvcbuHG36odRTayCXUk,54911
3
- graph_games_proto/fns.py,sha256=Z1wk8lPk39U7qe84dnL749mSIcG-xiIiPKFjCG0mUGE,182031
3
+ graph_games_proto/fns.py,sha256=k9npdHPzXr009dTSA_iJeTC7GiZj8TYohJlLyp1wULg,182582
4
4
  graph_games_proto/main.py,sha256=fj2U7KcwrpZtuUhjOX5yVxY18LZvvsxDFYZ_S5mxe04,145
5
5
  graph_games_proto/state.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- graph_games_proto-0.3.1935.dist-info/METADATA,sha256=LWnt0sLT7V7SwgFk3_hXmRXORmEu0mZQshFGugg-sTw,188
7
- graph_games_proto-0.3.1935.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
8
- graph_games_proto-0.3.1935.dist-info/top_level.txt,sha256=-4QSrBMf_MM4BGsr2QXBpqDx8c8k_OPnzGyFjqjakes,18
9
- graph_games_proto-0.3.1935.dist-info/RECORD,,
6
+ graph_games_proto-0.3.1952.dist-info/METADATA,sha256=5BHTXHDTovOFX5UTg17YNTXEHi2_Q1vTPpfCl6vJkwA,188
7
+ graph_games_proto-0.3.1952.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
8
+ graph_games_proto-0.3.1952.dist-info/top_level.txt,sha256=-4QSrBMf_MM4BGsr2QXBpqDx8c8k_OPnzGyFjqjakes,18
9
+ graph_games_proto-0.3.1952.dist-info/RECORD,,