forked from VSharp-team/VSharp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
79 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file removed
BIN
-47.2 KB
VSharp.ML.AIAgent/ml/imported/GNN_state_pred_het_dict_StateGNNEncoderConvEdgeAttr_32ch.zip
Binary file not shown.
Binary file removed
BIN
-59.7 KB
VSharp.ML.AIAgent/ml/imported/GNN_state_pred_het_full_StateGNNEncoderConvEdgeAttr_32ch.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from collections import namedtuple | ||
|
||
import torch | ||
from torch_geometric.data import HeteroData | ||
|
||
from config import GeneralConfig | ||
|
||
StateVectorMapping = namedtuple("StateVectorMapping", ["state", "vector"]) | ||
|
||
|
||
def predict_state_with_dict( | ||
model: torch.nn.Module, data: HeteroData, state_map: dict[int, int] | ||
) -> int: | ||
"""Gets state id from model and heterogeneous graph | ||
data.state_map - maps real state id to state index""" | ||
|
||
data.to(GeneralConfig.DEVICE) | ||
reversed_state_map = {v: k for k, v in state_map.items()} | ||
|
||
with torch.no_grad(): | ||
out = model( | ||
data.x_dict["game_vertex"], | ||
data.x_dict["state_vertex"], | ||
data.edge_index_dict["game_vertex_to_game_vertex"], | ||
data["game_vertex_to_game_vertex"].edge_type, | ||
data["game_vertex_history_state_vertex"].edge_index, | ||
data["game_vertex_history_state_vertex"].edge_attr, | ||
data["game_vertex_in_state_vertex"].edge_index, | ||
data["state_vertex_parent_of_state_vertex"].edge_index, | ||
) | ||
|
||
remapped = [] | ||
|
||
for index, vector in enumerate(out["state_vertex"]): | ||
state_vector_mapping = StateVectorMapping( | ||
state=reversed_state_map[index], | ||
vector=(vector.detach().cpu().numpy()).tolist(), | ||
) | ||
remapped.append(state_vector_mapping) | ||
|
||
return max(remapped, key=lambda mapping: sum(mapping.vector)).state, out | ||
|
||
|
||
def predict_state_single_out( | ||
model: torch.nn.Module, data: HeteroData, state_map: dict[int, int] | ||
) -> int: | ||
"""Gets state id from model and heterogeneous graph | ||
data.state_map - maps real state id to state index""" | ||
|
||
data.to(GeneralConfig.DEVICE) | ||
reversed_state_map = {v: k for k, v in state_map.items()} | ||
|
||
with torch.no_grad(): | ||
out = model.forward(data.x_dict, data.edge_index_dict, data.edge_attr_dict) | ||
|
||
remapped = [] | ||
if type(out) is dict: | ||
out = out["state_vertex"] | ||
for index, vector in enumerate(out): | ||
state_vector_mapping = StateVectorMapping( | ||
state=reversed_state_map[index], | ||
vector=(vector.detach().cpu().numpy()).tolist(), | ||
) | ||
remapped.append(state_vector_mapping) | ||
|
||
return max(remapped, key=lambda mapping: sum(mapping.vector)).state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters