2021-04-22 17:08:43 +02:00
|
|
|
#####################################################
|
|
|
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.04 #
|
|
|
|
#####################################################
|
|
|
|
import math
|
|
|
|
import abc
|
|
|
|
import numpy as np
|
2021-04-26 14:16:38 +02:00
|
|
|
from typing import List, Optional, Dict
|
2021-04-22 17:08:43 +02:00
|
|
|
import torch
|
|
|
|
import torch.utils.data as data
|
|
|
|
|
2021-04-26 14:16:38 +02:00
|
|
|
from .synthetic_utils import TimeStamp
|
2021-04-22 17:08:43 +02:00
|
|
|
|
|
|
|
|
2021-04-26 14:16:38 +02:00
|
|
|
class SyntheticDEnv(data.Dataset):
|
2021-04-22 17:08:43 +02:00
|
|
|
"""The synethtic dynamic environment."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-04-26 14:16:38 +02:00
|
|
|
mean_functors: List[data.Dataset],
|
|
|
|
cov_functors: List[List[data.Dataset]],
|
2021-04-22 17:08:43 +02:00
|
|
|
num_per_task: int = 5000,
|
2021-04-26 15:16:08 +02:00
|
|
|
timestamp_config: Optional[Dict] = None,
|
2021-04-22 17:08:43 +02:00
|
|
|
mode: Optional[str] = None,
|
|
|
|
):
|
2021-04-26 14:16:38 +02:00
|
|
|
self._ndim = len(mean_functors)
|
2021-04-22 17:08:43 +02:00
|
|
|
assert self._ndim == len(
|
2021-04-26 14:16:38 +02:00
|
|
|
cov_functors
|
|
|
|
), "length does not match {:} vs. {:}".format(self._ndim, len(cov_functors))
|
|
|
|
for cov_functor in cov_functors:
|
2021-04-22 17:08:43 +02:00
|
|
|
assert self._ndim == len(
|
2021-04-26 14:16:38 +02:00
|
|
|
cov_functor
|
|
|
|
), "length does not match {:} vs. {:}".format(self._ndim, len(cov_functor))
|
2021-04-22 17:08:43 +02:00
|
|
|
self._num_per_task = num_per_task
|
2021-04-26 15:16:08 +02:00
|
|
|
if timestamp_config is None:
|
|
|
|
timestamp_config = dict(mode=mode)
|
2021-04-26 14:16:38 +02:00
|
|
|
else:
|
2021-04-26 15:16:08 +02:00
|
|
|
timestamp_config["mode"] = mode
|
2021-04-22 17:08:43 +02:00
|
|
|
|
2021-04-26 15:16:08 +02:00
|
|
|
self._timestamp_generator = TimeStamp(**timestamp_config)
|
2021-04-22 17:08:43 +02:00
|
|
|
|
2021-04-26 14:16:38 +02:00
|
|
|
self._mean_functors = mean_functors
|
|
|
|
self._cov_functors = cov_functors
|
2021-04-22 17:08:43 +02:00
|
|
|
|
2021-04-27 14:09:37 +02:00
|
|
|
self._oracle_map = None
|
|
|
|
|
2021-04-28 17:56:25 +02:00
|
|
|
@property
|
|
|
|
def min_timestamp(self):
|
|
|
|
return self._timestamp_generator.min_timestamp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_timestamp(self):
|
|
|
|
return self._timestamp_generator.max_timestamp
|
|
|
|
|
2021-05-10 08:14:06 +02:00
|
|
|
@property
|
|
|
|
def timestamp_interval(self):
|
|
|
|
return self._timestamp_generator.interval
|
|
|
|
|
2021-05-13 09:32:44 +02:00
|
|
|
def get_timestamp(self, index):
|
|
|
|
index, timestamp = self._timestamp_generator[index]
|
|
|
|
return timestamp
|
|
|
|
|
2021-04-27 14:09:37 +02:00
|
|
|
def set_oracle_map(self, functor):
|
|
|
|
self._oracle_map = functor
|
|
|
|
|
2021-04-22 17:08:43 +02:00
|
|
|
def __iter__(self):
|
|
|
|
self._iter_num = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
if self._iter_num >= len(self):
|
|
|
|
raise StopIteration
|
|
|
|
self._iter_num += 1
|
|
|
|
return self.__getitem__(self._iter_num - 1)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
assert 0 <= index < len(self), "{:} is not in [0, {:})".format(index, len(self))
|
2021-04-26 14:16:38 +02:00
|
|
|
index, timestamp = self._timestamp_generator[index]
|
2021-05-10 08:14:06 +02:00
|
|
|
return self.__call__(timestamp)
|
|
|
|
|
|
|
|
def __call__(self, timestamp):
|
2021-04-26 14:16:38 +02:00
|
|
|
mean_list = [functor(timestamp) for functor in self._mean_functors]
|
2021-04-22 17:08:43 +02:00
|
|
|
cov_matrix = [
|
2021-04-28 17:56:25 +02:00
|
|
|
[abs(cov_gen(timestamp)) for cov_gen in cov_functor]
|
2021-04-26 14:16:38 +02:00
|
|
|
for cov_functor in self._cov_functors
|
2021-04-22 17:08:43 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
dataset = np.random.multivariate_normal(
|
|
|
|
mean_list, cov_matrix, size=self._num_per_task
|
|
|
|
)
|
2021-04-27 14:09:37 +02:00
|
|
|
if self._oracle_map is None:
|
|
|
|
return timestamp, torch.Tensor(dataset)
|
|
|
|
else:
|
|
|
|
targets = self._oracle_map.noise_call(dataset, timestamp)
|
|
|
|
return timestamp, (torch.Tensor(dataset), torch.Tensor(targets))
|
2021-04-22 17:08:43 +02:00
|
|
|
|
|
|
|
def __len__(self):
|
2021-04-26 14:16:38 +02:00
|
|
|
return len(self._timestamp_generator)
|
2021-04-22 17:08:43 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "{name}({cur_num:}/{total} elements, ndim={ndim}, num_per_task={num_per_task})".format(
|
|
|
|
name=self.__class__.__name__,
|
|
|
|
cur_num=len(self),
|
2021-04-26 14:16:38 +02:00
|
|
|
total=len(self._timestamp_generator),
|
2021-04-22 17:08:43 +02:00
|
|
|
ndim=self._ndim,
|
|
|
|
num_per_task=self._num_per_task,
|
|
|
|
)
|