コード例 #1
0
def dumps(data: dict, environment: Environment, **kwargs):
    """
    Dumps full_data given into dumps directory
    :param environment:
    :param data:
    :return:
    """

    timestamp = int(time.time())

    # Get environment name in snake case
    environment = um.str_to_snake_case(environment.__class__.__name__)

    # Get only first letter of each word
    env_name_abbr = ''.join([word[0] for word in environment.split('_')])

    columns = kwargs.get('columns')

    if columns:
        # Specify full path
        file_path = dumps_path.joinpath(
            'bn/train_data/{}_bn_{}_{}_{}.yml'.format(env_name_abbr, timestamp,
                                                      Vector.decimal_precision,
                                                      columns))
    else:
        # Specify full path
        file_path = dumps_path.joinpath('bn/train_data/{}_bn_{}_{}.yml'.format(
            env_name_abbr, timestamp, Vector.decimal_precision))

    # If any parents doesn't exist, make it.
    file_path.parent.mkdir(parents=True, exist_ok=True)

    with file_path.open(mode='w+', encoding='UTF-8') as f:
        f.write(um.structures_to_yaml(data=data))
コード例 #2
0
ファイル: draft_mpq.py プロジェクト: Pozas91/tiadas
def train_from_file():
    # Models Path
    models_path = 'mpq/models/dstrds_1579869395_1.0_4.bin'

    agent: AgentMPQ = u_models.binary_load(
        path=dumps_path.joinpath(models_path))

    # Data Path
    data_path = dumps_path.joinpath(
        'mpq/train_data/dstrds_1579869395_1.0_4.yml')
    data_file = data_path.open(mode='r', encoding='UTF-8')

    # Load yaml from file
    data = yaml.load(data_file, Loader=yaml.FullLoader)

    # Extract relevant data for training
    before_training_execution = float(data['time'])
    decimal_precision = float(data['agent']['decimal_precision'])
    graph_type = GraphType.from_string(data['training']['graph_type'])
    limit = int(data['training']['limit'])
    columns = int(data['environment']['columns'])

    # Set decimal precision
    Vector.set_decimal_precision(decimal_precision=decimal_precision)

    # Time train
    t0 = time.time()

    # Agent training
    agent.train(graph_type=graph_type, limit=limit)

    # Calc total time
    total_time = (time.time() - t0) + before_training_execution

    prepare_for_dumps(agent, columns, decimal_precision, graph_type, limit,
                      total_time)
コード例 #3
0
    def load(filename: str, mode: str = 'binary', **kwargs) -> 'Agent':
        """
        This method load an agent from filename given.
        At moment only can load in binary mode
        :param filename: Filename from load the model.
        :param mode:
        :return:
        """

        if mode == 'binary':
            agent = u_models.binary_load(path=dumps_path.joinpath(filename))
        else:
            raise ValueError('Indicate mode don\'t recognize.')

        return agent
コード例 #4
0
    def save(self, mode: str = 'binary', filename: str = None) -> None:
        """
        Dumps model given into dumps directory
        At moment only can save in binary mode
        :param filename:
        :param mode:
        :return:
        """

        if filename is None:
            filename = self.default_filename(mode=mode)

        # Define file path
        file_path = dumps_path.joinpath(filename)

        # If does not exists make it.
        file_path.parent.mkdir(parents=True, exist_ok=True)

        if mode == 'binary':
            # Dumps model in binary mode
            u_models.binary_save(path=file_path, model=self)
        else:
            raise ValueError('Indicate mode don\'t recognize.')
コード例 #5
0
ファイル: agent_b.py プロジェクト: Pozas91/tiadas
    def dumps(self):
        """
        Dumps agent data into dumps directory
        :return:
        """

        # Calc total time
        total_time = time.time() - self.initial_time

        if self.dumps_format == 'yml':

            # Convert states_vectors (and tuples into strings)
            vectors = {
                um.tuples_to_string(key):
                [vector.tolist() for vector in vectors]
                for key, vectors in self.states_vectors.items()
            }

            # Convert initial state
            initial_state = um.tuples_to_string(self.environment.initial_state)

        else:
            # Convert states_vectors
            vectors = {
                key: [vector.tolist() for vector in vectors]
                for key, vectors in self.states_vectors.items()
            }

            # Convert initial state
            initial_state = self.environment.initial_state

        # Prepare data to dumps
        data = {
            'time': '{}s'.format(total_time),
            'memory': {
                'v_s_0': len(vectors[initial_state]),
                'full': sum(len(vectors) for vectors in vectors.values())
            },
            'vectors': vectors
        }

        # Extract timestamp
        timestamp = int(time.time())

        # Get environment name in snake case
        env_name = um.str_to_snake_case(self.environment.__class__.__name__)

        # Get only first letter of each word
        env_name_abbr = ''.join([word[0] for word in env_name.split('_')])

        # Specify full path
        if self.limited_precision:
            agent_path = 'b_lp/train_data/{}_{}_{}.yml'.format(
                env_name_abbr, Vector.decimal_precision, timestamp)
        else:
            agent_path = 'b/train_data/{}_{}.yml'.format(
                env_name_abbr, timestamp)

        file_path = dumps_path.joinpath(agent_path)

        # If any parents doesn't exist, make it.
        file_path.parent.mkdir(parents=True, exist_ok=True)

        with file_path.open(mode='w+', encoding='UTF-8') as f:

            if self.dumps_format == 'yml':
                f.write(um.structures_to_yaml(data=data))
            else:
                f.write(json.dumps(data))