File size: 2,591 Bytes
42d9709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import gymnasium as gym
import json
import os
from abc import ABC, abstractmethod
from datetime import datetime
from typing import Dict, Optional

JSON_INDENT = 4


class EvaluatorBase(ABC):
    """
    Base class for all evaluators. An evaluator tracks the performance of a task over a series of demos.
    """

    def __init__(self, checkpoint_name: str, eval_file_path: Optional[str] = None, seed: int = 10) -> None:
        """
        Initializes the EvaluatorBase object.

        Args:
            eval_file_path (os.path, optional): The path where the the evaluation file should be stored.
                                                Defaults to None (which means no evaluation file will be stored).
            checkpoint_name (str, optional): Name of checkpoint used for evaluation.
        """
        if eval_file_path is not None:
            assert os.path.exists(os.path.dirname(eval_file_path))
        self.eval_file_path = eval_file_path
        self.eval_dict = {}
        self.eval_dict["metadata"] = {
            "checkpoint_name": checkpoint_name,
            "seed": seed,
            "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        }

    @abstractmethod
    def evaluate_step(self, env: gym.Env) -> None:
        """
        Evaluates the current state of the task.

        Args:
            observed_state (State): The observed state of the environment.
            env (gym.Env): The environment in which the cube stacking task is being evaluated.
        """
        pass

    def maybe_write_eval_file(self):
        """
        If the evaluation file is set, the eval dict will be written to it.
        """
        if self.eval_file_path is not None:
            with open(self.eval_file_path, "w") as json_file:
                json.dump(self.eval_dict, json_file, indent=JSON_INDENT)

    @abstractmethod
    def summarize_demos(self) -> Dict:
        pass