Skip to content

Commit

Permalink
Fix bugs in simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryolite committed Mar 23, 2022
1 parent 9ec60ce commit 4e313ca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
65 changes: 54 additions & 11 deletions kanachan/simulation/test_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#!/usr/bin/env python3

import math
from typing import List
import sys
from traceback import print_exc
import torch
from kanachan.training.constants import (
NUM_TYPES_OF_SPARSE_FEATURES, MAX_NUM_ACTIVE_SPARSE_FEATURES,
NUM_TYPES_OF_PROGRESSION_FEATURES, MAX_LENGTH_OF_PROGRESSION_FEATURES,
NUM_TYPES_OF_ACTIONS, MAX_NUM_ACTION_CANDIDATES,)


class TestModel(object):
Expand All @@ -11,7 +17,7 @@ def __init__(self, rounds: List[dict]) -> None:
self.__round_index = 0
self.__decision_index = 0

def __call__(self, features: List[List[int]]) -> int:
def __call__(self, features: List[List[int]]) -> torch.Tensor:
if len(features) != 4:
raise ValueError(len(features))
sparse = features[0]
Expand All @@ -35,24 +41,56 @@ def __call__(self, features: List[List[int]]) -> int:
continue
decision = decisions[self.__decision_index]
error = False
if sparse != decision['sparse']:
message = ''
sparse = torch.squeeze(sparse)
sparse_ = list(decision['sparse'])
for i in range(len(sparse_), MAX_NUM_ACTIVE_SPARSE_FEATURES):
# Padding.
sparse_.append(NUM_TYPES_OF_SPARSE_FEATURES)
sparse_ = torch.tensor(
sparse_, device=sparse.device, dtype=sparse.dtype)
if torch.any(sparse != sparse_).item():
error = True
if numeric != decision['numeric']:
message += "sparse != decision['sparse']\n"
numeric = torch.squeeze(numeric)
numeric_ = list(decision['numeric'])
numeric_[2:] = [float(x) / 10000.0 for x in numeric_[2:]]
numeric_ = torch.tensor(
numeric_, device=numeric.device, dtype=numeric.dtype)
if torch.any(numeric != numeric_).item():
error = True
if progression != decision['progression']:
message += "numeric != decision['numeric']\n"
progression = torch.squeeze(progression)
progression_ = list(decision['progression'])
for i in range(len(progression_), MAX_LENGTH_OF_PROGRESSION_FEATURES):
# Padding.
progression_.append(NUM_TYPES_OF_PROGRESSION_FEATURES)
progression_ = torch.tensor(
progression_, device=progression.device,
dtype=progression.dtype)
if torch.any(progression != progression_).item():
error = True
if candidates != decision['candidates']:
message += "progression != decision['progression']\n"
candidates = torch.squeeze(candidates)
candidates_ = list(decision['candidates'])
for i in range(len(candidates_), MAX_NUM_ACTION_CANDIDATES):
# Padding.
candidates_.append(NUM_TYPES_OF_ACTIONS + 1)
candidates_ = torch.tensor(
candidates_, device=candidates.device, dtype=candidates.dtype)
if torch.any(candidates != candidates_).item():
error = True
message += "candidates != decision['candidates']\n"
if error:
message = f'''Decision error:
message += f'''Decision error:
sparse: {sparse}
sparse: {decision["sparse"]}
sparse: {sparse_}
numeric: {numeric}
numeric: {decision["numeric"]}
numeric: {numeric_}
progression: {progression}
progression: {decision["progression"]}
progression: {progression_}
candidates: {candidates}
candidates: {decision["candidates"]}
candidates: {candidates_}
index: {decision["index"]}
'''
if self.__decision_index >= 1:
Expand Down Expand Up @@ -80,6 +118,11 @@ def __call__(self, features: List[List[int]]) -> int:
if index >= len(decision['candidates']):
raise RuntimeError('Out of index.')
self.__decision_index += 1
return decision['candidates'][index]
prediction = torch.full(
(MAX_NUM_ACTION_CANDIDATES,), -math.inf, device=numeric.device,
dtype=numeric.dtype)
prediction[index] = 0.0
prediction = torch.unsqueeze(prediction, dim=0)
return prediction

raise RuntimeError('No more decision.')
2 changes: 1 addition & 1 deletion src/simulation/model_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::uint_fast16_t ModelWrapper::operator()(python::object features) const
python::object numeric = features[1];
for (long i = 2; i < constants.attr("NUM_NUMERIC_FEATURES"); ++i) {
// Scaling.
numeric[i] /= 10000.0;
numeric[i] = double(python::extract<long>(numeric[i])()) / 10000.0;
}
kwargs["device"] = device_;
kwargs["dtype"] = dtype_;
Expand Down
8 changes: 4 additions & 4 deletions test/annotation_vs_simulation/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def _on_leaf(test_file_path: Path):
{round_data["delta_scores"]}''')

final_ranking = [test_data['players'][i]['final_ranking'] for i in range(4)]
if result['final_ranking'] != final_ranking:
raise RuntimeError(f'{result["final_ranking"]} != {final_ranking}')
if result['ranking'] != final_ranking:
raise RuntimeError(f'{result["ranking"]} != {final_ranking}')

final_scores = [test_data['players'][i]['final_score'] for i in range(4)]
if result['final_scores'] != final_scores:
raise RuntimeError(f'{result["final_scores"]} != {final_scores}')
if result['scores'] != final_scores:
raise RuntimeError(f'{result["scores"]} != {final_scores}')

print('PASS')

Expand Down

0 comments on commit 4e313ca

Please sign in to comment.