repo
stringlengths
2
99
file
stringlengths
13
225
code
stringlengths
0
18.3M
file_length
int64
0
18.3M
avg_line_length
float64
0
1.36M
max_line_length
int64
0
4.26M
extension_type
stringclasses
1 value
pegnn
pegnn-master/src/datasets/csv_dataset.py
from typing import Iterator from torch_geometric.data import InMemoryDataset, Data from torch_geometric.loader import DataLoader import torch import pandas as pd import numpy as np from pymatgen.core.structure import Structure from pymatgen.io.ase import AseAtomsAdaptor from ase.neighborlist import neighbor_list from tqdm import tqdm from tqdm.contrib.concurrent import process_map from .data import CrystalData from src.models.layers.random import RandomMatrixSL3Z, apply_sl3z import multiprocessing as mp import warnings import os import json def process_cif(args): (cif, warning_queue) = args with warnings.catch_warnings(record=True) as ws: # Cause all warnings to always be triggered. warnings.simplefilter("always") struct = Structure.from_str(cif, fmt="cif") if warning_queue is not None: for w in ws: warning_queue.put((hash(str(w.message)), w)) lengths = np.array(struct.lattice.abc, dtype=np.float32) angles = np.array(struct.lattice.angles, dtype=np.float32) atoms = AseAtomsAdaptor.get_atoms(struct) atoms.set_scaled_positions(atoms.get_scaled_positions(wrap=True)) assert (0 <= atoms.get_scaled_positions()).all() and ( atoms.get_scaled_positions() < 1).all() cell = atoms.cell.array.astype(np.float32) z = np.array(struct.atomic_numbers, dtype=np.long) pos = struct.frac_coords.astype(np.float32) data = { "lattice": cell, "lengths": lengths, "angles": angles, "z": z, "pos": pos } return data, np.unique(z), pos.shape[0] class CSVDataset(InMemoryDataset): def __init__(self, csv_file: str, warn: bool = False, multithread: bool = True, verbose: bool = True, noise_scale: float = 0.1, knn: float = 8, sl3z_aug: bool = False): super().__init__() self._raw_file_names = [csv_file] df = pd.read_csv(csv_file) if warn: m = mp.Manager() warning_queue = m.Queue() else: warning_queue = None iterator = [(row["cif"], warning_queue) for _, row in df.iterrows()] if multithread: if verbose: result = process_map( process_cif, iterator, desc=f"loading dataset {csv_file}", chunksize=8) else: with mp.Pool(mp.cpu_count()) as p: result = p.map(process_cif, iterator) else: result = [] if verbose: iterator = tqdm( iterator, desc=f"loading dataset {csv_file}", total=len(df)) for args in iterator: result.append(process_cif(args)) if warn: warnings_type = {} while not warning_queue.empty(): key, warning = warning_queue.get() if key not in warnings_type: warnings_type[key] = warning for w in warnings_type.values(): warnings.warn_explicit( w.message, category=w.category, filename=w.filename, lineno=w.lineno ) self._elements = set( np.unique(np.concatenate([z for _, z, _ in result]))) size = np.array([s for _, _, s in result]) max_size = np.max(size) min_size = np.min(size) self.data = [c for c, _, _ in result] if verbose: print( f"dataset statistics: count={len(self.data)}, min={min_size}, max={max_size}") @ property def raw_file_names(self): return self._raw_file_names @ property def processed_file_names(self): return [] def len(self) -> int: return len(self.data) def get_sample_size(self, idx: int) -> int: return len(self.data[idx]["z"]) def get(self, idx: int) -> Data: lattice = torch.from_numpy(self.data[idx]["lattice"]).unsqueeze(0) z = torch.from_numpy(self.data[idx]["z"]) pos = torch.from_numpy(self.data[idx]["pos"]) return CrystalData( z=z, pos=pos, cell=lattice, num_atoms=z.shape[0] )
4,203
28.194444
172
py
pegnn
pegnn-master/src/utils/scaler.py
import torch import torch.nn as nn import numpy as np from torch_geometric.loader import DataLoader import tqdm from src.utils.geometry import Geometry from typing import Tuple class LatticeScaler(nn.Module): def __init__(self): super(LatticeScaler, self).__init__() self.mean = nn.Parameter( torch.zeros(6, dtype=torch.float32), requires_grad=False ) self.std = nn.Parameter(torch.ones( 6, dtype=torch.float32), requires_grad=False) def get_lattices_parameters( self, lattices: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: lengths = lattices.norm(dim=2) i = torch.tensor([0, 1, 2], dtype=torch.long, device=lattices.device) j = torch.tensor([1, 2, 0], dtype=torch.long, device=lattices.device) k = torch.tensor([2, 0, 1], dtype=torch.long, device=lattices.device) cross = torch.cross(lattices[:, j], lattices[:, k], dim=2) dot = (lattices[:, j] * lattices[:, k]).sum(dim=2) angles = torch.atan2(cross.norm(dim=2), dot) * 180 / torch.pi inv_mask = (cross * lattices[:, i]).sum(dim=2) < 0 angles[inv_mask] *= -1 return lengths, angles def get_lattices( self, lengths: torch.FloatTensor, angles: torch.FloatTensor ) -> torch.FloatTensor: """Converts lattice from abc, angles to matrix. https://github.com/materialsproject/pymatgen/blob/b789d74639aa851d7e5ee427a765d9fd5a8d1079/pymatgen/core/lattice.py#L311 """ a, b, c = lengths alpha, beta, gamma = angles angles_r = torch.deg2rad(torch.tensor([alpha, beta, gamma])) cos_alpha, cos_beta, cos_gamma = np.cos(angles_r) sin_alpha, sin_beta, sin_gamma = np.sin(angles_r) val = (cos_alpha * cos_beta - cos_gamma) / (sin_alpha * sin_beta) # Sometimes rounding errors result in values slightly > 1. # val = max(min(val, val), -val) gamma_star = torch.arccos(val) vector_a = [a * sin_beta, 0.0, a * cos_beta] vector_b = [ -b * sin_alpha * np.cos(gamma_star), b * sin_alpha * np.sin(gamma_star), b * cos_alpha, ] vector_c = [0.0, 0.0, float(c)] return torch.tensor([vector_a, vector_b, vector_c]) @torch.no_grad() def fit(self, dataloader: DataLoader, verbose: bool = True): lengths, angles = [], [] if verbose: iterator = tqdm.tqdm( dataloader, desc="calculating normalization paremeters") else: iterator = dataloader for batch in iterator: current_lengths, current_angles = self.get_lattices_parameters( batch.cell) lengths.append(current_lengths) angles.append(current_angles) lengths = torch.cat(lengths, dim=0) angles = torch.cat(angles, dim=0) params = torch.cat((lengths, angles), dim=1) self.mean.data = params.mean(dim=0) self.std.data = params.std(dim=0) def normalise_lattice( self, lattices: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: lengths, angles = self.get_lattices_parameters(lattices) lengths_scaled = (lengths - self.mean[:3]) / (self.std[:3]+1e-6) angles_scaled = (angles - self.mean[3:]) / (self.std[3:]+1e-6) return lengths_scaled, angles_scaled def normalise( self, lengths: torch.FloatTensor, angles: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: lengths_scaled = (lengths - self.mean[:3]) / (self.std[:3]+1e-6) angles_scaled = (angles - self.mean[3:]) / (self.std[3:]+1e-6) return lengths_scaled, angles_scaled def denormalise( self, lengths: torch.FloatTensor, angles: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: lengths_scaled = lengths * self.std[:3] + self.mean[:3] angles_scaled = angles * self.std[3:] + self.mean[3:] return lengths_scaled, angles_scaled class BondsScaler(nn.Module): def __init__(self, knn: int = 8): super().__init__() self.knn = knn self.edges_mean = nn.Parameter( torch.zeros(1, dtype=torch.float32), requires_grad=False ) self.edges_std = nn.Parameter( torch.ones(1, dtype=torch.float32), requires_grad=False ) self.triplets_mean = nn.Parameter( torch.zeros(3, dtype=torch.float32), requires_grad=False ) self.triplets_std = nn.Parameter( torch.ones(3, dtype=torch.float32), requires_grad=False ) @property def device(self): return self.edges_mean.data.device @torch.no_grad() def fit(self, dataloader: DataLoader): edges, triplets = [], [] for batch in tqdm.tqdm(dataloader): batch = batch.to(self.device) geometry = Geometry(batch.cell, batch.num_atoms, batch.pos, knn=self.knn) edges.append(geometry.edges_n_ij) triplets.append( torch.stack( ( geometry.triplets_cos_ijk, geometry.triplets_n_ij, geometry.triplets_n_ik, ), dim=1, ) ) edges = torch.cat(edges, dim=0) triplets = torch.cat(triplets, dim=0) self.edges_mean.data = edges.mean() self.edges_std.data = edges.std() self.triplets_mean.data = triplets.mean(dim=0) self.triplets_std.data = triplets.std(dim=0) def normalize( self, edges: torch.FloatTensor, triplets: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: edges_scaled = ( edges - self.edges_mean.data[None]) / self.edges_std.data[None] triplets_scaled = ( triplets - self.triplets_mean.data[None] ) / self.triplets_std.data[None] return edges_scaled, triplets_scaled def denormalize( self, edges: torch.FloatTensor, triplets: torch.FloatTensor ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: edges_scaled = edges * \ self.edges_std.data[None] + self.edges_mean.data[None] triplets_scaled = ( triplets * self.triplets_std.data[None] + self.triplets_mean.data[None] ) return edges_scaled, triplets_scaled
6,553
32.269036
128
py
pegnn
pegnn-master/src/utils/shape.py
import torch from typing import Tuple, List, Union, Dict from collections import namedtuple class shape: def __init__(self, *dim: Union[int, str], dtype=None): assert isinstance(dim, tuple) for d in dim: assert (type(d) == int and -1 <= d) or type(d) == str assert (dtype is None) or isinstance(dtype, torch.dtype) self.dim = dim self.dtype = dtype def get_dim(self, dim: List[Union[int, str]], context: Dict[str, int] = {}): dim_eval = [] for d in dim: if type(d) == str and (d in context): dim_eval.append(context[d]) else: dim_eval.append(d) return tuple(dim_eval) def assert_match(self, x: torch.Tensor, context: Dict[str, int] = {}): assert isinstance(x, torch.Tensor), "x is not a Tensor" assert x.dim() == len( self.dim ), f"the dimension of x should match with {self.dim}" for x_dim, trg_dim in zip(x.shape, self.dim): if (trg_dim is None) or (trg_dim == -1): continue if type(trg_dim) == str: if trg_dim in context: trg_dim = context[trg_dim] else: context[trg_dim] = x_dim continue assert ( x_dim == trg_dim ), f"the shape of x {tuple(x.shape)} should match with {self.get_dim(self.dim,context)}" if self.dtype is not None: assert ( x.dtype == self.dtype ), f"the data type of x ({x.dtype}) should match with {self.dtype}" return context def build_shapes(context: Dict[str, int]) -> namedtuple("shapes", tuple()): return namedtuple("shapes", context.keys())(*context.values()) def assert_tensor_match( *args: Tuple[torch.Tensor, shape] ) -> namedtuple("shapes", tuple()): context = {} for x, s in args: context = s.assert_match(x, context=context) return build_shapes(context)
2,051
27.901408
100
py
pegnn
pegnn-master/src/utils/polar.py
import torch import unittest __all__ = ["polar"] def polar(a: torch.FloatTensor, side: str = "right"): if side not in ["right", "left"]: raise ValueError("`side` must be either 'right' or 'left'") assert a.ndim == 3 and a.shape[1] == a.shape[2] w, s, vh = torch.linalg.svd(a, full_matrices=False) u = torch.bmm(w, vh) if side == "right": # a = up p = torch.bmm(torch.transpose(vh, 1, 2).conj() * s[:, None], vh) else: # a = pu p = torch.bmm(w * s[:, None], torch.transpose(w, 1, 2).conj()) mask = torch.where(torch.det(u) < 0, -1.0, 1.0) u *= mask[:, None, None] p *= mask[:, None, None] return u, p class TestPolar(unittest.TestCase): def test_left(self): import numpy as np from scipy.linalg import polar as polat_gt torch.manual_seed(0) A = torch.matrix_exp(torch.randn(1 << 10, 3, 3)) R, K = polar(A, side="left") for i in range(A.shape[0]): R_gt, K_gt = polat_gt(A[i].numpy(), side="left") R_gt = torch.from_numpy(R_gt) K_gt = torch.from_numpy(K_gt) if torch.det(R_gt) < 0: R_gt -= R_gt K_gt -= K_gt self.assertAlmostEqual((R[i] - R_gt).abs().sum().item(), 0.0, places=4) self.assertAlmostEqual((K[i] - K_gt).abs().sum().item(), 0.0, places=4) def test_right(self): import numpy as np from scipy.linalg import polar as polat_gt torch.manual_seed(0) A = torch.matrix_exp(torch.randn(1 << 10, 3, 3)) R, K = polar(A, side="right") for i in range(A.shape[0]): R_gt, K_gt = polat_gt(A[i].numpy(), side="right") R_gt = torch.from_numpy(R_gt) K_gt = torch.from_numpy(K_gt) if torch.det(R_gt) < 0: R_gt -= R_gt K_gt -= K_gt self.assertAlmostEqual((R[i] - R_gt).abs().sum().item(), 0.0, places=4) self.assertAlmostEqual((K[i] - K_gt).abs().sum().item(), 0.0, places=4) def volume(x: torch.FloatTensor) -> torch.FloatTensor: return (torch.cross(x[:, :, 0], x[:, :, 1]) * x[:, :, 2]).sum(dim=1).abs() def volume2(x): return torch.linalg.svd(x)[1].prod(dim=1).abs().detach() if __name__ == "__main__": from torch import tensor calc_scale = 1.5 rho = tensor( [ [-1.9330e-01, 3.3560e00, -2.1579e00], [6.8199e01, -3.8512e02, 2.6373e02], [-3.6272e01, 2.0426e02, -1.3885e02], ] ) actions_rho = tensor( [ [0.9919, 1.0756, -0.5697], [1.0756, -143.5471, 76.5437], [-0.5697, 76.5437, -39.5333], ] ) action_normalize = tensor( [ [0.1996, 0.2165, -0.1146], [0.2165, -28.8877, 15.4038], [-0.1146, 15.4038, -7.9557], ] ) rho.unsqueeze_(0) actions_rho.unsqueeze_(0) action_normalize.unsqueeze_(0) """ print( torch.cross( tensor([-1.9330e-01, 6.8199e01, -3.6272e01]), tensor([3.3560e00, -3.8512e02, 2.0426e02]), ) .dot(tensor([-2.1579e00, 2.6373e02, -1.3885e02])) .abs() ) print(volume2(rho)) print(volume(rho)) exit() """ print(torch.linalg.matrix_rank(rho)) print(torch.linalg.matrix_rank(actions_rho, hermitian=True)) U = torch.linalg.svd(rho).U[0] print( torch.dot(U[:, 0], U[:, 1]), torch.dot(U[:, 1], U[:, 2]), torch.dot(U[:, 2], U[:, 0]), ) U = torch.linalg.svd(actions_rho).U[0] print( torch.dot(U[:, 0], U[:, 1]), torch.dot(U[:, 1], U[:, 2]), torch.dot(U[:, 2], U[:, 0]), ) print(volume(action_normalize) * volume(rho)) print(volume(torch.bmm(action_normalize, rho))) print(volume2(torch.bmm(action_normalize, rho))) # unittest.main()
3,936
25.782313
83
py
pegnn
pegnn-master/src/utils/timeout.py
import signal class Timeout(Exception): pass class timeout: def __init__(self, seconds, error_message=None): if error_message is None: error_message = "test timed out after {}s.".format(seconds) self.seconds = seconds self.error_message = error_message def handle_timeout(self, signum, frame): raise Timeout(self.error_message) def __enter__(self): signal.signal(signal.SIGALRM, self.handle_timeout) signal.alarm(self.seconds) def __exit__(self, exc_type, exc_val, exc_tb): signal.alarm(0)
585
23.416667
71
py
pegnn
pegnn-master/src/utils/encoder.py
import torch import json import numpy as np from ase.spacegroup import Spacegroup __all__ = ["CrystalEncoder"] class CrystalEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() if isinstance(obj, torch.Tensor): return obj.tolist() if isinstance(obj, Spacegroup): return {"number": obj.no, "symbol": obj.symbol} # if isinstance(obj, tf.Tensor): # return obj.numpy().tolist() return json.JSONEncoder.default(self, obj)
561
27.1
59
py
pegnn
pegnn-master/src/utils/replay.py
import torch class Replay: def __init__(self, batch_size: int, max_depth: int = 32, proba_in: float = 0.1): self.batch_size = batch_size self.max_depth = max_depth self.proba_in = proba_in self.cell = torch.zeros(0, 3, 3, dtype=torch.float32) self.pos = torch.zeros(0, 3, dtype=torch.float32) self.z = torch.zeros(0, dtype=torch.float32) self.num_atoms = torch.zeros(0, dtype=torch.long) self.depth = 0 def push(self, cell, pos, z, num_atoms): if torch.rand(1) < self.proba_in: cell = cell.clone().detach().cpu() pos = pos.clone().detach().cpu() z = z.clone().detach().cpu() num_atoms = num_atoms.clone().detach().cpu() if self.depth < self.max_depth: self.cell = torch.cat((self.cell, cell)) self.pos = torch.cat((self.pos, pos)) self.z = torch.cat((self.z, z)) self.num_atoms = torch.cat((self.num_atoms, num_atoms)) self.depth += 1 else: struct_idx = torch.arange( self.num_atoms.shape[0], device=self.num_atoms.device ) batch = struct_idx.repeat_interleave(self.num_atoms) remove = torch.randint(self.depth, (1,)) * self.batch_size mask = (batch < remove) | ((remove + self.batch_size) <= batch) self.cell = torch.cat( (self.cell[:remove], self.cell[(remove + self.batch_size) :], cell) ) self.pos = torch.cat((self.pos[mask], pos)) self.z = torch.cat((self.z[mask], z)) self.num_atoms = torch.cat( ( self.num_atoms[:remove], self.num_atoms[(remove + self.batch_size) :], num_atoms, ) ) def random(self, device="cpu"): assert self.num_atoms.shape[0] > 0 struct_idx = torch.arange(self.num_atoms.shape[0], device=self.num_atoms.device) batch = struct_idx.repeat_interleave(self.num_atoms) idx = torch.randperm(self.num_atoms.shape[0])[: self.batch_size] mask = (batch[:, None] == idx[None, :]).any(dim=1) return ( self.cell[idx].to(device), self.pos[mask].to(device), self.z[mask].to(device), self.num_atoms[idx].to(device), )
2,510
36.477612
88
py
pegnn
pegnn-master/src/utils/geometry.py
import torch import torch.nn.functional as F from .shape import build_shapes, assert_tensor_match, shape from .timeout import timeout from dataclasses import dataclass import crystallographic_graph @dataclass(init=False) class Geometry: batch: torch.LongTensor batch_edges: torch.LongTensor batch_triplets: torch.LongTensor num_atoms: torch.LongTensor cell: torch.FloatTensor x: torch.FloatTensor lengths: torch.FloatTensor angles: torch.FloatTensor edges: crystallographic_graph.Edges edges_e_ij: torch.FloatTensor edges_v_ij: torch.FloatTensor edges_u_ij: torch.FloatTensor edges_r_ij: torch.FloatTensor triplets: crystallographic_graph.Triplets triplets_e_ij: torch.FloatTensor triplets_e_ik: torch.FloatTensor triplets_v_ij: torch.FloatTensor triplets_v_ik: torch.FloatTensor triplets_u_ij: torch.FloatTensor triplets_u_ik: torch.FloatTensor triplets_r_ij: torch.FloatTensor triplets_r_ik: torch.FloatTensor triplets_angle_ijk: torch.FloatTensor triplets_cos_ijk: torch.FloatTensor triplets_sin_ijk: torch.FloatTensor def __init__( self, cell: torch.FloatTensor, num_atoms: torch.LongTensor, x: torch.FloatTensor, mask: torch.BoolTensor = None, knn: int = 0, cutoff: float = 0, check_tensor: bool = True, edges: bool = True, triplets: bool = True, edges_idx: torch.LongTensor = None, edges_attr: torch.LongTensor = None, ): assert knn > 0 or cutoff > 0 if check_tensor: shapes = assert_tensor_match( (cell, shape("b", 3, 3, dtype=torch.float32)), (num_atoms, shape("b", dtype=torch.long)), (x, shape("n", 3, dtype=torch.float32)), ) else: shapes = build_shapes( { "b": cell.shape[0], "n": x.shape[0], } ) assert (edges_idx is None) == (edges_attr is None) self.num_atoms = num_atoms self.cell = cell self.x = x self.edges = None self.batch_edges = None self.triplets = None self.batch_triplets = None struct_idx = torch.arange(shapes.b, device=x.device) self.batch = struct_idx.repeat_interleave(num_atoms) if edges: if edges_idx is None: self.edges = crystallographic_graph.make_graph( self.cell, self.x, self.num_atoms, knn=knn, cutoff=cutoff ) self.batch_edges = self.batch[self.edges.src] else: self.edges = crystallographic_graph.Edges( src=edges_idx[0], dst=edges_idx[1], cell=edges_attr ) self.batch_edges = self.batch[self.edges.src] if triplets: self.triplets = crystallographic_graph.make_triplets( self.num_atoms, self.edges, check_tensor=check_tensor ) self.batch_triplets = self.batch[self.triplets.src] self.update_vectors() def get_cell_parameters(self, cell=None): if cell is None: cell = self.cell lengths = cell.norm(dim=2) cross = torch.cross(cell[:, [1, 2, 0]], cell[:, [2, 0, 1]], dim=2) dot = (cell[:, [1, 2, 0]] * cell[:, [2, 0, 1]]).sum(dim=2) angles = torch.atan2(cross.norm(dim=2), dot) return lengths, angles def filter_edges(self, mask: torch.BoolTensor): assert mask.shape == self.edges.src.shape self.batch_edges = self.batch_edges[mask] self.edges.src = self.edges.src[mask] self.edges.dst = self.edges.dst[mask] self.edges.cell = self.edges.cell[mask] self.edges_e_ij = self.edges_e_ij[mask] self.edges_v_ij = self.edges_v_ij[mask] self.edges_r_ij = self.edges_r_ij[mask] self.edges_u_ij = self.edges_u_ij[mask] def filter_triplets(self, mask: torch.BoolTensor): assert mask.shape == self.triplets.src.shape self.batch_triplets = self.batch_triplets[mask] self.triplets.src = self.triplets.src[mask] self.triplets.dst_i = self.triplets.dst_i[mask] self.triplets.cell_i = self.triplets.cell_i[mask] self.triplets.dst_j = self.triplets.dst_j[mask] self.triplets.cell_j = self.triplets.cell_j[mask] self.triplets_e_ij = self.triplets_e_ij[mask] self.triplets_v_ij = self.triplets_v_ij[mask] self.triplets_r_ij = self.triplets_r_ij[mask] self.triplets_u_ij = self.triplets_u_ij[mask] self.triplets_e_ik = self.triplets_e_ik[mask] self.triplets_v_ik = self.triplets_v_ik[mask] self.triplets_r_ik = self.triplets_r_ik[mask] self.triplets_u_ik = self.triplets_u_ik[mask] self.triplets_cos_ijk = self.triplets_cos_ijk[mask] self.triplets_sin_ijk = self.triplets_sin_ijk[mask] self.triplets_angle_ijk = self.triplets_angle_ijk[mask] def update_vectors(self, cell=None, x=None): if cell is None: cell = self.cell if x is None: x = self.x self.lengths, self.angles = self.get_cell_parameters() if self.edges is not None: self.edges_e_ij = ( x[self.edges.dst, :] - x[self.edges.src, :] + self.edges.cell ) edges_batch = self.batch[self.edges.src] self.edges_v_ij = torch.bmm( cell[edges_batch], self.edges_e_ij.unsqueeze(2) ).squeeze(2) self.edges_r_ij = self.edges_v_ij.norm(dim=1) self.edges_u_ij = self.edges_v_ij / self.edges_r_ij[:, None] if self.edges_r_ij.isinf().any(): raise Exception("infinite edges") else: empty_scalar = torch.empty( (0,), dtype=torch.float32, device=self.cell.device ) empty_vector = torch.empty( (0, 3), dtype=torch.float32, device=self.cell.device ) self.edges_e_ij = empty_vector self.edges_v_ij = empty_vector self.edges_r_ij = empty_scalar self.edges_u_ij = empty_vector if (self.triplets is not None) and (self.triplets.src.shape[0] > 0): self.triplets_e_ij = ( x[self.triplets.dst_i, :] - x[self.triplets.src, :] + self.triplets.cell_i ) self.triplets_e_ik = ( x[self.triplets.dst_j, :] - x[self.triplets.src, :] + self.triplets.cell_j ) triplets_batch = self.batch[self.triplets.src] self.triplets_v_ij = torch.bmm( cell[triplets_batch], self.triplets_e_ij.unsqueeze(2) ).squeeze(2) self.triplets_v_ik = torch.bmm( cell[triplets_batch], self.triplets_e_ik.unsqueeze(2) ).squeeze(2) self.triplets_r_ij = self.triplets_v_ij.norm(dim=1) self.triplets_r_ik = self.triplets_v_ik.norm(dim=1) self.triplets_u_ij = self.triplets_v_ij / ( self.triplets_r_ij[:, None] + 1e-12 ) self.triplets_u_ik = self.triplets_v_ik / ( self.triplets_r_ik[:, None] + 1e-12 ) self.triplets_cos_ijk = ( self.triplets_u_ij * self.triplets_u_ik).sum(dim=1) self.triplets_sin_ijk = torch.cross( self.triplets_u_ij, self.triplets_u_ik ).norm(dim=1) self.triplets_angle_ijk = torch.atan2( self.triplets_sin_ijk, self.triplets_cos_ijk ) else: empty_scalar = torch.empty( (0,), dtype=torch.float32, device=self.cell.device ) empty_vector = torch.empty( (0, 3), dtype=torch.float32, device=self.cell.device ) self.triplets_e_ij = empty_vector self.triplets_e_ik = empty_vector self.triplets_v_ij = empty_vector self.triplets_v_ik = empty_vector self.triplets_u_ij = empty_vector self.triplets_u_ik = empty_vector self.triplets_r_ij = empty_scalar self.triplets_r_ik = empty_scalar self.triplets_cos_ijk = empty_scalar self.triplets_sin_ijk = empty_scalar self.triplets_angle_ijk = empty_scalar if __name__ == "__main__": from ase.neighborlist import neighbor_list from ase.spacegroup import crystal import torch.nn as nn import numpy as np class RandomCrystal(nn.Module): def __init__( self, size_pdf: torch.FloatTensor, std_lattice: float = 0.2, scale_lattice: float = 5.0, features: int = 128, ): super().__init__() self.features = features self.scale_lattice = scale_lattice self.std_lattice = std_lattice size_cdf = torch.cumsum(size_pdf, dim=0) assert size_cdf[-1] >= 1.0 self.size_cdf = nn.Parameter(size_cdf, requires_grad=False) @property def device(self): return self.size_cdf.data.device def forward(self, batch_size: int): size = torch.bucketize( torch.rand(batch_size, device=self.device), self.size_cdf ) num_atoms = size.sum() cells = self.scale_lattice * torch.matrix_exp( self.std_lattice * torch.randn(batch_size, 3, 3, device=self.device) ) x = torch.rand(num_atoms, 3, device=self.device) z = torch.rand(num_atoms, self.features, device=self.device) return cells, x, z, size batch_size = 256 pdf = torch.tensor([0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) rand = RandomCrystal(pdf).to("cuda") cell, x, z, size = rand(batch_size=batch_size) batch = torch.arange(batch_size, device=size.device) batch = batch.repeat_interleave(size) geometry = Geometry(cell, size, x, knn=128, triplets=False) distance = [] lengths = [] angles = [] for i in range(batch_size): mask = batch == i size_i = size[i].item() cell_i = cell[i].clone().detach().cpu().numpy() x_i = x[mask].clone().detach().cpu().numpy() cry = crystal("C" * size_i, [tuple(x) for x in x_i], cell=cell_i) [a, b, c, alpha, beta, gamma] = cry.get_cell_lengths_and_angles() lengths.append([a, b, c]) angles.append([alpha, beta, gamma]) dist = neighbor_list("d", cry, cutoff=5.0) distance.append(dist) distance = np.concatenate(distance) lengths = np.array(lengths) angles = np.array(angles) print( "lengths mean absolut error", np.max(np.abs(geometry.lengths.cpu().numpy() - lengths)), ) print( "angles mean absolut error", np.max(np.abs(geometry.angles.cpu().numpy() - (angles * np.pi / 180))), ) mask = geometry.edges_r_ij <= 5.0 geom_dist = geometry.edges_r_ij[mask].detach().cpu().numpy() import matplotlib.pyplot as plt hist1, bins = np.histogram(distance, bins=32, range=(0.0, 5.0)) hist2, _ = np.histogram(geom_dist, bins=32, range=(0.0, 5.0)) bins = 0.5 * (bins[1:] + bins[:-1]) plt.plot(bins, hist1) plt.plot(bins, hist2) plt.savefig("out.png")
11,649
30.233244
79
py
pegnn
pegnn-master/src/utils/io.py
from ctypes import Structure import torch import torch.nn.functional as F from ase.spacegroup import crystal import ase.io as io import pandas as pd from src.utils.visualize import select import os def write_cif(file_name, idx, cell, pos, z, num_atoms): cell, pos, z = select(idx, cell, pos, z, num_atoms) c = crystal(z, basis=pos, cell=cell) c.write(file_name, format="cif") def get_atoms(idx, cell, pos, z, num_atoms): cell, pos, z = select(idx, cell, pos, z, num_atoms) return crystal(z, basis=pos, cell=cell) class AggregateBatch: def __init__(self): self.reset() def reset(self): self.cell = [] self.cell_noisy = [] self.cell_denoised = [] self.pos = [] self.z = [] self.num_atoms = [] def append(self, cell, cell_denoised, pos, z, num_atoms): self.cell.append(cell.clone().detach().cpu()) self.cell_denoised.append(cell_denoised.clone().detach().cpu()) self.pos.append(pos.clone().detach().cpu()) self.z.append(z.clone().detach().cpu()) self.num_atoms.append(num_atoms.clone().detach().cpu()) def cat(self): z = torch.cat(self.z, dim=0) if z.ndim == 1: z = F.one_hot(z, num_classes=100) return ( torch.cat(self.cell, dim=0), torch.cat(self.cell_denoised, dim=0), torch.cat(self.pos, dim=0), z, torch.cat(self.num_atoms, dim=0) ) def write(self, path, verbose=False): cell, cell_denoised, pos, z, num_atoms = self.cat() os.makedirs(path, exist_ok=True) iterator = range(cell.shape[0]) if verbose: import tqdm iterator = tqdm.tqdm(iterator, desc=f"saving cif to {path}") struct_original = [] struct_denoised = [] for idx in iterator: struct_original.append(get_atoms(idx, cell, pos, z, num_atoms)) struct_denoised.append( get_atoms(idx, cell_denoised, pos, z, num_atoms)) io.write(os.path.join(path, "original.cif"), struct_original) io.write(os.path.join(path, "generated.cif"), struct_denoised)
2,197
27.179487
75
py
pegnn
pegnn-master/src/utils/elements.py
elements = { "H": 1, "He": 2, "Li": 3, "Be": 4, "B": 5, "C": 6, "N": 7, "O": 8, "F": 9, "Ne": 10, "Na": 11, "Mg": 12, "Al": 13, "Si": 14, "P": 15, "S": 16, "Cl": 17, "Ar": 18, "K": 19, "Ca": 20, "Sc": 21, "Ti": 22, "V": 23, "Cr": 24, "Mn": 25, "Fe": 26, "Co": 27, "Ni": 28, "Cu": 29, "Zn": 30, "Ga": 31, "Ge": 32, "As": 33, "Se": 34, "Br": 35, "Kr": 36, "Rb": 37, "Sr": 38, "Y": 39, "Zr": 40, "Nb": 41, "Mo": 42, "Tc": 43, "Ru": 44, "Rh": 45, "Pd": 46, "Ag": 47, "Cd": 48, "In": 49, "Sn": 50, "Sb": 51, "Te": 52, "I": 53, "Xe": 54, "Cs": 55, "Ba": 56, "La": 57, "Ce": 58, "Pr": 59, "Nd": 60, "Pm": 61, "Sm": 62, "Eu": 63, "Gd": 64, "Tb": 65, "Dy": 66, "Ho": 67, "Er": 68, "Tm": 69, "Yb": 70, "Lu": 71, "Hf": 72, "Ta": 73, "W": 74, "Re": 75, "Os": 76, "Ir": 77, "Pt": 78, "Au": 79, "Hg": 80, "Tl": 81, "Pb": 82, "Bi": 83, "Po": 84, "At": 85, "Rn": 86, "Fr": 87, "Ra": 88, "Ac": 89, "Th": 90, "Pa": 91, "U": 92, "Np": 93, "Pu": 94, "Am": 95, "Cm": 96, "Bk": 97, "Cf": 98, "Es": 99, "Fm": 100, "Md": 101, "No": 102, "Lr": 103, "Rf": 104, "Db": 105, "Sg": 106, "Bh": 107, "Hs": 108, "Mt": 109, "Ds": 110, "Rg": 111, "Cn": 112, "Nh": 113, "Fl": 114, "Mc": 115, "Lv": 116, "Ts": 117, "Og": 118, }
1,663
12.752066
14
py
pegnn
pegnn-master/src/utils/debug.py
def check_grad(model, verbose=True, debug=False): must_break = False for k, p in model.named_parameters(): if (p.grad is not None) and (p.grad != p.grad).any(): must_break = True break if must_break: if verbose: print("grad") for k, p in model.named_parameters(): if p.grad is not None: print(k, p.grad.mean(), p.grad.std(), p.grad.min(), p.grad.max()) if debug: breakpoint() else: exit(0)
544
27.684211
85
py
pegnn
pegnn-master/src/utils/visualize.py
import torch from ase.spacegroup import crystal from ase.visualize.plot import plot_atoms import matplotlib.pyplot as plt from src.utils.elements import elements from src.models.operator.utils import lattice_params_to_matrix_torch def select(idx, cell, pos, z, num_atoms): struct_idx = torch.arange(num_atoms.shape[0], device=num_atoms.device) batch = struct_idx.repeat_interleave(num_atoms) mask = idx == batch return ( cell[idx].clone().detach().cpu().numpy(), pos[mask].clone().detach().cpu().numpy(), z[mask].argmax(dim=1).clone().detach().cpu().numpy(), ) def plot(ax, idx, cell, pos, z, num_atoms, radii=0.3, rotation=("90x,45y,0z")): cell, pos, z = select(idx, cell, pos, z, num_atoms) c = crystal(z, basis=pos, cell=cell) plot_atoms(c, ax, radii=radii, rotation=rotation) def plot_grid( cell, pos, z, num_atoms, rows=2, cols=3, radii=0.3, rotation=("30x,30y,30z") ): fig, axs = plt.subplots(rows, cols) for i in range(rows): for j in range(cols): plot( axs[i][j], j + i * cols, cell, pos, z, num_atoms, radii=radii, rotation=rotation, ) return fig def generate_fig(original, denoised, n): import matplotlib.pyplot as plt from ase.visualize.plot import plot_atoms from ase.spacegroup import crystal L_o, x_o, z_o, atoms_count_o = original batch_o = torch.arange(L_o.shape[0], device=L_o.device) batch_atoms_o = batch_o.repeat_interleave(atoms_count_o) L_t, x_t, z_t, atoms_count_t = denoised batch_t = torch.arange(atoms_count_t.shape[0], device=atoms_count_t.device) batch_atoms_t = batch_t.repeat_interleave(atoms_count_t) elems = ["" for _ in range(128)] for s, e in elements.items(): elems[e] = s fig, axarr = plt.subplots(n, 2, figsize=(15, n * 5)) for i in range(n): mask_o = batch_atoms_o == i mask_t = batch_atoms_t == i for k, (L, x, z, title) in enumerate( zip( [L_o[i], L_t[i]], [x_o[mask_o], x_t[mask_t]], [z_o[mask_o], z_t[mask_t]], ["original", "denoised"], ) ): cell_i = L.clone().detach().cpu().numpy() x_i = x.clone().detach().cpu().numpy() z_i = z.clone().detach().cpu().numpy() sym_i = [elems[max(e, 1)] for e in z_i] cry = crystal(sym_i, [tuple(x) for x in x_i], cell=cell_i) axarr[i][k].set_title(title) axarr[i][k].set_axis_off() try: plot_atoms(cry, axarr[i][k], rotation=("45x,45y,0z")) except: pass # fig.savefig(os.path.join(output_directory, f"gen_{n_iter}.png")) return fig def get_fig(batch, model, n, lattice_scaler=None): # get data from the batch L_real = batch.cell x_real = batch.pos z_real = batch.z struct_size = batch.num_atoms # denoise L_denoised = model(L_real, x_real, z_real, struct_size) if isinstance(L_denoised, tuple): lengths_scaled, angles_scaled = lattice_scaler.denormalise( L_denoised[0], L_denoised[1]) L_denoised = lattice_params_to_matrix_torch( lengths_scaled, angles_scaled) original = (L_real, x_real, z_real, struct_size) denoised = (L_denoised, x_real, z_real, struct_size) return generate_fig(original, denoised, n)
3,558
28.172131
80
py
T2TL
T2TL-main/src/T2TL.py
import argparse import time import datetime import torch import torch_ac import tensorboardX import sys import glob from math import floor import utils from model import ACModel from context_model import ContextACModel if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser() ## General parameters parser.add_argument("--algo", default='ppo', help="algorithm to use: a2c | ppo (REQUIRED)") parser.add_argument("--env", default='Zones-25-v1', help="name of the environment to train on (REQUIRED)") parser.add_argument("--ltl-sampler", default="Until_1_2_1_1", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--model", default=None, help="name of the model (default: {ENV}_{SAMPLER}_{ALGO}_{TIME})") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--log-interval", type=int, default=1, help="number of updates between two logs (default: 10)") parser.add_argument("--save-interval", type=int, default=2, help="number of updates between two saves (default: 10, 0 means no saving)") parser.add_argument("--procs", type=int, default=16, help="number of processes (default: 16)") parser.add_argument("--frames", type=int, default=1*10**7, help="number of frames of training (default: 2*10e8)") parser.add_argument("--checkpoint-dir", default=None) ## Evaluation parameters parser.add_argument("--eval", action="store_true", default=False, help="evaluate the saved model (default: False)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--eval-env", default=None, help="name of the environment to train on (default: use the same \"env\" as training)") parser.add_argument("--ltl-samplers-eval", default=None, nargs='+', help="the ltl formula templates to sample from for evaluation (default: use the same \"ltl-sampler\" as training)") parser.add_argument("--eval-procs", type=int, default=1, help="number of processes (default: use the same \"procs\" as training)") ## Parameters for main algorithm parser.add_argument("--epochs", type=int, default=10, help="number of epochs for PPO (default: 4)") parser.add_argument("--batch-size", type=int, default=1024, help="batch size for PPO (default: 256)") parser.add_argument("--frames-per-proc", type=int, default=4096, help="number of frames per process before update (default: 5 for A2C and 128 for PPO)") parser.add_argument("--discount", type=float, default=0.998, help="discount factor (default: 0.99)") parser.add_argument("--lr", type=float, default=0.0003, help="learning rate (default: 0.0003)") parser.add_argument("--gae-lambda", type=float, default=0.95, help="lambda coefficient in GAE formula (default: 0.95, 1 means no gae)") parser.add_argument("--entropy-coef", type=float, default=0.003, help="entropy term coefficient (default: 0.01)") parser.add_argument("--value-loss-coef", type=float, default=0.5, help="value loss term coefficient (default: 0.5)") parser.add_argument("--max-grad-norm", type=float, default=0.5, help="maximum norm of gradient (default: 0.5)") parser.add_argument("--optim-eps", type=float, default=1e-8, help="Adam and RMSprop optimizer epsilon (default: 1e-8)") parser.add_argument("--optim-alpha", type=float, default=0.99, help="RMSprop optimizer alpha (default: 0.99)") parser.add_argument("--clip-eps", type=float, default=0.2, help="clipping epsilon for PPO (default: 0.2)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--noLTL", action="store_true", default=False, help="the environment no longer has an LTL goal. --ignoreLTL must be specified concurrently.") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="Transformer", help="use gnn to model the LTL (only if ignoreLTL==True)") parser.add_argument("--trans_layer", type=int, default=1, help="the number of Transformer layers need to use") parser.add_argument("--int-reward", type=float, default=0.0, help="the intrinsic reward for LTL progression (default: 0.0)") parser.add_argument("--pretrained-gnn", action="store_true", default=False, help="load a pre-trained LTL module.") parser.add_argument("--dumb-ac", action="store_true", default=False,help="Use a single-layer actor-critic") parser.add_argument("--freeze-ltl", action="store_true", default=False,help="Freeze the gradient updates of the LTL module") # Transformer special parameters parser.add_argument("--d_model", type=int, default=64, help="") parser.add_argument("--nhead", type=int, default=8, help="") parser.add_argument("--num_encoder_layers", type=int, default=4, help="") parser.add_argument("--pool", type=str, default='mean', help="") parser.add_argument("--dim_feedforward", type=int, default=256, help="") parser.add_argument("--dropout", type=float, default=0.0, help="") parser.add_argument("--d_out", type=int, default=16, help="") parser.add_argument("--layer_norm_eps", type=float, default=1e-5, help="") parser.add_argument("--TFixup", type=bool, default=True, help="") # Context Special variable parser.add_argument("--ContType", type=str, default='Transformer', help="To choose which model to encode CONTEXT VARIABLE (e.g., RNN, Transformer)") parser.add_argument("--use_cont", type=bool, default=True, help="") parser.add_argument("--hist_length", type=int, default=8, help="") parser.add_argument("--cont_dim", type=int, default=16, help="") parser.add_argument("--cont_d_model", type=int, default=64, help="") parser.add_argument("--cont_nhead", type=int, default=8, help="") parser.add_argument("--cont_num_encoder_layers", type=int, default=2, help="") parser.add_argument("--cont_pool", type=str, default='mean', help="") parser.add_argument("--cont_dim_feedforward", type=int, default=256, help="") parser.add_argument("--cont_d_out", type=int, default=16, help="") # device parser.add_argument("--cuda", type=str, default='cuda:0', help="") parser.add_argument("--device", type=str, default='cuda:0', help="") # additional desciption for test parser.add_argument("--sth", type=str, default='None', help="") args = parser.parse_args() use_mem = args.recurrence > 1 # whether use memory or not # Set run dir date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") # '21-08-21-22-36-39' gnn_name = args.gnn if args.ignoreLTL: gnn_name = "IgnoreLTL" if args.dumb_ac: gnn_name = gnn_name + "-dumb_ac" if args.pretrained_gnn: gnn_name = gnn_name + "-pretrained" if args.freeze_ltl: gnn_name = gnn_name + "-freeze_ltl" if use_mem: gnn_name = gnn_name + "-recurrence:%d"%(args.recurrence) if args.gnn == 'Transformer': default_model_name = f"{gnn_name}+TL2_{args.env}_seed:{args.seed}_bs:{args.batch_size}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_FFD:{args.dim_feedforward}_hist:{args.hist_length}_L_Cont:{args.cont_num_encoder_layers}_Init:{args.TFixup}_sth:{args.sth}" else: # 'RGCN_8x32_ROOT_SHARED_Until_1_2_1_1_Zones-5-v0_seed:1_epochs:10_bs:2048_fpp:4096_dsc:0.998_lr:0.0003_ent:0.003_clip:0.2_prog:full' default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_epochs:{args.epochs}_bs:{args.batch_size}_fpp:{args.frames_per_proc}_dsc:{args.discount}_lr:{args.lr}_ent:{args.entropy_coef}_clip:{args.clip_eps}_prog:{args.progression_mode}" model_name = args.model or default_model_name storage_dir = "storage" if args.checkpoint_dir is None else args.checkpoint_dir model_dir = utils.get_model_dir(model_name, storage_dir) pretrained_model_dir = None if args.pretrained_gnn: assert(args.progression_mode == "full") # default_dir = f"symbol-storage/{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" default_dir = f"{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" print(default_dir) model_dirs = glob.glob(default_dir) if len(model_dirs) == 0: raise Exception("Pretraining directory not found.") elif len(model_dirs) > 1: raise Exception("More than 1 candidate pretraining directory found.") pretrained_model_dir = model_dirs[0] # Load loggers and Tensorboard writer txt_logger = utils.get_txt_logger(model_dir + "/train") csv_file, csv_logger = utils.get_csv_logger(model_dir + "/train") tb_writer = tensorboardX.SummaryWriter(model_dir + "/train") utils.save_config(model_dir + "/train", args) # Log command and all script arguments txt_logger.info("{}\n".format(" ".join(sys.argv))) txt_logger.info("{}\n".format(args)) # It will output the context of Namespace # Set seed for all randomness sources utils.seed(args.seed) # Set device device = torch.device(args.cuda if torch.cuda.is_available() else "cpu") # device = torch.device('cpu') txt_logger.info(f"Device: {device}\n") # Output the device (default is cpu) # Load environments envs = [] progression_mode = args.progression_mode for i in range(args.procs): # load the env & progression_mode & LTL formula # turn to utils/env.py envs.append(utils.make_env(args.env, progression_mode, args.ltl_sampler, args.seed, args.int_reward, args.noLTL)) # Sync environments envs[0].reset() # Add the agent to map & translate the LTL formula txt_logger.info("Environments loaded\n") # Load training status try: status = utils.get_status(model_dir + "/train", args) except OSError: status = {"num_frames": 0, "update": 0} # ??? the state of algorithm ? txt_logger.info("Training status loaded.\n") if pretrained_model_dir is not None: try: pretrained_status = utils.get_status(pretrained_model_dir, args) except: txt_logger.info("Failed to load pretrained model.\n") exit(1) # Load observations preprocessor-- build AST using_gnn = (args.gnn != "GRU" and args.gnn != "LSTM" and args.gnn != "Transformer") # turn to env/format.py obs_space, preprocess_obss = utils.get_obss_preprocessor(envs[0], using_gnn, progression_mode) if "vocab" in status and preprocess_obss.vocab is not None: preprocess_obss.vocab.load_vocab(status["vocab"]) txt_logger.info("Observations preprocessor loaded.\n") # Load model if use_mem: acmodel = RecurrentACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl) else: acmodel = ContextACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl, args) if "model_state" in status: acmodel.load_state_dict(status["model_state"]) txt_logger.info("Loading model from existing run.\n") elif args.pretrained_gnn: acmodel.load_pretrained_gnn(pretrained_status["model_state"]) txt_logger.info("Pretrained model loaded.\n") acmodel.to(device) txt_logger.info("Model loaded.\n") txt_logger.info("{}\n".format(acmodel)) # Load algo if args.algo == "a2c": algo = torch_ac.A2CAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_alpha, args.optim_eps, preprocess_obss) elif args.algo == "ppo": algo = torch_ac.PPOAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_eps, args.clip_eps, args.epochs, args.batch_size, preprocess_obss, history_length=args.hist_length) else: raise ValueError("Incorrect algorithm name: {}".format(args.algo)) if "optimizer_state" in status: algo.optimizer.load_state_dict(status["optimizer_state"]) txt_logger.info("Loading optimizer from existing run.\n") txt_logger.info("Optimizer loaded.\n") # init the evaluator if args.eval: eval_samplers = args.ltl_samplers_eval if args.ltl_samplers_eval else [args.ltl_sampler] eval_env = args.eval_env if args.eval_env else args.env eval_procs = args.eval_procs if args.eval_procs else args.procs evals = [] for eval_sampler in eval_samplers: evals.append(utils.Eval(eval_env, model_name, eval_sampler, seed=args.seed, device=device, num_procs=eval_procs, ignoreLTL=args.ignoreLTL, progression_mode=progression_mode, gnn=args.gnn, dumb_ac = args.dumb_ac)) # Train model num_frames = status["num_frames"] # num_frames:0 update = status["update"] # update:0 start_time = time.time() while num_frames < args.frames: # Update model parameters update_start_time = time.time() exps, logs1 = algo.collect_experiences() # interacte with environmets (very important) logs2 = algo.update_parameters(exps) logs = {**logs1, **logs2} update_end_time = time.time() num_frames += logs["num_frames"] update += 1 # Print logs if update % args.log_interval == 0: fps = logs["num_frames"]/(update_end_time - update_start_time) duration = int(time.time() - start_time) return_per_episode = utils.synthesize(logs["return_per_episode"]) rreturn_per_episode = utils.synthesize(logs["reshaped_return_per_episode"]) average_reward_per_step = utils.average_reward_per_step(logs["return_per_episode"], logs["num_frames_per_episode"]) average_discounted_return = utils.average_discounted_return(logs["return_per_episode"], logs["num_frames_per_episode"], args.discount) num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"]) header = ["update", "frames", "FPS", "duration"] data = [update, num_frames, fps, duration] header += ["rreturn_" + key for key in rreturn_per_episode.keys()] data += rreturn_per_episode.values() header += ["average_reward_per_step", "average_discounted_return"] data += [average_reward_per_step, average_discounted_return] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["entropy", "value", "policy_loss", "value_loss", "grad_norm"] data += [logs["entropy"], logs["value"], logs["policy_loss"], logs["value_loss"], logs["grad_norm"]] txt_logger.info( "U {} | F {:06} | FPS {:04.0f} | D {} | rR:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | ARPS: {:.3f} | ADR: {:.3f} | F:μσmM {:.1f} {:.1f} {} {} | H {:.3f} | V {:.3f} | pL {:.3f} | vL {:.3f} | ∇ {:.3f}" .format(*data)) header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() if status["num_frames"] == 0: csv_logger.writerow(header) csv_logger.writerow(data) csv_file.flush() for field, value in zip(header, data): tb_writer.add_scalar(field, value, num_frames) # Save status if args.save_interval > 0 and update % args.save_interval == 0: status = {"num_frames": num_frames, "update": update, "model_state": algo.acmodel.state_dict(), "optimizer_state": algo.optimizer.state_dict()} if hasattr(preprocess_obss, "vocab") and preprocess_obss.vocab is not None: status["vocab"] = preprocess_obss.vocab.vocab utils.save_status(status, model_dir + "/train") txt_logger.info("Status saved") if args.eval: # we send the num_frames to align the eval curves with the training curves on TB for evalu in evals: evalu.eval(num_frames, episodes=args.eval_episodes)
17,759
50.32948
296
py
T2TL
T2TL-main/src/ltl_progression.py
""" This code allows to progress LTL formulas. It requires installing the SPOT library: - https://spot.lrde.epita.fr/install.html To encode LTL formulas, we use tuples, e.g., ( 'and', ('until','True', ('and', 'd', ('until','True','c'))), ('until','True', ('and', 'a', ('until','True', ('and', 'b', ('until','True','c'))))) ) Some notes about the format: - It supports the following temporal operators: "next", "until", "always", and "eventually". - It supports the following logical operators: "not", "or", "and". - Propositions are assume to be one char. - Negations are always followed by a proposition. - true and false are encoded as "True" and "False" """ from sympy import * from sympy.logic import simplify_logic from sympy.logic.boolalg import And, Or, Not import time, collections, spot """ This module contains functions to progress co-safe LTL formulas such as: ( 'and', ('until','True', ('and', 'd', ('until','True','c'))), ('until','True', ('and', 'a', ('until','True', ('and', 'b', ('until','True','c'))))) ) """ def _is_prop_formula(f): # returns True if the formula does not contains temporal operators return 'next' not in str(f) and 'until' not in str(f) def _subsume_until(f1, f2): if str(f1) not in str(f2): return False while type(f2) != str: if f1 == f2: return True if f2[0] == 'until': f2 = f2[2] elif f2[0] == 'and': if _is_prop_formula(f2[1]) and not _is_prop_formula(f2[2]): f2 = f2[2] elif not _is_prop_formula(f2[1]) and _is_prop_formula(f2[2]): f2 = f2[1] else: return False else: return False return False def _subsume_or(f1, f2): if str(f1) not in str(f2): return False while type(f2) != str: if f1 == f2: return True if f2[0] == 'until': f2 = f2[2] elif f2[0] == 'and': if _is_prop_formula(f2[1]) and not _is_prop_formula(f2[2]): f2 = f2[2] elif not _is_prop_formula(f2[1]) and _is_prop_formula(f2[2]): f2 = f2[1] else: return False else: return False return False def progress_and_clean(ltl_formula, truth_assignment): # print('---------------------------------------------------------') # print('Current ltl_formula is {}'.format(ltl_formula)) # print('Current truth_assignment is {}'.format(truth_assignment)) ltl = progress(ltl_formula, truth_assignment) # I am using spot to simplify the resulting ltl formula ltl_spot = _get_spot_format(ltl) f = spot.formula(ltl_spot) f = spot.simplify(f) ltl_spot = f.__format__("l") ltl_std, r = _get_std_format(ltl_spot.split(' ')) # print('Current ltl_std is {}'.format(ltl_std)) # print('---------------------------------------------------------') assert len(r) == 0, "Format error" + str(ltl_std) + " " + str(r) return ltl_std def spotify(ltl_formula): ltl_spot = _get_spot_format(ltl_formula) f = spot.formula(ltl_spot) f = spot.simplify(f) ltl_spot = f.__format__("l") # return ltl_spot return f#.to_str('latex') def _get_spot_format(ltl_std): ltl_spot = str(ltl_std).replace("(","").replace(")","").replace(",","") ltl_spot = ltl_spot.replace("'until'","U").replace("'not'","!").replace("'or'","|").replace("'and'","&") ltl_spot = ltl_spot.replace("'next'","X").replace("'eventually'","F").replace("'always'","G").replace("'True'","t").replace("'False'","f").replace("\'","\"") return ltl_spot def _get_std_format(ltl_spot): s = ltl_spot[0] r = ltl_spot[1:] if s in ["X","U","&","|"]: v1,r1 = _get_std_format(r) v2,r2 = _get_std_format(r1) if s == "X": op = 'next' if s == "U": op = 'until' if s == "&": op = 'and' if s == "|": op = 'or' return (op,v1,v2),r2 if s in ["F","G","!"]: v1,r1 = _get_std_format(r) if s == "F": op = 'eventually' if s == "G": op = 'always' if s == "!": op = 'not' return (op,v1),r1 if s == "f": return 'False', r if s == "t": return 'True', r if s[0] == '"': return s.replace('"',''), r assert False, "Format error in spot2std" def progress(ltl_formula, truth_assignment): if type(ltl_formula) == str: # True, False, or proposition if len(ltl_formula) == 1: # ltl_formula is a proposition if ltl_formula in truth_assignment: return 'True' else: return 'False' return ltl_formula if ltl_formula[0] == 'not': # negations should be over propositions only according to the cosafe ltl syntactic restriction result = progress(ltl_formula[1], truth_assignment) if result == 'True': return 'False' elif result == 'False': return 'True' else: raise NotImplementedError("The following formula doesn't follow the cosafe syntactic restriction: " + str(ltl_formula)) if ltl_formula[0] == 'and': res1 = progress(ltl_formula[1], truth_assignment) res2 = progress(ltl_formula[2], truth_assignment) if res1 == 'True' and res2 == 'True': return 'True' if res1 == 'False' or res2 == 'False': return 'False' if res1 == 'True': return res2 if res2 == 'True': return res1 if res1 == res2: return res1 #if _subsume_until(res1, res2): return res2 #if _subsume_until(res2, res1): return res1 return ('and',res1,res2) if ltl_formula[0] == 'or': res1 = progress(ltl_formula[1], truth_assignment) res2 = progress(ltl_formula[2], truth_assignment) if res1 == 'True' or res2 == 'True' : return 'True' if res1 == 'False' and res2 == 'False': return 'False' if res1 == 'False': return res2 if res2 == 'False': return res1 if res1 == res2: return res1 #if _subsume_until(res1, res2): return res1 #if _subsume_until(res2, res1): return res2 return ('or',res1,res2) if ltl_formula[0] == 'next': return progress(ltl_formula[1], truth_assignment) # NOTE: What about release and other temporal operators? if ltl_formula[0] == 'eventually': res = progress(ltl_formula[1], truth_assignment) return ("or", ltl_formula, res) if ltl_formula[0] == 'always': res = progress(ltl_formula[1], truth_assignment) return ("and", ltl_formula, res) if ltl_formula[0] == 'until': res1 = progress(ltl_formula[1], truth_assignment) res2 = progress(ltl_formula[2], truth_assignment) if res1 == 'False': f1 = 'False' elif res1 == 'True': f1 = ('until', ltl_formula[1], ltl_formula[2]) else: f1 = ('and', res1, ('until', ltl_formula[1], ltl_formula[2])) if res2 == 'True': return 'True' if res2 == 'False': return f1 # Returning ('or', res2, f1) #if _subsume_until(f1, res2): return f1 #if _subsume_until(res2, f1): return res2 return ('or', res2, f1) if __name__ == '__main__': #ltl = ('and',('eventually','a'),('and',('eventually','b'),('eventually','c'))) #ltl = ('and',('eventually','a'),('eventually',('and','b',('eventually','c')))) #ltl = ('until',('not','a'),('and', 'b', ('eventually','d'))) ltl = ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) while True: print(ltl) props = input() ltl = progress_and_clean(ltl, props)
7,821
33.008696
161
py
T2TL
T2TL-main/src/T1TL_pretrain.py
import argparse import time import datetime import torch import torch_ac import tensorboardX import sys import glob from math import floor import utils from model import ACModel from recurrent_model import RecurrentACModel if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser() ## General parameters parser.add_argument("--algo", default='ppo', help="algorithm to use: a2c | ppo (REQUIRED)") parser.add_argument("--env", default='Zones-25-v1', help="name of the environment to train on (REQUIRED)") parser.add_argument("--ltl-sampler", default="Until_1_2_1_1", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--model", default=None, help="name of the model (default: {ENV}_{SAMPLER}_{ALGO}_{TIME})") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--log-interval", type=int, default=1, help="number of updates between two logs (default: 10)") parser.add_argument("--save-interval", type=int, default=2, help="number of updates between two saves (default: 10, 0 means no saving)") parser.add_argument("--procs", type=int, default=16, help="number of processes (default: 16)") parser.add_argument("--frames", type=int, default=1*10**7, help="number of frames of training (default: 2*10e8)") parser.add_argument("--checkpoint-dir", default=None) ## Evaluation parameters parser.add_argument("--eval", action="store_true", default=False, help="evaluate the saved model (default: False)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--eval-env", default=None, help="name of the environment to train on (default: use the same \"env\" as training)") parser.add_argument("--ltl-samplers-eval", default=None, nargs='+', help="the ltl formula templates to sample from for evaluation (default: use the same \"ltl-sampler\" as training)") parser.add_argument("--eval-procs", type=int, default=1, help="number of processes (default: use the same \"procs\" as training)") ## Parameters for main algorithm parser.add_argument("--epochs", type=int, default=10, help="number of epochs for PPO (default: 4)") parser.add_argument("--batch-size", type=int, default=1024, help="batch size for PPO (default: 256)") parser.add_argument("--frames-per-proc", type=int, default=4096, help="number of frames per process before update (default: 5 for A2C and 128 for PPO)") parser.add_argument("--discount", type=float, default=0.998, help="discount factor (default: 0.99)") parser.add_argument("--lr", type=float, default=0.0003, help="learning rate (default: 0.0003)") parser.add_argument("--gae-lambda", type=float, default=0.95, help="lambda coefficient in GAE formula (default: 0.95, 1 means no gae)") parser.add_argument("--entropy-coef", type=float, default=0.003, help="entropy term coefficient (default: 0.01)") parser.add_argument("--value-loss-coef", type=float, default=0.5, help="value loss term coefficient (default: 0.5)") parser.add_argument("--max-grad-norm", type=float, default=0.5, help="maximum norm of gradient (default: 0.5)") parser.add_argument("--optim-eps", type=float, default=1e-8, help="Adam and RMSprop optimizer epsilon (default: 1e-8)") parser.add_argument("--optim-alpha", type=float, default=0.99, help="RMSprop optimizer alpha (default: 0.99)") parser.add_argument("--clip-eps", type=float, default=0.2, help="clipping epsilon for PPO (default: 0.2)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--noLTL", action="store_true", default=False, help="the environment no longer has an LTL goal. --ignoreLTL must be specified concurrently.") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="Transformer", help="use gnn to model the LTL (only if ignoreLTL==True)") parser.add_argument("--trans_layer", type=int, default=1, help="the number of Transformer layers need to use") parser.add_argument("--int-reward", type=float, default=0.0, help="the intrinsic reward for LTL progression (default: 0.0)") parser.add_argument("--pretrained-gnn", action="store_true", default=True, help="load a pre-trained LTL module.") parser.add_argument("--dumb-ac", action="store_true", default=False, help="Use a single-layer actor-critic") parser.add_argument("--freeze-ltl", action="store_true", default=False, help="Freeze the gradient updates of the LTL module") # Transformer special parameters parser.add_argument("--d_model", type=int, default=64, help="") parser.add_argument("--nhead", type=int, default=8, help="") parser.add_argument("--num_encoder_layers", type=int, default=4, help="") parser.add_argument("--pool", type=str, default='mean', help="") parser.add_argument("--dim_feedforward", type=int, default=256, help="") parser.add_argument("--dropout", type=float, default=0.0, help="") parser.add_argument("--d_out", type=int, default=16, help="") parser.add_argument("--layer_norm_eps", type=float, default=1e-5, help="") parser.add_argument("--TFixup", type=bool, default=True, help="") parser.add_argument("--cuda", type=str, default='cuda:0', help="") # additional desciption for test parser.add_argument("--sth", type=str, default='None', help="") args = parser.parse_args() use_mem = args.recurrence > 1 # whether use memory or not # Set run dir date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") # '21-08-21-22-36-39' gnn_name = args.gnn if args.ignoreLTL: gnn_name = "IgnoreLTL" if args.dumb_ac: gnn_name = gnn_name + "-dumb_ac" if args.pretrained_gnn: gnn_name = gnn_name + "-pretrained" if args.freeze_ltl: gnn_name = gnn_name + "-freeze_ltl" if use_mem: gnn_name = gnn_name + "-recurrence:%d"%(args.recurrence) if args.gnn == 'Transformer': default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_FFD:{args.dim_feedforward}_d_out:{args.d_out}_sth:{args.sth}" else: # 'RGCN_8x32_ROOT_SHARED_Until_1_2_1_1_Zones-5-v0_seed:1_epochs:10_bs:2048_fpp:4096_dsc:0.998_lr:0.0003_ent:0.003_clip:0.2_prog:full' default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_epochs:{args.epochs}_bs:{args.batch_size}_fpp:{args.frames_per_proc}_dsc:{args.discount}_lr:{args.lr}_ent:{args.entropy_coef}_clip:{args.clip_eps}_prog:{args.progression_mode}" model_name = args.model or default_model_name storage_dir = "storage" if args.checkpoint_dir is None else args.checkpoint_dir model_dir = utils.get_model_dir(model_name, storage_dir) pretrained_model_dir = None if args.pretrained_gnn: assert(args.progression_mode == "full") # default_dir = f"symbol-storage/{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" # default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_*_d_out:{args.d_out}/train" default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_dim_feedforward:{args.dim_feedforward}_d_out:{args.d_out}_None/train" print(default_dir) model_dirs = glob.glob(default_dir) if len(model_dirs) == 0: raise Exception("Pretraining directory not found.") elif len(model_dirs) > 1: raise Exception("More than 1 candidate pretraining directory found.") pretrained_model_dir = model_dirs[0] # Load loggers and Tensorboard writer txt_logger = utils.get_txt_logger(model_dir + "/train") csv_file, csv_logger = utils.get_csv_logger(model_dir + "/train") tb_writer = tensorboardX.SummaryWriter(model_dir + "/train") utils.save_config(model_dir + "/train", args) # Log command and all script arguments txt_logger.info("{}\n".format(" ".join(sys.argv))) txt_logger.info("{}\n".format(args)) # It will output the context of Namespace # Set seed for all randomness sources utils.seed(args.seed) # Set device device = torch.device(args.cuda if torch.cuda.is_available() else "cpu") # device = torch.device('cpu') txt_logger.info(f"Device: {device}\n") # Output the device (default is cpu) # Load environments envs = [] progression_mode = args.progression_mode for i in range(args.procs): # load the env & progression_mode & LTL formula # turn to utils/env.py envs.append(utils.make_env(args.env, progression_mode, args.ltl_sampler, args.seed, args.int_reward, args.noLTL)) # Sync environments envs[0].reset() # Add the agent to map & translate the LTL formula txt_logger.info("Environments loaded\n") # Load training status try: status = utils.get_status(model_dir + "/train", args) except OSError: status = {"num_frames": 0, "update": 0} # ??? the state of algorithm ? txt_logger.info("Training status loaded.\n") if pretrained_model_dir is not None: try: pretrained_status = utils.get_status(pretrained_model_dir, args) except: txt_logger.info("Failed to load pretrained model.\n") exit(1) # Load observations preprocessor-- build AST using_gnn = (args.gnn != "GRU" and args.gnn != "LSTM" and args.gnn != "Transformer") # turn to env/format.py obs_space, preprocess_obss = utils.get_obss_preprocessor(envs[0], using_gnn, progression_mode) if "vocab" in status and preprocess_obss.vocab is not None: preprocess_obss.vocab.load_vocab(status["vocab"]) txt_logger.info("Observations preprocessor loaded.\n") # Load model if use_mem: acmodel = RecurrentACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl) else: acmodel = ACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl, args) if "model_state" in status: acmodel.load_state_dict(status["model_state"]) txt_logger.info("Loading model from existing run.\n") elif args.pretrained_gnn: acmodel.load_pretrained_gnn(pretrained_status["model_state"]) txt_logger.info("Pretrained model loaded.\n") acmodel.to(device) txt_logger.info("Model loaded.\n") txt_logger.info("{}\n".format(acmodel)) # Load algo if args.algo == "a2c": algo = torch_ac.A2CAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_alpha, args.optim_eps, preprocess_obss) elif args.algo == "ppo": algo = torch_ac.PPOAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_eps, args.clip_eps, args.epochs, args.batch_size, preprocess_obss) else: raise ValueError("Incorrect algorithm name: {}".format(args.algo)) if "optimizer_state" in status: algo.optimizer.load_state_dict(status["optimizer_state"]) txt_logger.info("Loading optimizer from existing run.\n") txt_logger.info("Optimizer loaded.\n") # init the evaluator if args.eval: eval_samplers = args.ltl_samplers_eval if args.ltl_samplers_eval else [args.ltl_sampler] eval_env = args.eval_env if args.eval_env else args.env eval_procs = args.eval_procs if args.eval_procs else args.procs evals = [] for eval_sampler in eval_samplers: evals.append(utils.Eval(eval_env, model_name, eval_sampler, seed=args.seed, device=device, num_procs=eval_procs, ignoreLTL=args.ignoreLTL, progression_mode=progression_mode, gnn=args.gnn, dumb_ac = args.dumb_ac)) # Train model num_frames = status["num_frames"] # num_frames:0 update = status["update"] # update:0 start_time = time.time() while num_frames < args.frames: # Update model parameters update_start_time = time.time() exps, logs1 = algo.collect_experiences() # interacte with environmets (very important) logs2 = algo.update_parameters(exps) logs = {**logs1, **logs2} update_end_time = time.time() num_frames += logs["num_frames"] update += 1 # Print logs if update % args.log_interval == 0: fps = logs["num_frames"]/(update_end_time - update_start_time) duration = int(time.time() - start_time) return_per_episode = utils.synthesize(logs["return_per_episode"]) rreturn_per_episode = utils.synthesize(logs["reshaped_return_per_episode"]) average_reward_per_step = utils.average_reward_per_step(logs["return_per_episode"], logs["num_frames_per_episode"]) average_discounted_return = utils.average_discounted_return(logs["return_per_episode"], logs["num_frames_per_episode"], args.discount) num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"]) header = ["update", "frames", "FPS", "duration"] data = [update, num_frames, fps, duration] header += ["rreturn_" + key for key in rreturn_per_episode.keys()] data += rreturn_per_episode.values() header += ["average_reward_per_step", "average_discounted_return"] data += [average_reward_per_step, average_discounted_return] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["entropy", "value", "policy_loss", "value_loss", "grad_norm"] data += [logs["entropy"], logs["value"], logs["policy_loss"], logs["value_loss"], logs["grad_norm"]] txt_logger.info( "U {} | F {:06} | FPS {:04.0f} | D {} | rR:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | ARPS: {:.3f} | ADR: {:.3f} | F:μσmM {:.1f} {:.1f} {} {} | H {:.3f} | V {:.3f} | pL {:.3f} | vL {:.3f} | ∇ {:.3f}" .format(*data)) header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() if status["num_frames"] == 0: csv_logger.writerow(header) csv_logger.writerow(data) csv_file.flush() for field, value in zip(header, data): tb_writer.add_scalar(field, value, num_frames) # Save status if args.save_interval > 0 and update % args.save_interval == 0: status = {"num_frames": num_frames, "update": update, "model_state": algo.acmodel.state_dict(), "optimizer_state": algo.optimizer.state_dict()} if hasattr(preprocess_obss, "vocab") and preprocess_obss.vocab is not None: status["vocab"] = preprocess_obss.vocab.vocab utils.save_status(status, model_dir + "/train") txt_logger.info("Status saved") if args.eval: # we send the num_frames to align the eval curves with the training curves on TB for evalu in evals: evalu.eval(num_frames, episodes=args.eval_episodes)
16,899
50.057402
265
py
T2TL
T2TL-main/src/context_model.py
""" This is the description of the deep NN currently being used. It is a small CNN for the features with an GRU encoding of the LTL task. The features and LTL are preprocessed by utils.format.get_obss_preprocessor(...) function: - In that function, I transformed the LTL tuple representation into a text representation: - Input: ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) - output: ['until', 'not', 'a', 'and', 'b', 'until', 'not', 'c', 'd'] Each of those tokens get a one-hot embedding representation by the utils.format.Vocabulary class. """ import torch import torch.nn as nn import torch.nn.functional as F from torch.distributions import Categorical, Normal import torch_ac import copy from gym.spaces import Box, Discrete from gnns.graphs.GCN import * from gnns.graphs.GNN import GNNMaker from env_model import getEnvModel from policy_network import PolicyNetwork from transEncoder import ContextTransformer # Function from https://github.com/ikostrikov/pytorch-a2c-ppo-acktr/blob/master/model.py def init_params(m): classname = m.__class__.__name__ if classname.find("Linear") != -1: m.weight.data.normal_(0, 1) m.weight.data *= 1 / torch.sqrt(m.weight.data.pow(2).sum(1, keepdim=True)) if m.bias is not None: m.bias.data.fill_(0) class ContextACModel(nn.Module,): def __init__(self, env, obs_space, action_space, ignoreLTL, gnn_type, dumb_ac, freeze_ltl, args): super().__init__() # Decide which components are enabled self.use_progression_info = "progress_info" in obs_space self.use_text = not ignoreLTL and (gnn_type == "GRU" or gnn_type == "LSTM") and "text" in obs_space self.use_ast = not ignoreLTL and ("GCN" in gnn_type) and "text" in obs_space # True self.use_trans = not ignoreLTL and ("Transformer" in gnn_type) and "text" in obs_space # True self.gnn_type = gnn_type self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.action_space = action_space self.dumb_ac = dumb_ac self.recurrent = False self.context = True self.cont_dim = args.cont_dim # the dimension for context variable self.freeze_pretrained_params = freeze_ltl if self.freeze_pretrained_params: print("Freezing the LTL module.") self.env_model = getEnvModel(env, obs_space) # Define text embedding if self.use_progression_info: self.text_embedding_size = 32 self.simple_encoder = nn.Sequential( nn.Linear(obs_space["progress_info"], 64), nn.Tanh(), nn.Linear(64, self.text_embedding_size), nn.Tanh() ).to(self.device) print("Linear encoder Number of parameters:", sum(p.numel() for p in self.simple_encoder.parameters() if p.requires_grad)) elif self.use_text: self.word_embedding_size = 32 self.text_embedding_size = args.gnn_out if self.gnn_type == "GRU": self.text_rnn = GRUModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to( self.device) else: assert (self.gnn_type == "LSTM") self.text_rnn = LSTMModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to( self.device) print("RNN Number of parameters:", sum(p.numel() for p in self.text_rnn.parameters() if p.requires_grad)) elif self.use_ast: hidden_dim = 32 self.text_embedding_size = 32 self.gnn = GNNMaker(self.gnn_type, obs_space["text"], self.text_embedding_size).to(self.device) # for param in self.gnn.parameters(): # param.requires_grad = False print("GNN Number of parameters:", sum(p.numel() for p in self.gnn.parameters() if p.requires_grad)) elif self.use_trans: self.word_embedding_size = 512 self.text_embedding_size = args.d_out self.ltl2transformer = TransfomerSyn(obs_space["text"], self.word_embedding_size, self.text_embedding_size, 'mean', args) # for param in self.ltl2transformer.parameters(): # param.requires_grad = False print("Transformer Number of parameters:", sum(p.numel() for p in self.ltl2transformer.parameters() if p.requires_grad)) # Resize image embedding self.embedding_size = self.env_model.size() # 64 # Context specific code reward_dim = 1 action_dim = self.action_space.shape[0] # action dim input_dim_context = action_dim + reward_dim + self.embedding_size # self.context = Context(hidden_sizes=[args.cont_dim], # input_dim=input_dim_context, # history_length=args.hist_length, # action_dim=action_dim, # obsr_dim=self.embedding_size, # device=args.device) # todo: update self.context = ContextTransformer(input_dim_context, self.embedding_size, self.word_embedding_size, self.text_embedding_size, 'mean', args, context=True) print("embedding size:", self.embedding_size) if self.use_text or self.use_ast or self.use_progression_info or self.use_trans: self.embedding_size += self.text_embedding_size # 96 if self.dumb_ac: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 1) ) else: # Define actor's model self.actor = PolicyNetwork(self.embedding_size + self.cont_dim, self.action_space, hiddens=[64, 64, 64], activation=nn.ReLU()) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size + self.cont_dim, 64), nn.Tanh(), nn.Linear(64, 64), nn.Tanh(), nn.Linear(64, 1) ) # Initialize parameters correctly self.apply(init_params) if self.use_trans and args.TFixup: self.ltl2transformer.init_by_TFixup(args) print("Transformer Module has been initialized") self.context.init_by_TFixup(args) print("Context Module has been initialized") def forward(self, obs, context): embedding_ = self.env_model(obs) # shape = torch.Size([16, 64]) cont = self.context(context) if self.use_progression_info: embed_ltl = self.simple_encoder(obs.progress_info) embedding = torch.cat((embedding_, embed_ltl), dim=1) if embedding_ is not None else embed_ltl # Adding Text elif self.use_text: embed_text = self.text_rnn(obs.text) embedding = torch.cat((embedding_, embed_text), dim=1) if embedding_ is not None else embed_text # Adding GNN elif self.use_ast: embed_gnn = self.gnn(obs.text) # shape = torch.Size([16, 32]) embedding = torch.cat((embedding_, embed_gnn), dim=1) if embedding_ is not None else embed_gnn # shape = torch.Size([16, 96]) elif self.use_trans: embed_transformer = self.ltl2transformer(obs.text) embedding = torch.cat((embedding_, embed_transformer), dim=1) if embedding_ is not None else embed_transformer embedding = torch.cat((embedding, cont), dim=1) # Actor dist = self.actor(embedding) # Critic x = self.critic(embedding) value = x.squeeze(1) return dist, value, embedding_ def load_pretrained_gnn(self, model_state): # We delete all keys relating to the actor/critic. new_model_state = model_state.copy() for key in model_state.keys(): if key.find("actor") != -1 or key.find("critic") != -1: # ??? key.find()? del new_model_state[key] self.load_state_dict(new_model_state, strict=False) if self.freeze_pretrained_params: target = self.text_rnn if self.gnn_type == "GRU" or self.gnn_type == "LSTM" else self.gnn for param in target.parameters(): param.requires_grad = False class TransfomerSyn(nn.Module): def __init__(self, obs_size, d_model, d_out, pool, args, context=False): super(TransfomerSyn, self).__init__() self.context = context if self.context: self.embed_linear = nn.Linear(obs_size, args.d_model) self.transformer = TransformerEncoderModel(d_model=args.cont_d_model//2, nhead=args.cont_nhead, num_encoder_layers=args.cont_num_encoder_layers//2, pool=args.cont_pool, dim_feedforward=args.cont_dim_feedforward//2, dropout=args.dropout, d_out=args.cont_d_out, layer_norm_eps=args.layer_norm_eps) else: self.embedded = nn.Embedding(obs_size, args.d_model) self.transformer = TransformerEncoderModel(d_model=args.d_model, nhead=args.nhead, num_encoder_layers=args.num_encoder_layers, pool=args.pool, dim_feedforward=args.dim_feedforward, dropout=args.dropout, d_out=args.d_out, layer_norm_eps=args.layer_norm_eps) def forward(self, text): if self.context: embed_text = self.embed_linear(text) else: embed_text = self.embedded(text) feature = self.transformer(embed_text) return feature def init_by_TFixup(self, args): # todo:debug # for k, v in self.transformer.named_parameters(): # print(k, v, v.shape) for p in self.embedded.parameters(): if p.dim() > 1: torch.nn.init.normal_(p, 0, args.d_model ** (- 1. / 2.)) temp_state_dic = {} for name, param in self.embedded.named_parameters(): if 'weight' in name: temp_state_dic[name] = ((9 * args.num_encoder_layers) ** (- 1. / 4.)) * param for name in self.embedded.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.embedded.state_dict()[name] self.embedded.load_state_dict(temp_state_dic) temp_state_dic = {} for name, param in self.transformer.named_parameters(): if any(s in name for s in ["linear1.weight", "linear2.weight", "self_attn.out_proj.weight"]): temp_state_dic[name] = (0.67 * (args.num_encoder_layers) ** (- 1. / 4.)) * param elif "self_attn.in_proj_weight" in name: temp_state_dic[name] = (0.67 * (args.num_encoder_layers) ** (- 1. / 4.)) * (param * (2 ** 0.5)) for name in self.transformer.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.transformer.state_dict()[name] self.transformer.load_state_dict(temp_state_dic) class LSTMModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() # For all our experiments we want the embedding to be a fixed size so we can "transfer". self.word_embedding = nn.Embedding(obs_size, word_embedding_size) self.lstm = nn.LSTM(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2 * hidden_dim, text_embedding_size) def forward(self, text): hidden, _ = self.lstm(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class GRUModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() self.word_embedding = nn.Embedding(obs_size, word_embedding_size) # word_embedding_size = 32, hidden_dim = 16 self.gru = nn.GRU(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2 * hidden_dim, text_embedding_size) def forward(self, text): # hidden_shape: [16, 9, 32] _shape: [4, 16, 16] hidden, _ = self.gru(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class TransformerEncoderModel(nn.Module): def __init__(self, d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 1, pool: str = 'mean', dim_feedforward: int = 2048, dropout: float = 0.1, d_out: int = 8, activation=F.relu, layer_norm_eps: float = 1e-5, batch_first: bool = True, norm_first: bool = False): """ :param d_model: the number of expected features in the encoder/decoder inputs (default=512). :param nhead: the number of heads in the multiheadattention models (default=8). :param num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6). :param dim_feedforward: the dimension of the feedforward network model (default=2048). :param dropout: the dropout value (default=0.1). :param activation: the activation function of encoder/decoder intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu :param layer_norm_eps: the eps value in layer normalization components (default=1e-5). :param batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False`` (seq, batch, feature). :param norm_first: if ``True``, encoder and decoder layers will perform LayerNorms before other attention and feedforward operations, otherwise after. Default: ``False`` Examples:: >>> transformer_model = nn.Transformer(nhead=16, num_encoder_layers=12) >>> src = torch.rand((10, 32, 512)) >>> out = transformer_model(src) """ super(TransformerEncoderModel, self).__init__() self.d_model = d_model self.nhead = nhead self.batch_first = batch_first encoder_layer = TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first) encoder_norm = nn.LayerNorm(d_model, eps=layer_norm_eps) self.encoder = TransformerEncoder(encoder_layer, num_encoder_layers, encoder_norm) self.pool = pool self.to_latent = nn.Identity() self.mlp_head = nn.Sequential( nn.LayerNorm(d_model), nn.Linear(d_model, d_out) ) self._reset_parameters() def forward(self, src, src_mask=None, src_key_padding_mask=None): """ Args: src: the sequence to the encoder (required). src_mask: the additive mask for the src sequence (optional). src_key_padding_mask: the ByteTensor mask for src keys per batch (optional). Shape: - src: :math:`(S, N, E)`, `(N, S, E)` if batch_first. - src_mask: :math:`(S, S)`. - src_key_padding_mask: :math:`(N, S)`. where S is the source sequence length, T is the target sequence length, N is the batch size, E is the feature number """ memory = self.encoder(src, mask=src_mask, src_key_padding_mask=src_key_padding_mask) memory = memory.mean(dim=1) if self.pool == 'mean' else memory[:, -1, :] memory = self.to_latent(memory) memory = torch.tanh(self.mlp_head(memory)) return memory def _reset_parameters(self): r"""Initiate parameters in the transformer model.""" for p in self.parameters(): if p.dim() > 1: torch.nn.init.xavier_uniform_(p) class TransformerEncoder(nn.Module): def __init__(self, encoder_layer, num_layers, norm=None): r"""TransformerEncoder is a stack of N encoder layers Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) """ super(TransformerEncoder, self).__init__() self.layers = _get_clones(encoder_layer, num_layers) self.num_layers = num_layers self.norm = norm def forward(self, src, mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layers in turn. Args: src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ output = src for mod in self.layers: output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask) if self.norm is not None: output = self.norm(output) return output class TransformerEncoderLayer(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=F.relu, layer_norm_eps=1e-5, batch_first=False, norm_first=False): """ Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). activation: the activation function of the intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu layer_norm_eps: the eps value in layer normalization components (default=1e-5). batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False``. norm_first: if ``True``, layer norm is done prior to attention and feedforward operations, respectivaly. Otherwise it's done after. Default: ``False`` (after). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8, batch_first=True) >>> src = torch.rand(32, 10, 512) >>> out = encoder_layer(src) """ super(TransformerEncoderLayer, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=batch_first) # Implementation of Feedforward model self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm_first = norm_first self.norm1 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.norm2 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = activation def forward(self, src, src_mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ x = src if self.norm_first: x = x + self._sa_block(self.norm1(x), src_mask, src_key_padding_mask) x = x + self._ff_block(self.norm2(x)) else: x = self.norm1(x + self._sa_block(x, src_mask, src_key_padding_mask)) x = self.norm2(x + self._ff_block(x)) return x # self-attention block def _sa_block(self, x, attn_mask, key_padding_mask): x = self.self_attn(x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=False)[0] return self.dropout1(x) # feed forward block def _ff_block(self, x): x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x) def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)]) class Context(nn.Module): """ This layer just does non-linear transformation(s) """ def __init__(self, hidden_sizes=[50], input_dim=None, history_length=1, action_dim=None, obsr_dim=None, device='cpu' ): super(Context, self).__init__() self.hidden_sizes = hidden_sizes self.input_dim = input_dim self.hist_length = history_length self.device = device self.action_dim = action_dim self.obsr_dim = obsr_dim self.recurrent = nn.GRU(self.input_dim, self.hidden_sizes[0], bidirectional=False, batch_first=True, num_layers=1) def init_recurrent(self, bsize=None): ''' init hidden states Batch size can't be none ''' # The order is (num_layers, minibatch_size, hidden_dim) # LSTM ==> return (torch.zeros(1, bsize, self.hidden_sizes[0]), # torch.zeros(1, bsize, self.hidden_sizes[0])) return torch.zeros(1, bsize, self.hidden_sizes[0]).to(self.device) def forward(self, data): ''' pre_x : B * D where B is batch size and D is input_dim pre_a : B * A where B is batch size and A is input_dim previous_reward: B * 1 where B is batch size and 1 is input_dim ''' previous_action, previous_reward, pre_x = data[0], data[1], data[2] # first prepare data for LSTM bsize, dim = previous_action.shape # previous_action is B* (history_len * D) pacts = previous_action.view(bsize, -1, self.action_dim) # view(bsize, self.hist_length, -1) prews = previous_reward.view(bsize, -1, 1) # reward dim is 1, view(bsize, self.hist_length, 1) pxs = pre_x.view(bsize, -1, self.obsr_dim) # view(bsize, self.hist_length, -1) pre_act_rew = torch.cat([pacts, prews, pxs], dim=-1) # input to LSTM is [action, reward] # init lstm/gru hidden = self.init_recurrent(bsize=bsize) # lstm/gru _, hidden = self.recurrent(pre_act_rew, hidden) # hidden is (1, B, hidden_size) out = hidden.squeeze(0) # (1, B, hidden_size) ==> (B, hidden_size) return out
24,186
42.817029
122
py
T2TL
T2TL-main/src/T2TL_pretrain.py
import argparse import time import datetime import torch import torch_ac import tensorboardX import sys import glob from math import floor import utils from model import ACModel from context_model import ContextACModel if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser() ## General parameters parser.add_argument("--algo", default='ppo', help="algorithm to use: a2c | ppo (REQUIRED)") parser.add_argument("--env", default='Zones-25-v1', help="name of the environment to train on (REQUIRED)") parser.add_argument("--ltl-sampler", default="Until_1_2_1_1", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--model", default=None, help="name of the model (default: {ENV}_{SAMPLER}_{ALGO}_{TIME})") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--log-interval", type=int, default=1, help="number of updates between two logs (default: 10)") parser.add_argument("--save-interval", type=int, default=2, help="number of updates between two saves (default: 10, 0 means no saving)") parser.add_argument("--procs", type=int, default=16, help="number of processes (default: 16)") parser.add_argument("--frames", type=int, default=1*10**7, help="number of frames of training (default: 2*10e8)") parser.add_argument("--checkpoint-dir", default=None) ## Evaluation parameters parser.add_argument("--eval", action="store_true", default=False, help="evaluate the saved model (default: False)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--eval-env", default=None, help="name of the environment to train on (default: use the same \"env\" as training)") parser.add_argument("--ltl-samplers-eval", default=None, nargs='+', help="the ltl formula templates to sample from for evaluation (default: use the same \"ltl-sampler\" as training)") parser.add_argument("--eval-procs", type=int, default=1, help="number of processes (default: use the same \"procs\" as training)") ## Parameters for main algorithm parser.add_argument("--epochs", type=int, default=10, help="number of epochs for PPO (default: 4)") parser.add_argument("--batch-size", type=int, default=1024, help="batch size for PPO (default: 256)") parser.add_argument("--frames-per-proc", type=int, default=4096, help="number of frames per process before update (default: 5 for A2C and 128 for PPO)") parser.add_argument("--discount", type=float, default=0.998, help="discount factor (default: 0.99)") parser.add_argument("--lr", type=float, default=0.0003, help="learning rate (default: 0.0003)") parser.add_argument("--gae-lambda", type=float, default=0.95, help="lambda coefficient in GAE formula (default: 0.95, 1 means no gae)") parser.add_argument("--entropy-coef", type=float, default=0.003, help="entropy term coefficient (default: 0.01)") parser.add_argument("--value-loss-coef", type=float, default=0.5, help="value loss term coefficient (default: 0.5)") parser.add_argument("--max-grad-norm", type=float, default=0.5, help="maximum norm of gradient (default: 0.5)") parser.add_argument("--optim-eps", type=float, default=1e-8, help="Adam and RMSprop optimizer epsilon (default: 1e-8)") parser.add_argument("--optim-alpha", type=float, default=0.99, help="RMSprop optimizer alpha (default: 0.99)") parser.add_argument("--clip-eps", type=float, default=0.2, help="clipping epsilon for PPO (default: 0.2)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--noLTL", action="store_true", default=False, help="the environment no longer has an LTL goal. --ignoreLTL must be specified concurrently.") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="Transformer", help="use gnn to model the LTL (only if ignoreLTL==True)") parser.add_argument("--trans_layer", type=int, default=1, help="the number of Transformer layers need to use") parser.add_argument("--int-reward", type=float, default=0.0, help="the intrinsic reward for LTL progression (default: 0.0)") parser.add_argument("--pretrained-gnn", action="store_true", default=True, help="load a pre-trained LTL module.") parser.add_argument("--dumb-ac", action="store_true", default=False, help="Use a single-layer actor-critic") parser.add_argument("--freeze-ltl", action="store_true", default=False, help="Freeze the gradient updates of the LTL module") # Transformer special parameters parser.add_argument("--d_model", type=int, default=64, help="") parser.add_argument("--nhead", type=int, default=8, help="") parser.add_argument("--num_encoder_layers", type=int, default=4, help="") parser.add_argument("--pool", type=str, default='mean', help="") parser.add_argument("--dim_feedforward", type=int, default=256, help="") parser.add_argument("--dropout", type=float, default=0.0, help="") parser.add_argument("--d_out", type=int, default=16, help="") parser.add_argument("--layer_norm_eps", type=float, default=1e-5, help="") parser.add_argument("--TFixup", type=bool, default=True, help="") # Context Special variable parser.add_argument("--ContType", type=str, default='Transformer', help="To choose which model to encode CONTEXT VARIABLE (e.g., RNN, Transformer)") parser.add_argument("--use_cont", type=bool, default=True, help="") parser.add_argument("--hist_length", type=int, default=8, help="") parser.add_argument("--cont_dim", type=int, default=16, help="") parser.add_argument("--cont_d_model", type=int, default=64, help="") parser.add_argument("--cont_nhead", type=int, default=8, help="") parser.add_argument("--cont_num_encoder_layers", type=int, default=2, help="") parser.add_argument("--cont_pool", type=str, default='mean', help="") parser.add_argument("--cont_dim_feedforward", type=int, default=256, help="") parser.add_argument("--cont_d_out", type=int, default=16, help="") # device parser.add_argument("--cuda", type=str, default='cuda:0', help="") parser.add_argument("--device", type=str, default='cuda:0', help="") # additional desciption for test parser.add_argument("--sth", type=str, default='None', help="") args = parser.parse_args() use_mem = args.recurrence > 1 # whether use memory or not # Set run dir date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") # '21-08-21-22-36-39' gnn_name = args.gnn if args.ignoreLTL: gnn_name = "IgnoreLTL" if args.dumb_ac: gnn_name = gnn_name + "-dumb_ac" if args.pretrained_gnn: gnn_name = gnn_name + "-pretrained" if args.freeze_ltl: gnn_name = gnn_name + "-freeze_ltl" if use_mem: gnn_name = gnn_name + "-recurrence:%d"%(args.recurrence) if args.gnn == 'Transformer': default_model_name = f"{gnn_name}+TL2_{args.ltl_sampler}_{args.env}_seed:{args.seed}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_FFD:{args.dim_feedforward}_d_out:{args.d_out}_hist:{args.hist_length}_L_Cont:{args.cont_num_encoder_layers}_Init:{args.TFixup}_sth:{args.sth}" else: # 'RGCN_8x32_ROOT_SHARED_Until_1_2_1_1_Zones-5-v0_seed:1_epochs:10_bs:2048_fpp:4096_dsc:0.998_lr:0.0003_ent:0.003_clip:0.2_prog:full' default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_epochs:{args.epochs}_bs:{args.batch_size}_fpp:{args.frames_per_proc}_dsc:{args.discount}_lr:{args.lr}_ent:{args.entropy_coef}_clip:{args.clip_eps}_prog:{args.progression_mode}" model_name = args.model or default_model_name storage_dir = "storage" if args.checkpoint_dir is None else args.checkpoint_dir model_dir = utils.get_model_dir(model_name, storage_dir) pretrained_model_dir = None if args.pretrained_gnn: assert(args.progression_mode == "full") # default_dir = f"symbol-storage/{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" # default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_*_d_out:{args.d_out}/train" default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_dim_feedforward:{args.dim_feedforward}_d_out:{args.d_out}_None/train" print(default_dir) model_dirs = glob.glob(default_dir) if len(model_dirs) == 0: raise Exception("Pretraining directory not found.") elif len(model_dirs) > 1: raise Exception("More than 1 candidate pretraining directory found.") pretrained_model_dir = model_dirs[0] # Load loggers and Tensorboard writer txt_logger = utils.get_txt_logger(model_dir + "/train") csv_file, csv_logger = utils.get_csv_logger(model_dir + "/train") tb_writer = tensorboardX.SummaryWriter(model_dir + "/train") utils.save_config(model_dir + "/train", args) # Log command and all script arguments txt_logger.info("{}\n".format(" ".join(sys.argv))) txt_logger.info("{}\n".format(args)) # It will output the context of Namespace # Set seed for all randomness sources utils.seed(args.seed) # Set device device = torch.device(args.cuda if torch.cuda.is_available() else "cpu") # device = torch.device('cpu') txt_logger.info(f"Device: {device}\n") # Output the device (default is cpu) # Load environments envs = [] progression_mode = args.progression_mode for i in range(args.procs): # load the env & progression_mode & LTL formula # turn to utils/env.py envs.append(utils.make_env(args.env, progression_mode, args.ltl_sampler, args.seed, args.int_reward, args.noLTL)) # Sync environments envs[0].reset() # Add the agent to map & translate the LTL formula txt_logger.info("Environments loaded\n") # Load training status try: status = utils.get_status(model_dir + "/train", args) except OSError: status = {"num_frames": 0, "update": 0} # ??? the state of algorithm ? txt_logger.info("Training status loaded.\n") if pretrained_model_dir is not None: try: pretrained_status = utils.get_status(pretrained_model_dir, args) except: txt_logger.info("Failed to load pretrained model.\n") exit(1) # Load observations preprocessor-- build AST using_gnn = (args.gnn != "GRU" and args.gnn != "LSTM" and args.gnn != "Transformer") # turn to env/format.py obs_space, preprocess_obss = utils.get_obss_preprocessor(envs[0], using_gnn, progression_mode) if "vocab" in status and preprocess_obss.vocab is not None: preprocess_obss.vocab.load_vocab(status["vocab"]) txt_logger.info("Observations preprocessor loaded.\n") # Load model if use_mem: acmodel = RecurrentACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl) else: acmodel = ContextACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl, args) if "model_state" in status: acmodel.load_state_dict(status["model_state"]) txt_logger.info("Loading model from existing run.\n") elif args.pretrained_gnn: acmodel.load_pretrained_gnn(pretrained_status["model_state"]) txt_logger.info("Pretrained model loaded.\n") acmodel.to(device) txt_logger.info("Model loaded.\n") txt_logger.info("{}\n".format(acmodel)) # Load algo if args.algo == "a2c": algo = torch_ac.A2CAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_alpha, args.optim_eps, preprocess_obss) elif args.algo == "ppo": algo = torch_ac.PPOAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_eps, args.clip_eps, args.epochs, args.batch_size, preprocess_obss, history_length=args.hist_length) else: raise ValueError("Incorrect algorithm name: {}".format(args.algo)) if "optimizer_state" in status: algo.optimizer.load_state_dict(status["optimizer_state"]) txt_logger.info("Loading optimizer from existing run.\n") txt_logger.info("Optimizer loaded.\n") # init the evaluator if args.eval: eval_samplers = args.ltl_samplers_eval if args.ltl_samplers_eval else [args.ltl_sampler] eval_env = args.eval_env if args.eval_env else args.env eval_procs = args.eval_procs if args.eval_procs else args.procs evals = [] for eval_sampler in eval_samplers: evals.append(utils.Eval(eval_env, model_name, eval_sampler, seed=args.seed, device=device, num_procs=eval_procs, ignoreLTL=args.ignoreLTL, progression_mode=progression_mode, gnn=args.gnn, dumb_ac = args.dumb_ac)) # Train model num_frames = status["num_frames"] # num_frames:0 update = status["update"] # update:0 start_time = time.time() while num_frames < args.frames: # Update model parameters update_start_time = time.time() exps, logs1 = algo.collect_experiences() # interacte with environmets (very important) logs2 = algo.update_parameters(exps) logs = {**logs1, **logs2} update_end_time = time.time() num_frames += logs["num_frames"] update += 1 # Print logs if update % args.log_interval == 0: fps = logs["num_frames"]/(update_end_time - update_start_time) duration = int(time.time() - start_time) return_per_episode = utils.synthesize(logs["return_per_episode"]) rreturn_per_episode = utils.synthesize(logs["reshaped_return_per_episode"]) average_reward_per_step = utils.average_reward_per_step(logs["return_per_episode"], logs["num_frames_per_episode"]) average_discounted_return = utils.average_discounted_return(logs["return_per_episode"], logs["num_frames_per_episode"], args.discount) num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"]) header = ["update", "frames", "FPS", "duration"] data = [update, num_frames, fps, duration] header += ["rreturn_" + key for key in rreturn_per_episode.keys()] data += rreturn_per_episode.values() header += ["average_reward_per_step", "average_discounted_return"] data += [average_reward_per_step, average_discounted_return] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["entropy", "value", "policy_loss", "value_loss", "grad_norm"] data += [logs["entropy"], logs["value"], logs["policy_loss"], logs["value_loss"], logs["grad_norm"]] txt_logger.info( "U {} | F {:06} | FPS {:04.0f} | D {} | rR:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | ARPS: {:.3f} | ADR: {:.3f} | F:μσmM {:.1f} {:.1f} {} {} | H {:.3f} | V {:.3f} | pL {:.3f} | vL {:.3f} | ∇ {:.3f}" .format(*data)) header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() if status["num_frames"] == 0: csv_logger.writerow(header) csv_logger.writerow(data) csv_file.flush() for field, value in zip(header, data): tb_writer.add_scalar(field, value, num_frames) # Save status if args.save_interval > 0 and update % args.save_interval == 0: status = {"num_frames": num_frames, "update": update, "model_state": algo.acmodel.state_dict(), "optimizer_state": algo.optimizer.state_dict()} if hasattr(preprocess_obss, "vocab") and preprocess_obss.vocab is not None: status["vocab"] = preprocess_obss.vocab.vocab utils.save_status(status, model_dir + "/train") txt_logger.info("Status saved") if args.eval: # we send the num_frames to align the eval curves with the training curves on TB for evalu in evals: evalu.eval(num_frames, episodes=args.eval_episodes)
18,009
51.354651
313
py
T2TL
T2TL-main/src/ltl_wrappers.py
""" This is a simple wrapper that will include LTL goals to any given environment. It also progress the formulas as the agent interacts with the envirionment. However, each environment must implement the followng functions: - *get_events(...)*: Returns the propositions that currently hold on the environment. - *get_propositions(...)*: Maps the objects in the environment to a set of propositions that can be referred to in LTL. Notes about LTLEnv: - The episode ends if the LTL goal is progressed to True or False. - If the LTL goal becomes True, then an extra +1 reward is given to the agent. - If the LTL goal becomes False, then an extra -1 reward is given to the agent. - Otherwise, the agent gets the same reward given by the original environment. """ import numpy as np import gym from gym import spaces import ltl_progression, random from ltl_samplers import getLTLSampler, SequenceSampler def prYellow(prt): print("\033[93m {}\033[00m".format(prt)) class LTLEnv(gym.Wrapper): def __init__(self, env, progression_mode="full", ltl_sampler=None, intrinsic=0.0): """ LTL environment -------------------- It adds an LTL objective to the current environment - The observations become a dictionary with an added "text" field specifying the LTL objective - It also automatically progress the formula and generates an appropriate reward function - However, it does requires the user to define a labeling function and a set of training formulas progression_mode: - "full": the agent gets the full, progressed LTL formula as part of the observation - "partial": the agent sees which propositions (individually) will progress or falsify the formula - "none": the agent gets the full, original LTL formula as part of the observation """ super().__init__(env) self.progression_mode = progression_mode self.propositions = self.env.get_propositions() self.sampler = getLTLSampler(ltl_sampler, self.propositions) self.observation_space = spaces.Dict({'features': env.observation_space}) self.known_progressions = {} self.intrinsic = intrinsic def sample_ltl_goal(self): # This function must return an LTL formula for the task # Format: #( # 'and', # ('until','True', ('and', 'd', ('until','True',('not','c')))), # ('until','True', ('and', 'a', ('until','True', ('and', 'b', ('until','True','c'))))) #) # NOTE: The propositions must be represented by a char raise NotImplementedError def get_events(self, obs, act, next_obs): # This function must return the events that currently hold on the environment # NOTE: The events are represented by a string containing the propositions with positive values only (e.g., "ac" means that only propositions 'a' and 'b' hold) raise NotImplementedError def reset(self): self.known_progressions = {} self.obs = self.env.reset() self.ltl_goal = ('eventually', ('and', 'R', ('eventually', ('and', 'J', ('eventually', 'Y'))))) self.ltl_original = self.ltl_goal # Adding the ltl goal to the observation if self.progression_mode == "partial": ltl_obs = {'features': self.obs,'progress_info': self.progress_info(self.ltl_goal)} else: ltl_obs = {'features': self.obs,'text': self.ltl_goal} return ltl_obs def step(self, action): int_reward = 0 # executing the action in the environment next_obs, original_reward, env_done, info = self.env.step(action) # prYellow('original_reward is {}'.format(original_reward)) # progressing the ltl formula truth_assignment = self.get_events(self.obs, action, next_obs) self.ltl_goal = self.progression(self.ltl_goal, truth_assignment) self.obs = next_obs # Computing the LTL reward and done signal ltl_reward = 0.0 ltl_done = False if self.ltl_goal == 'True': ltl_reward = 1.0 ltl_done = True elif self.ltl_goal == 'False': ltl_reward = -1.0 ltl_done = True else: ltl_reward = int_reward # Computing the new observation and returning the outcome of this action if self.progression_mode == "full": ltl_obs = {'features': self.obs,'text': self.ltl_goal} elif self.progression_mode == "none": ltl_obs = {'features': self.obs,'text': self.ltl_original} elif self.progression_mode == "partial": ltl_obs = {'features': self.obs, 'progress_info': self.progress_info(self.ltl_goal)} else: raise NotImplementedError reward = original_reward + ltl_reward done = env_done or ltl_done return ltl_obs, reward, done, info def progression(self, ltl_formula, truth_assignment): if (ltl_formula, truth_assignment) not in self.known_progressions: result_ltl = ltl_progression.progress_and_clean(ltl_formula, truth_assignment) self.known_progressions[(ltl_formula, truth_assignment)] = result_ltl return self.known_progressions[(ltl_formula, truth_assignment)] # # X is a vector where index i is 1 if prop i progresses the formula, -1 if it falsifies it, 0 otherwise. def progress_info(self, ltl_formula): propositions = self.env.get_propositions() X = np.zeros(len(self.propositions)) for i in range(len(propositions)): progress_i = self.progression(ltl_formula, propositions[i]) if progress_i == 'False': X[i] = -1. elif progress_i != ltl_formula: X[i] = 1. return X def sample_ltl_goal(self): # NOTE: The propositions must be represented by a char # This function must return an LTL formula for the task formula = self.sampler.sample() if isinstance(self.sampler, SequenceSampler): def flatten(bla): output = [] for item in bla: output += flatten(item) if isinstance(item, tuple) else [item] return output length = flatten(formula).count("and") + 1 self.env.timeout = 25 # 10 * length return formula def get_events(self, obs, act, next_obs): # This function must return the events that currently hold on the environment # NOTE: The events are represented by a string containing the propositions with positive values only (e.g., "ac" means that only propositions 'a' and 'b' hold) return self.env.get_events() class NoLTLWrapper(gym.Wrapper): def __init__(self, env): """ Removes the LTL formula from an LTLEnv It is useful to check the performance of off-the-shelf agents """ super().__init__(env) self.observation_space = env.observation_space # self.observation_space = env.observation_space['features'] def reset(self): obs = self.env.reset() # obs = obs['features'] # obs = {'features': obs} return obs def step(self, action): # executing the action in the environment obs, reward, done, info = self.env.step(action) # obs = obs['features'] # obs = {'features': obs} return obs, reward, done, info def get_propositions(self): return list([])
7,689
38.84456
167
py
T2TL
T2TL-main/src/env_model.py
import torch import torch.nn as nn from envs import * from gym.envs.classic_control import PendulumEnv def getEnvModel(env, obs_space): env = env.unwrapped if isinstance(env, ZonesEnv): return ZonesEnvModel(obs_space) # Add your EnvModel here... # The default case (No environment observations) - SimpleLTLEnv uses this return EnvModel(obs_space) """ This class is in charge of embedding the environment part of the observations. Every environment has its own set of observations ('image', 'direction', etc) which is handeled here by associated EnvModel subclass. How to subclass this: 1. Call the super().__init__() from your init 2. In your __init__ after building the compute graph set the self.embedding_size appropriately 3. In your forward() method call the super().forward as the default case. 4. Add the if statement in the getEnvModel() method """ class EnvModel(nn.Module): def __init__(self, obs_space): super().__init__() self.embedding_size = 0 def forward(self, obs): return None def size(self): return self.embedding_size class LetterEnvModel(EnvModel): def __init__(self, obs_space): super().__init__(obs_space) if "image" in obs_space.keys(): n = obs_space["image"][0] m = obs_space["image"][1] k = obs_space["image"][2] self.image_conv = nn.Sequential( nn.Conv2d(k, 16, (2, 2)), nn.ReLU(), nn.Conv2d(16, 32, (2, 2)), nn.ReLU(), nn.Conv2d(32, 64, (2, 2)), nn.ReLU() ) self.embedding_size = (n-3)*(m-3)*64 def forward(self, obs): if "image" in obs.keys(): x = obs.image.transpose(1, 3).transpose(2, 3) x = self.image_conv(x) x = x.reshape(x.shape[0], -1) return x return super().forward(obs) class MinigridEnvModel(EnvModel): def __init__(self, obs_space): super().__init__(obs_space) if "image" in obs_space.keys(): n = obs_space["image"][0] m = obs_space["image"][1] k = obs_space["image"][2] self.image_conv = nn.Sequential( nn.Conv2d(k, 16, (2, 2)), nn.ReLU(), nn.MaxPool2d((2, 2)), nn.Conv2d(16, 32, (2, 2)), nn.ReLU(), nn.Conv2d(32, 64, (2, 2)), nn.ReLU() ) self.embedding_size = ((n-1)//2-2)*((m-1)//2-2)*64 def forward(self, obs): if "image" in obs.keys(): x = obs.image.transpose(1, 3).transpose(2, 3) x = self.image_conv(x) x = x.reshape(x.shape[0], -1) return x return super().forward(obs) class ZonesEnvModel(EnvModel): def __init__(self, obs_space): super().__init__(obs_space) if "image" in obs_space.keys(): n = obs_space["image"][0] lidar_num_bins = 16 self.embedding_size = 64 #(n-12)//lidar_num_bins + 4 self.net_ = nn.Sequential( nn.Linear(n, 128), nn.ReLU(), nn.Linear(128, self.embedding_size), nn.ReLU() ) # embedding_size = number of propositional lidars + 4 normal sensors def forward(self, obs): if "image" in obs.keys(): return self.net_(obs.image) return super().forward(obs) class PendulumEnvModel(EnvModel): def __init__(self, obs_space): super().__init__(obs_space) if "image" in obs_space.keys(): self.net_ = nn.Sequential( nn.Linear(obs_space["image"][0], 3), nn.Tanh(), # nn.Linear(3, 3), # nn.Tanh() ) self.embedding_size = 3 def forward(self, obs): if "image" in obs.keys(): x = obs.image # x = torch.cat((x, x * x), 1) x = self.net_(x) return x return super().forward(obs)
4,146
28.204225
98
py
T2TL
T2TL-main/src/model.py
""" This is the description of the deep NN currently being used. It is a small CNN for the features with an GRU encoding of the LTL task. The features and LTL are preprocessed by utils.format.get_obss_preprocessor(...) function: - In that function, I transformed the LTL tuple representation into a text representation: - Input: ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) - output: ['until', 'not', 'a', 'and', 'b', 'until', 'not', 'c', 'd'] Each of those tokens get a one-hot embedding representation by the utils.format.Vocabulary class. """ import torch import torch.nn as nn import torch.nn.functional as F from torch.distributions import Categorical, Normal import torch_ac import copy from gym.spaces import Box, Discrete from gnns.graphs.GCN import * from gnns.graphs.GNN import GNNMaker from env_model import getEnvModel from policy_network import PolicyNetwork # Function from https://github.com/ikostrikov/pytorch-a2c-ppo-acktr/blob/master/model.py def init_params(m): classname = m.__class__.__name__ if classname.find("Linear") != -1: m.weight.data.normal_(0, 1) m.weight.data *= 1 / torch.sqrt(m.weight.data.pow(2).sum(1, keepdim=True)) if m.bias is not None: m.bias.data.fill_(0) class BasicACModel(nn.Module, torch_ac.ACModel): def __init__(self, env, obs_space, action_space, ignoreLTL, gnn_type, dumb_ac, freeze_ltl, args): super().__init__() # Decide which components are enabled self.use_progression_info = "progress_info" in obs_space self.use_text = not ignoreLTL and (gnn_type == "GRU" or gnn_type == "LSTM") and "text" in obs_space self.use_ast = not ignoreLTL and ("GCN" in gnn_type) and "text" in obs_space # True self.use_trans = not ignoreLTL and ("Transformer" in gnn_type) and "text" in obs_space # True self.use_dfa = not ignoreLTL and ("DFA" in gnn_type) and "text" in obs_space # True self.gnn_type = gnn_type self.device = torch.device(args.cuda) self.action_space = action_space self.dumb_ac = dumb_ac self.context = False self.freeze_pretrained_params = freeze_ltl if self.freeze_pretrained_params: print("Freezing the LTL module.") self.env_model = getEnvModel(env, obs_space) # Resize image embedding self.embedding_size = self.env_model.size() # 64 print("embedding size:", self.embedding_size) if self.use_text or self.use_ast or self.use_progression_info or self.use_trans or self.use_dfa: self.embedding_size += args.d_out # 96 if self.dumb_ac: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 1) ) else: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space, hiddens=[64, 64, 64], activation=nn.ReLU()) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 64), nn.Tanh(), nn.Linear(64, 64), nn.Tanh(), nn.Linear(64, 1) ) # Initialize parameters correctly self.apply(init_params) def forward(self, obs): embedding = self.env_model(obs) # shape = torch.Size([16, 64]) embedding = torch.cat((embedding, obs.text), dim=-1) # print(embedding[:, -4:]) # Actor dist = self.actor(embedding) # Critic x = self.critic(embedding) value = x.squeeze(1) return dist, value def load_pretrained_gnn(self, model_state): # We delete all keys relating to the actor/critic. new_model_state = model_state.copy() for key in model_state.keys(): if key.find("actor") != -1 or key.find("critic") != -1: del new_model_state[key] self.load_state_dict(new_model_state, strict=False) if self.freeze_pretrained_params: target = self.text_rnn if self.gnn_type == "GRU" or self.gnn_type == "LSTM" else self.gnn for param in target.parameters(): param.requires_grad = False class ACModel(nn.Module, torch_ac.ACModel): def __init__(self, env, obs_space, action_space, ignoreLTL, gnn_type, dumb_ac, freeze_ltl, args): super().__init__() # Decide which components are enabled self.use_progression_info = "progress_info" in obs_space self.use_text = not ignoreLTL and (gnn_type == "GRU" or gnn_type == "LSTM") and "text" in obs_space self.use_ast = not ignoreLTL and ("GCN" in gnn_type) and "text" in obs_space # True self.use_trans = not ignoreLTL and ("Transformer" in gnn_type) and "text" in obs_space # True self.gnn_type = gnn_type self.device = torch.device(args.cuda) self.action_space = action_space self.dumb_ac = dumb_ac self.context = False self.freeze_pretrained_params = freeze_ltl if self.freeze_pretrained_params: print("Freezing the LTL module.") self.env_model = getEnvModel(env, obs_space) # Define text embedding if self.use_progression_info: self.text_embedding_size = 32 self.simple_encoder = nn.Sequential( nn.Linear(obs_space["progress_info"], 64), nn.Tanh(), nn.Linear(64, self.text_embedding_size), nn.Tanh() ).to(self.device) print("Linear encoder Number of parameters:", sum(p.numel() for p in self.simple_encoder.parameters() if p.requires_grad)) elif self.use_text: self.word_embedding_size = 32 self.text_embedding_size = args.gnn_out if self.gnn_type == "GRU": self.text_rnn = GRUModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to(self.device) else: assert(self.gnn_type == "LSTM") self.text_rnn = LSTMModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to(self.device) print("RNN Number of parameters:", sum(p.numel() for p in self.text_rnn.parameters() if p.requires_grad)) elif self.use_ast: hidden_dim = 32 self.text_embedding_size = 32 self.gnn = GNNMaker(self.gnn_type, obs_space["text"], self.text_embedding_size).to(self.device) print("GNN Number of parameters:", sum(p.numel() for p in self.gnn.parameters() if p.requires_grad)) elif self.use_trans: self.word_embedding_size = 512 self.text_embedding_size = args.d_out self.ltl2transformer = TransfomerSyn(obs_space["text"], self.word_embedding_size, self.text_embedding_size, 'mean' , args) print("Transformer Number of parameters:", sum(p.numel() for p in self.ltl2transformer.parameters() if p.requires_grad)) # Resize image embedding self.embedding_size = self.env_model.size() # 64 print("embedding size:", self.embedding_size) if self.use_text or self.use_ast or self.use_progression_info or self.use_trans: self.embedding_size += self.text_embedding_size # 96 if self.dumb_ac: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 1) ) else: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space, hiddens=[64, 64, 64], activation=nn.ReLU()) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 64), nn.Tanh(), nn.Linear(64, 64), nn.Tanh(), nn.Linear(64, 1) ) # Initialize parameters correctly self.apply(init_params) if self.use_trans and args.TFixup: self.ltl2transformer.init_by_TFixup(args) def forward(self, obs): embedding = self.env_model(obs) # shape = torch.Size([16, 64]) if self.use_progression_info: embed_ltl = self.simple_encoder(obs.progress_info) embedding = torch.cat((embedding, embed_ltl), dim=1) if embedding is not None else embed_ltl # Adding Text elif self.use_text: embed_text = self.text_rnn(obs.text) embedding = torch.cat((embedding, embed_text), dim=1) if embedding is not None else embed_text # Adding GNN elif self.use_ast: embed_gnn = self.gnn(obs.text) # shape = torch.Size([16, 32]) embedding = torch.cat((embedding, embed_gnn), dim=1) if embedding is not None else embed_gnn # shape = torch.Size([16, 96]) elif self.use_trans: embed_transformer = self.ltl2transformer(obs.text) embedding = torch.cat((embedding, embed_transformer), dim=1) if embedding is not None else embed_transformer # Actor dist = self.actor(embedding) # Critic x = self.critic(embedding) value = x.squeeze(1) return dist, value def load_pretrained_gnn(self, model_state): # We delete all keys relating to the actor/critic. new_model_state = model_state.copy() for key in model_state.keys(): if key.find("actor") != -1 or key.find("critic") != -1: # ??? key.find()? del new_model_state[key] self.load_state_dict(new_model_state, strict=False) if self.freeze_pretrained_params: target = self.text_rnn if self.gnn_type == "GRU" or self.gnn_type == "LSTM" else self.gnn for param in target.parameters(): param.requires_grad = False class TransfomerSyn(nn.Module): def __init__(self, obs_size, d_model, d_out, pool, args): super(TransfomerSyn, self).__init__() self.embedded = nn.Embedding(obs_size, args.d_model) self.transformer = TransformerEncoderModel(d_model=args.d_model, nhead=args.nhead, num_encoder_layers=args.num_encoder_layers, pool=args.pool, dim_feedforward=args.dim_feedforward, dropout=args.dropout, d_out=args.d_out, layer_norm_eps=args.layer_norm_eps) def forward(self, text): embed_text = self.embedded(text) feature = self.transformer(embed_text) return feature def init_by_TFixup(self, args): # todo:debug # for k, v in self.transformer.named_parameters(): # print(k, v, v.shape) for p in self.embedded.parameters(): if p.dim() > 1: torch.nn.init.normal_(p, 0, args.d_model ** (- 1. / 2.)) temp_state_dic = {} for name, param in self.embedded.named_parameters(): if 'weight' in name: temp_state_dic[name] = ((9 * args.num_encoder_layers) ** (- 1. / 4.)) * param for name in self.embedded.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.embedded.state_dict()[name] self.embedded.load_state_dict(temp_state_dic) temp_state_dic = {} for name, param in self.transformer.named_parameters(): if any(s in name for s in ["linear1.weight", "linear2.weight", "self_attn.out_proj.weight"]): temp_state_dic[name] = (0.67 * (args.num_encoder_layers) ** (- 1. / 4.)) * param elif "self_attn.in_proj_weight" in name: temp_state_dic[name] = (0.67 * (args.num_encoder_layers) ** (- 1. / 4.)) * (param * (2 ** 0.5)) for name in self.transformer.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.transformer.state_dict()[name] self.transformer.load_state_dict(temp_state_dic) class LSTMModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() # For all our experiments we want the embedding to be a fixed size so we can "transfer". self.word_embedding = nn.Embedding(obs_size, word_embedding_size) self.lstm = nn.LSTM(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2*hidden_dim, text_embedding_size) def forward(self, text): hidden, _ = self.lstm(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class GRUModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() self.word_embedding = nn.Embedding(obs_size, word_embedding_size) # word_embedding_size = 32, hidden_dim = 16 self.gru = nn.GRU(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2*hidden_dim, text_embedding_size) def forward(self, text): # hidden_shape: [16, 9, 32] _shape: [4, 16, 16] hidden, _ = self.gru(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class TransformerEncoderModel(nn.Module): def __init__(self, d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 1, pool: str = 'mean', dim_feedforward: int = 2048, dropout: float = 0.1, d_out: int = 8, activation = F.relu, layer_norm_eps: float = 1e-5, batch_first: bool = True, norm_first: bool = False): """ :param d_model: the number of expected features in the encoder/decoder inputs (default=512). :param nhead: the number of heads in the multiheadattention models (default=8). :param num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6). :param dim_feedforward: the dimension of the feedforward network model (default=2048). :param dropout: the dropout value (default=0.1). :param activation: the activation function of encoder/decoder intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu :param layer_norm_eps: the eps value in layer normalization components (default=1e-5). :param batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False`` (seq, batch, feature). :param norm_first: if ``True``, encoder and decoder layers will perform LayerNorms before other attention and feedforward operations, otherwise after. Default: ``False`` Examples:: >>> transformer_model = nn.Transformer(nhead=16, num_encoder_layers=12) >>> src = torch.rand((10, 32, 512)) >>> out = transformer_model(src) """ super(TransformerEncoderModel, self).__init__() self.d_model = d_model self.nhead = nhead self.batch_first = batch_first encoder_layer = TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first) encoder_norm = nn.LayerNorm(d_model, eps=layer_norm_eps) self.encoder = TransformerEncoder(encoder_layer, num_encoder_layers, encoder_norm) self.pool = pool self.to_latent = nn.Identity() self.mlp_head = nn.Sequential( nn.LayerNorm(d_model), nn.Linear(d_model, d_out) ) self._reset_parameters() def forward(self, src, src_mask=None, src_key_padding_mask=None): """ Args: src: the sequence to the encoder (required). src_mask: the additive mask for the src sequence (optional). src_key_padding_mask: the ByteTensor mask for src keys per batch (optional). Shape: - src: :math:`(S, N, E)`, `(N, S, E)` if batch_first. - src_mask: :math:`(S, S)`. - src_key_padding_mask: :math:`(N, S)`. where S is the source sequence length, T is the target sequence length, N is the batch size, E is the feature number """ memory = self.encoder(src, mask=src_mask, src_key_padding_mask=src_key_padding_mask) memory = memory.mean(dim=1) if self.pool == 'mean' else memory[:, -1, :] memory = self.to_latent(memory) memory = torch.tanh(self.mlp_head(memory)) return memory def _reset_parameters(self): r"""Initiate parameters in the transformer model.""" for p in self.parameters(): if p.dim() > 1: torch.nn.init.xavier_uniform_(p) class TransformerEncoder(nn.Module): def __init__(self, encoder_layer, num_layers, norm=None): r"""TransformerEncoder is a stack of N encoder layers Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) """ super(TransformerEncoder, self).__init__() self.layers = _get_clones(encoder_layer, num_layers) self.num_layers = num_layers self.norm = norm def forward(self, src, mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layers in turn. Args: src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ output = src for mod in self.layers: output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask) if self.norm is not None: output = self.norm(output) return output class TransformerEncoderLayer(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=F.relu, layer_norm_eps=1e-5, batch_first=False, norm_first=False): """ Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). activation: the activation function of the intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu layer_norm_eps: the eps value in layer normalization components (default=1e-5). batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False``. norm_first: if ``True``, layer norm is done prior to attention and feedforward operations, respectivaly. Otherwise it's done after. Default: ``False`` (after). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8, batch_first=True) >>> src = torch.rand(32, 10, 512) >>> out = encoder_layer(src) """ super(TransformerEncoderLayer, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=batch_first) # Implementation of Feedforward model self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm_first = norm_first self.norm1 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.norm2 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = activation def forward(self, src, src_mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ x = src if self.norm_first: x = x + self._sa_block(self.norm1(x), src_mask, src_key_padding_mask) x = x + self._ff_block(self.norm2(x)) else: x = self.norm1(x + self._sa_block(x, src_mask, src_key_padding_mask)) x = self.norm2(x + self._ff_block(x)) return x # self-attention block def _sa_block(self, x, attn_mask, key_padding_mask): x = self.self_attn(x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=False)[0] return self.dropout1(x) # feed forward block def _ff_block(self, x): x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x) def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
22,185
42.247563
136
py
T2TL
T2TL-main/src/train_PreGNNAgent.py
import argparse import time import datetime import torch import torch_ac import tensorboardX import sys import glob from math import floor import utils from model import ACModel from recurrent_model import RecurrentACModel if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser() ## General parameters parser.add_argument("--algo", default='ppo', help="algorithm to use: a2c | ppo (REQUIRED)") parser.add_argument("--env", default='Zones-25-v1', help="name of the environment to train on (REQUIRED)") parser.add_argument("--ltl-sampler", default="Until_1_2_1_1", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--model", default=None, help="name of the model (default: {ENV}_{SAMPLER}_{ALGO}_{TIME})") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--log-interval", type=int, default=1, help="number of updates between two logs (default: 10)") parser.add_argument("--save-interval", type=int, default=2, help="number of updates between two saves (default: 10, 0 means no saving)") parser.add_argument("--procs", type=int, default=16, help="number of processes (default: 16)") parser.add_argument("--frames", type=int, default=1*10**7, help="number of frames of training (default: 2*10e8)") parser.add_argument("--checkpoint-dir", default=None) ## Evaluation parameters parser.add_argument("--eval", action="store_true", default=False, help="evaluate the saved model (default: False)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--eval-env", default=None, help="name of the environment to train on (default: use the same \"env\" as training)") parser.add_argument("--ltl-samplers-eval", default=None, nargs='+', help="the ltl formula templates to sample from for evaluation (default: use the same \"ltl-sampler\" as training)") parser.add_argument("--eval-procs", type=int, default=1, help="number of processes (default: use the same \"procs\" as training)") ## Parameters for main algorithm parser.add_argument("--epochs", type=int, default=10, help="number of epochs for PPO (default: 4)") parser.add_argument("--batch-size", type=int, default=1024, help="batch size for PPO (default: 256)") parser.add_argument("--frames-per-proc", type=int, default=4096, help="number of frames per process before update (default: 5 for A2C and 128 for PPO)") parser.add_argument("--discount", type=float, default=0.998, help="discount factor (default: 0.99)") parser.add_argument("--lr", type=float, default=0.0003, help="learning rate (default: 0.0003)") parser.add_argument("--gae-lambda", type=float, default=0.95, help="lambda coefficient in GAE formula (default: 0.95, 1 means no gae)") parser.add_argument("--entropy-coef", type=float, default=0.003, help="entropy term coefficient (default: 0.01)") parser.add_argument("--value-loss-coef", type=float, default=0.5, help="value loss term coefficient (default: 0.5)") parser.add_argument("--max-grad-norm", type=float, default=0.5, help="maximum norm of gradient (default: 0.5)") parser.add_argument("--optim-eps", type=float, default=1e-8, help="Adam and RMSprop optimizer epsilon (default: 1e-8)") parser.add_argument("--optim-alpha", type=float, default=0.99, help="RMSprop optimizer alpha (default: 0.99)") parser.add_argument("--clip-eps", type=float, default=0.2, help="clipping epsilon for PPO (default: 0.2)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--noLTL", action="store_true", default=False, help="the environment no longer has an LTL goal. --ignoreLTL must be specified concurrently.") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="RGCN_8x32_ROOT_SHARED", help="use gnn to model the LTL (only if ignoreLTL==True)") parser.add_argument("--trans_layer", type=int, default=1, help="the number of Transformer layers need to use") parser.add_argument("--int-reward", type=float, default=0.0, help="the intrinsic reward for LTL progression (default: 0.0)") parser.add_argument("--pretrained-gnn", action="store_true", default=True, help="load a pre-trained LTL module.") parser.add_argument("--dumb-ac", action="store_true", default=False, help="Use a single-layer actor-critic") parser.add_argument("--freeze-ltl", action="store_true", default=False, help="Freeze the gradient updates of the LTL module") # Transformer special parameters parser.add_argument("--d_model", type=int, default=64, help="") parser.add_argument("--nhead", type=int, default=8, help="") parser.add_argument("--num_encoder_layers", type=int, default=2, help="") parser.add_argument("--pool", type=str, default='mean', help="") parser.add_argument("--dim_feedforward", type=int, default=256, help="") parser.add_argument("--dropout", type=float, default=0.0, help="") parser.add_argument("--d_out", type=int, default=16, help="") parser.add_argument("--layer_norm_eps", type=float, default=1e-5, help="") parser.add_argument("--cuda", type=str, default='cuda:0', help="") # additional desciption for test parser.add_argument("--sth", type=str, default='None', help="") args = parser.parse_args() use_mem = args.recurrence > 1 # whether use memory or not # Set run dir date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") # '21-08-21-22-36-39' gnn_name = args.gnn if args.ignoreLTL: gnn_name = "IgnoreLTL" if args.dumb_ac: gnn_name = gnn_name + "-dumb_ac" if args.pretrained_gnn: gnn_name = gnn_name + "-pretrained" if args.freeze_ltl: gnn_name = gnn_name + "-freeze_ltl" if use_mem: gnn_name = gnn_name + "-recurrence:%d"%(args.recurrence) if args.gnn == 'Transformer': default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_dim_feedforward:{args.dim_feedforward}_dropout:{args.dropout}_sth:{args.sth}" else: # 'RGCN_8x32_ROOT_SHARED_Until_1_2_1_1_Zones-5-v0_seed:1_epochs:10_bs:2048_fpp:4096_dsc:0.998_lr:0.0003_ent:0.003_clip:0.2_prog:full' default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_epochs:{args.epochs}_bs:{args.batch_size}_fpp:{args.frames_per_proc}_dsc:{args.discount}_lr:{args.lr}_ent:{args.entropy_coef}_clip:{args.clip_eps}_prog:{args.progression_mode}" model_name = args.model or default_model_name storage_dir = "storage" if args.checkpoint_dir is None else args.checkpoint_dir model_dir = utils.get_model_dir(model_name, storage_dir) pretrained_model_dir = None if args.pretrained_gnn: assert(args.progression_mode == "full") default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}_*/train" # default_dir = f"../src/pretrain/{args.gnn}-dumb_ac_*_Simple-LTL-Env-v0_seed:{args.seed}_*_d_out:{args.d_out}/train" print(default_dir) model_dirs = glob.glob(default_dir) if len(model_dirs) == 0: raise Exception("Pretraining directory not found.") elif len(model_dirs) > 1: raise Exception("More than 1 candidate pretraining directory found.") pretrained_model_dir = model_dirs[0] # Load loggers and Tensorboard writer txt_logger = utils.get_txt_logger(model_dir + "/train") csv_file, csv_logger = utils.get_csv_logger(model_dir + "/train") tb_writer = tensorboardX.SummaryWriter(model_dir + "/train") utils.save_config(model_dir + "/train", args) # Log command and all script arguments txt_logger.info("{}\n".format(" ".join(sys.argv))) txt_logger.info("{}\n".format(args)) # It will output the context of Namespace # Set seed for all randomness sources utils.seed(args.seed) # Set device device = torch.device(args.cuda if torch.cuda.is_available() else "cpu") # device = torch.device('cpu') txt_logger.info(f"Device: {device}\n") # Output the device (default is cpu) # Load environments envs = [] progression_mode = args.progression_mode for i in range(args.procs): # load the env & progression_mode & LTL formula # turn to utils/env.py envs.append(utils.make_env(args.env, progression_mode, args.ltl_sampler, args.seed, args.int_reward, args.noLTL)) # Sync environments envs[0].reset() # Add the agent to map & translate the LTL formula txt_logger.info("Environments loaded\n") # Load training status try: status = utils.get_status(model_dir + "/train", args) except OSError: status = {"num_frames": 0, "update": 0} # ??? the state of algorithm ? txt_logger.info("Training status loaded.\n") if pretrained_model_dir is not None: try: pretrained_status = utils.get_status(pretrained_model_dir, args) except: txt_logger.info("Failed to load pretrained model.\n") exit(1) # Load observations preprocessor-- build AST using_gnn = (args.gnn != "GRU" and args.gnn != "LSTM" and args.gnn != "Transformer") # turn to env/format.py obs_space, preprocess_obss = utils.get_obss_preprocessor(envs[0], using_gnn, progression_mode) if "vocab" in status and preprocess_obss.vocab is not None: preprocess_obss.vocab.load_vocab(status["vocab"]) txt_logger.info("Observations preprocessor loaded.\n") # Load model if use_mem: acmodel = RecurrentACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl) else: acmodel = ACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl, args) if "model_state" in status: acmodel.load_state_dict(status["model_state"]) txt_logger.info("Loading model from existing run.\n") elif args.pretrained_gnn: acmodel.load_pretrained_gnn(pretrained_status["model_state"]) txt_logger.info("Pretrained model loaded.\n") acmodel.to(device) txt_logger.info("Model loaded.\n") txt_logger.info("{}\n".format(acmodel)) # Load algo if args.algo == "a2c": algo = torch_ac.A2CAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_alpha, args.optim_eps, preprocess_obss) elif args.algo == "ppo": algo = torch_ac.PPOAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_eps, args.clip_eps, args.epochs, args.batch_size, preprocess_obss) else: raise ValueError("Incorrect algorithm name: {}".format(args.algo)) if "optimizer_state" in status: algo.optimizer.load_state_dict(status["optimizer_state"]) txt_logger.info("Loading optimizer from existing run.\n") txt_logger.info("Optimizer loaded.\n") # init the evaluator if args.eval: eval_samplers = args.ltl_samplers_eval if args.ltl_samplers_eval else [args.ltl_sampler] eval_env = args.eval_env if args.eval_env else args.env eval_procs = args.eval_procs if args.eval_procs else args.procs evals = [] for eval_sampler in eval_samplers: evals.append(utils.Eval(eval_env, model_name, eval_sampler, seed=args.seed, device=device, num_procs=eval_procs, ignoreLTL=args.ignoreLTL, progression_mode=progression_mode, gnn=args.gnn, dumb_ac = args.dumb_ac)) # Train model num_frames = status["num_frames"] # num_frames:0 update = status["update"] # update:0 start_time = time.time() while num_frames < args.frames: # Update model parameters update_start_time = time.time() exps, logs1 = algo.collect_experiences() # interacte with environmets (very important) logs2 = algo.update_parameters(exps) logs = {**logs1, **logs2} update_end_time = time.time() num_frames += logs["num_frames"] update += 1 # Print logs if update % args.log_interval == 0: fps = logs["num_frames"]/(update_end_time - update_start_time) duration = int(time.time() - start_time) return_per_episode = utils.synthesize(logs["return_per_episode"]) rreturn_per_episode = utils.synthesize(logs["reshaped_return_per_episode"]) average_reward_per_step = utils.average_reward_per_step(logs["return_per_episode"], logs["num_frames_per_episode"]) average_discounted_return = utils.average_discounted_return(logs["return_per_episode"], logs["num_frames_per_episode"], args.discount) num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"]) header = ["update", "frames", "FPS", "duration"] data = [update, num_frames, fps, duration] header += ["rreturn_" + key for key in rreturn_per_episode.keys()] data += rreturn_per_episode.values() header += ["average_reward_per_step", "average_discounted_return"] data += [average_reward_per_step, average_discounted_return] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["entropy", "value", "policy_loss", "value_loss", "grad_norm"] data += [logs["entropy"], logs["value"], logs["policy_loss"], logs["value_loss"], logs["grad_norm"]] txt_logger.info( "U {} | F {:06} | FPS {:04.0f} | D {} | rR:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | ARPS: {:.3f} | ADR: {:.3f} | F:μσmM {:.1f} {:.1f} {} {} | H {:.3f} | V {:.3f} | pL {:.3f} | vL {:.3f} | ∇ {:.3f}" .format(*data)) header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() if status["num_frames"] == 0: csv_logger.writerow(header) csv_logger.writerow(data) csv_file.flush() for field, value in zip(header, data): tb_writer.add_scalar(field, value, num_frames) # Save status if args.save_interval > 0 and update % args.save_interval == 0: status = {"num_frames": num_frames, "update": update, "model_state": algo.acmodel.state_dict(), "optimizer_state": algo.optimizer.state_dict()} if hasattr(preprocess_obss, "vocab") and preprocess_obss.vocab is not None: status["vocab"] = preprocess_obss.vocab.vocab utils.save_status(status, model_dir + "/train") txt_logger.info("Status saved") if args.eval: # we send the num_frames to align the eval curves with the training curves on TB for evalu in evals: evalu.eval(num_frames, episodes=args.eval_episodes)
16,595
49.443769
265
py
T2TL
T2TL-main/src/transEncoder.py
import torch import torch.nn as nn import torch.nn.functional as F import copy class ContextTransformer(nn.Module): def __init__(self, obs_size, obsr_dim, d_model, d_out, pool, args, context=False): super(ContextTransformer, self).__init__() self.context = context self.obsr_dim = obsr_dim self.action_dim = 2 # action dim self.device = torch.device(args.device) if self.context: self.embed_linear = nn.Linear(obs_size, args.cont_d_model//2) self.transformer = TransformerEncoderModel(d_model=args.cont_d_model//2, nhead=args.cont_nhead, num_encoder_layers=args.cont_num_encoder_layers//2, pool=args.cont_pool, dim_feedforward=args.cont_dim_feedforward//2, dropout=args.dropout, d_out=args.cont_d_out, layer_norm_eps=args.layer_norm_eps) else: self.embedded = nn.Embedding(obs_size, args.d_model) self.transformer = TransformerEncoderModel(d_model=args.d_model, nhead=args.nhead, num_encoder_layers=args.num_encoder_layers, pool=args.pool, dim_feedforward=args.dim_feedforward, dropout=args.dropout, d_out=args.d_out, layer_norm_eps=args.layer_norm_eps) def forward(self, data): previous_action, previous_reward, pre_x = data[0], data[1], data[2] bsize, dim = previous_action.shape # previous_action is B* (history_len * D) pacts = previous_action.view(bsize, -1, self.action_dim) # view(bsize, self.hist_length, -1) prews = previous_reward.view(bsize, -1, 1) # reward dim is 1, view(bsize, self.hist_length, 1) pxs = pre_x.view(bsize, -1, self.obsr_dim) # view(bsize, self.hist_length, -1) pre_act_rew = torch.cat([pacts, prews, pxs], dim=-1).to(self.device) if self.context: embed_text = self.embed_linear(pre_act_rew) else: embed_text = self.embedded(data) feature = self.transformer(embed_text) return feature def init_by_TFixup(self, args): for p in self.embed_linear.parameters(): if p.dim() > 1: torch.nn.init.normal_(p, 0, args.cont_d_model ** (- 1. / 2.)) temp_state_dic = {} for name, param in self.embed_linear.named_parameters(): if 'weight' in name: temp_state_dic[name] = ((9 * args.cont_num_encoder_layers) ** (- 1. / 4.)) * param for name in self.embed_linear.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.embed_linear.state_dict()[name] self.embed_linear.load_state_dict(temp_state_dic) temp_state_dic = {} for name, param in self.transformer.named_parameters(): if any(s in name for s in ["linear1.weight", "linear2.weight", "self_attn.out_proj.weight"]): temp_state_dic[name] = (0.67 * (args.cont_num_encoder_layers) ** (- 1. / 4.)) * param elif "self_attn.in_proj_weight" in name: temp_state_dic[name] = (0.67 * (args.cont_num_encoder_layers) ** (- 1. / 4.)) * (param * (2 ** 0.5)) for name in self.transformer.state_dict(): if name not in temp_state_dic: temp_state_dic[name] = self.transformer.state_dict()[name] self.transformer.load_state_dict(temp_state_dic) class LSTMModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() # For all our experiments we want the embedding to be a fixed size so we can "transfer". self.word_embedding = nn.Embedding(obs_size, word_embedding_size) self.lstm = nn.LSTM(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2 * hidden_dim, text_embedding_size) def forward(self, text): hidden, _ = self.lstm(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class GRUModel(nn.Module): def __init__(self, obs_size, word_embedding_size=32, hidden_dim=32, text_embedding_size=32): super().__init__() self.word_embedding = nn.Embedding(obs_size, word_embedding_size) # word_embedding_size = 32, hidden_dim = 16 self.gru = nn.GRU(word_embedding_size, hidden_dim, num_layers=2, batch_first=True, bidirectional=True) self.output_layer = nn.Linear(2 * hidden_dim, text_embedding_size) def forward(self, text): # hidden_shape: [16, 9, 32] _shape: [4, 16, 16] hidden, _ = self.gru(self.word_embedding(text)) return self.output_layer(hidden[:, -1, :]) class TransformerEncoderModel(nn.Module): def __init__(self, d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 1, pool: str = 'mean', dim_feedforward: int = 2048, dropout: float = 0.1, d_out: int = 8, activation=F.relu, layer_norm_eps: float = 1e-5, batch_first: bool = True, norm_first: bool = False): """ :param d_model: the number of expected features in the encoder/decoder inputs (default=512). :param nhead: the number of heads in the multiheadattention models (default=8). :param num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6). :param dim_feedforward: the dimension of the feedforward network model (default=2048). :param dropout: the dropout value (default=0.1). :param activation: the activation function of encoder/decoder intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu :param layer_norm_eps: the eps value in layer normalization components (default=1e-5). :param batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False`` (seq, batch, feature). :param norm_first: if ``True``, encoder and decoder layers will perform LayerNorms before other attention and feedforward operations, otherwise after. Default: ``False`` Examples:: >>> transformer_model = nn.Transformer(nhead=16, num_encoder_layers=12) >>> src = torch.rand((10, 32, 512)) >>> out = transformer_model(src) """ super(TransformerEncoderModel, self).__init__() self.d_model = d_model self.nhead = nhead self.batch_first = batch_first encoder_layer = TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first) encoder_norm = nn.LayerNorm(d_model, eps=layer_norm_eps) self.encoder = TransformerEncoder(encoder_layer, num_encoder_layers, encoder_norm) self.pool = pool self.to_latent = nn.Identity() self.mlp_head = nn.Sequential( nn.LayerNorm(d_model), nn.Linear(d_model, d_out) ) self._reset_parameters() def forward(self, src, src_mask=None, src_key_padding_mask=None): """ Args: src: the sequence to the encoder (required). src_mask: the additive mask for the src sequence (optional). src_key_padding_mask: the ByteTensor mask for src keys per batch (optional). Shape: - src: :math:`(S, N, E)`, `(N, S, E)` if batch_first. - src_mask: :math:`(S, S)`. - src_key_padding_mask: :math:`(N, S)`. where S is the source sequence length, T is the target sequence length, N is the batch size, E is the feature number """ memory = self.encoder(src, mask=src_mask, src_key_padding_mask=src_key_padding_mask) memory = memory.mean(dim=1) if self.pool == 'mean' else memory[:, -1, :] memory = self.to_latent(memory) memory = torch.tanh(self.mlp_head(memory)) return memory def _reset_parameters(self): r"""Initiate parameters in the transformer model.""" for p in self.parameters(): if p.dim() > 1: torch.nn.init.xavier_uniform_(p) class TransformerEncoder(nn.Module): def __init__(self, encoder_layer, num_layers, norm=None): r"""TransformerEncoder is a stack of N encoder layers Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) """ super(TransformerEncoder, self).__init__() self.layers = _get_clones(encoder_layer, num_layers) self.num_layers = num_layers self.norm = norm def forward(self, src, mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layers in turn. Args: src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ output = src for mod in self.layers: output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask) if self.norm is not None: output = self.norm(output) return output class TransformerEncoderLayer(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=F.relu, layer_norm_eps=1e-5, batch_first=True, norm_first=False): """ Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). activation: the activation function of the intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu layer_norm_eps: the eps value in layer normalization components (default=1e-5). batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False``. norm_first: if ``True``, layer norm is done prior to attention and feedforward operations, respectivaly. Otherwise it's done after. Default: ``False`` (after). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8, batch_first=True) >>> src = torch.rand(32, 10, 512) >>> out = encoder_layer(src) """ super(TransformerEncoderLayer, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=batch_first) # Implementation of Feedforward model self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm_first = norm_first self.norm1 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.norm2 = nn.LayerNorm(d_model, eps=layer_norm_eps) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = activation def forward(self, src, src_mask=None, src_key_padding_mask=None): r"""Pass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ x = src if self.norm_first: x = x + self._sa_block(self.norm1(x), src_mask, src_key_padding_mask) x = x + self._ff_block(self.norm2(x)) else: x = self.norm1(x + self._sa_block(x, src_mask, src_key_padding_mask)) x = self.norm2(x + self._ff_block(x)) return x # self-attention block def _sa_block(self, x, attn_mask, key_padding_mask): x = self.self_attn(x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=False)[0] return self.dropout1(x) # feed forward block def _ff_block(self, x): x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x) def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)]) class Context(nn.Module): """ This layer just does non-linear transformation(s) """ def __init__(self, hidden_sizes=[50], input_dim=None, history_length=1, action_dim=None, obsr_dim=None, device='cpu' ): super(Context, self).__init__() self.hidden_sizes = hidden_sizes self.input_dim = input_dim self.hist_length = history_length self.device = device self.action_dim = action_dim self.obsr_dim = obsr_dim self.recurrent = nn.GRU(self.input_dim, self.hidden_sizes[0], bidirectional=False, batch_first=True, num_layers=1) def init_recurrent(self, bsize=None): ''' init hidden states Batch size can't be none ''' # The order is (num_layers, minibatch_size, hidden_dim) # LSTM ==> return (torch.zeros(1, bsize, self.hidden_sizes[0]), # torch.zeros(1, bsize, self.hidden_sizes[0])) return torch.zeros(1, bsize, self.hidden_sizes[0]).to(self.device) def forward(self, data): ''' pre_x : B * D where B is batch size and D is input_dim pre_a : B * A where B is batch size and A is input_dim previous_reward: B * 1 where B is batch size and 1 is input_dim ''' previous_action, previous_reward, pre_x = data[0], data[1], data[2] # first prepare data for LSTM bsize, dim = previous_action.shape # previous_action is B* (history_len * D) pacts = previous_action.view(bsize, -1, self.action_dim) # view(bsize, self.hist_length, -1) prews = previous_reward.view(bsize, -1, 1) # reward dim is 1, view(bsize, self.hist_length, 1) pxs = pre_x.view(bsize, -1, self.obsr_dim) # view(bsize, self.hist_length, -1) pre_act_rew = torch.cat([pacts, prews, pxs], dim=-1) # input to LSTM is [action, reward] # init lstm/gru hidden = self.init_recurrent(bsize=bsize) # lstm/gru _, hidden = self.recurrent(pre_act_rew, hidden) # hidden is (1, B, hidden_size) out = hidden.squeeze(0) # (1, B, hidden_size) ==> (B, hidden_size) return out
15,896
43.90678
121
py
T2TL
T2TL-main/src/test_safety.py
import argparse import time import sys import numpy as np import glfw import utils import torch import gym import safety_gym import ltl_wrappers import ltl_progression from gym import wrappers, logger from envs.safety import safety_wrappers class RandomAgent(object): """This agent picks actions randomly""" def __init__(self, action_space): self.action_space = action_space def get_action(self, obs): return self.action_space.sample() class PlayAgent(object): """ This agent allows user to play with Safety's Point agent. Use the UP and DOWN arrows to move forward and back and use '<' and '>' to rotate the agent. """ def __init__(self, env): self.env = env self.action_space = env.action_space self.prev_act = np.array([0, 0]) self.last_obs = None def get_action(self, obs): # obs = obs["features"] key = self.env.key_pressed if(key == glfw.KEY_COMMA): current = np.array([0, 0.4]) elif(key == glfw.KEY_PERIOD): current = np.array([0, -0.4]) elif(key == glfw.KEY_UP): current = np.array([0.1, 0]) elif(key == glfw.KEY_DOWN): current = np.array([-0.1, 0]) elif(key == -1): # This is glfw.RELEASE current = np.array([0, 0]) self.prev_act = np.array([0, 0]) else: current = np.array([0, 0]) self.prev_act = np.clip(self.prev_act + current, -1, 1) return self.prev_act def run_policy(agent, env, max_ep_len=None, num_episodes=100, render=True): env = wrappers.Monitor(env, directory=outdir, force=True) env.seed(1) ######### o, r, d, ep_ret, ep_cost, ep_len, n = env.reset(), 0, False, 0, 0, 0, 0 while n < num_episodes: if render: env.render() time.sleep(1e-3) ltl_goal = ltl_progression.spotify(env.ltl_goal) env.show_text(ltl_goal.to_str()) if("progress_info" in o.keys()): env.show_prog_info(o["progress_info"]) a = agent.get_action(o) a = np.clip(a, env.action_space.low, env.action_space.high) o, r, d, info = env.step(a) ep_ret += r ep_cost += info.get('cost', 0) ep_len += 1 if d or (ep_len == max_ep_len): o, r, d, ep_ret, ep_cost, ep_len = env.reset(), 0, False, 0, 0, 0 n += 1 if __name__ == '__main__': parser = argparse.ArgumentParser(description=None) subparsers = parser.add_subparsers(dest='command') parser.add_argument('env_id', default='SafexpTest-v0', help='Select the environment to run') parser_play = subparsers.add_parser('play', help='A playable agent that can be controlled.') parser_random = subparsers.add_parser('random', help='An agent that picks actions at random (for testing).') parser_viz = subparsers.add_parser('viz', help='Load the agent model from a file and visualize its action on the env.') parser_viz.add_argument('model_path', type=str, help='The path to the model to load.') parser_viz.add_argument("--ltl-sampler", default="Default", help="the ltl formula template to sample from (default: DefaultSampler)") args = vars(parser.parse_args()) # make it a dictionary outdir = './storage/random-agent-results' if (args["command"] == "play"): env = gym.make(args["env_id"]) env.num_steps = 10000000 env = safety_wrappers.Play(env) env = ltl_wrappers.LTLEnv(env, ltl_sampler="Default") agent = PlayAgent(env) elif (args["command"] == "random"): env = gym.make(args["env_id"]) env.num_steps = 10000 env = safety_wrappers.Play(env) env = ltl_wrappers.LTLEnv(env, ltl_sampler="Default") agent = RandomAgent(env.action_space) elif (args["command"] == "viz"): # If the config is available (from trainig) then just load it here instead of asking the user of this script to provide all training time configs config = vars(utils.load_config(args["model_path"])) args.update(config) env = gym.make(args["env_id"]) env = safety_wrappers.Play(env) env = ltl_wrappers.LTLEnv(env, ltl_sampler=args["ltl_sampler"], progression_mode=args["progression_mode"]) # device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cpu") agent = utils.Agent(env, env.observation_space, env.action_space, args["model_path"], args["ignoreLTL"], args["progression_mode"], args["gnn"], device=device, dumb_ac=args["dumb_ac"]) else: print("Incorrect command: ", args["command"]) exit(1) run_policy(agent, env, max_ep_len=30000, num_episodes=1000)
4,800
33.292857
153
py
T2TL
T2TL-main/src/manual_control.py
#!/usr/bin/env python3 import time import argparse import numpy as np import gym import gym_minigrid import ltl_wrappers from gym_minigrid.wrappers import * from gym_minigrid.window import Window from envs.minigrid.adversarial import * def redraw(img): if not args.agent_view: img = base_env.render(mode='rgb_array', tile_size=args.tile_size) window.show_img(img) def reset(): if args.seed != -1: ltl_env.seed(args.seed) obs = ltl_env.reset() window.set_caption(ltl_env.ltl_goal) redraw(obs) def step(action): obs, reward, done, info = ltl_env.step(action) window.set_caption(ltl_env.ltl_goal) print('step=%s, reward=%.2f' % (base_env.step_count, reward)) if done: print('done!') reset() else: redraw(obs) def key_handler(event): print('pressed', event.key) if event.key == 'escape': window.close() return if event.key == 'backspace': reset() return if event.key == 'left': step(base_env.actions.left) return if event.key == 'right': step(base_env.actions.right) return if event.key == 'up': step(base_env.actions.forward) return # Spacebar if event.key == ' ': step(base_env.actions.toggle) return if event.key == 'pageup': step(base_env.actions.pickup) return if event.key == 'pagedown': step(base_env.actions.drop) return if event.key == 'enter': step(base_env.actions.done) return parser = argparse.ArgumentParser() parser.add_argument( "--env", help="gym environment to load", default='MiniGrid-MultiRoom-N6-v0' ) parser.add_argument( "--seed", type=int, help="random seed to generate the environment with", default=-1 ) parser.add_argument( "--tile_size", type=int, help="size at which to render tiles", default=32 ) parser.add_argument( '--agent_view', default=False, help="draw the agent sees (partially observable view)", action='store_true' ) args = parser.parse_args() # `base_env` is the backend minigrid # `env` is the (1-level) wrapped minigrid from our code # `ltl_env` is the (2-level) wrapped minigrid with LTL goals env = gym.make(args.env) base_env = env.env ltl_env = ltl_wrappers.LTLEnv(env, progression_mode="full", ltl_sampler="AdversarialSampler") window = Window('gym_minigrid - ' + args.env) window.reg_key_handler(key_handler) reset() # Blocking event loop window.show(block=True)
2,563
20.546218
93
py
T2TL
T2TL-main/src/run_openai.py
""" This code uses the OpenAI baselines to learn the policies. However, the current implementation ignores the LTL formula. I left this code here as a reference and for debugging purposes. """ try: from mpi4py import MPI except ImportError: MPI = None import numpy as np import tensorflow as tf import gym, multiprocessing, sys, os, argparse from baselines import deepq, bench, logger from baselines.ppo2 import ppo2 from baselines.common.vec_env.subproc_vec_env import SubprocVecEnv from baselines.common.vec_env.dummy_vec_env import DummyVecEnv from baselines.common.models import get_network_builder from baselines.common import set_global_seeds import envs.gym_letters import ltl_wrappers def make_env(env_id, mpi_rank=0, subrank=0, seed=None, logger_dir=None, initializer=None): if initializer is not None: initializer(mpi_rank=mpi_rank, subrank=subrank) env = gym.make(env_id) # Adding general wraps env = ltl_wrappers.LTLEnv(env) env = ltl_wrappers.NoLTLWrapper(env) # For testing purposes env.seed(seed + subrank if seed is not None else None) env = bench.Monitor(env, logger_dir and os.path.join(logger_dir, str(mpi_rank) + '.' + str(subrank)), allow_early_resets=True) return env def make_vec_env(env_id, num_env, seed, start_index=0, initializer=None, force_dummy=False): """ Create a wrapped, monitored SubprocVecEnv for Atari. """ mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else 0 seed = seed + 10000 * mpi_rank if seed is not None else None logger_dir = logger.get_dir() def make_thunk(rank, initializer=None): return lambda: make_env( env_id=env_id, mpi_rank=mpi_rank, subrank=rank, seed=seed, logger_dir=logger_dir, initializer=initializer ) set_global_seeds(seed) if not force_dummy and num_env > 1: return SubprocVecEnv([make_thunk(i + start_index, initializer=initializer) for i in range(num_env)]) else: return DummyVecEnv([make_thunk(i + start_index, initializer=None) for i in range(num_env)]) def build_env(env_id, agent, force_dummy=False, num_env=None, seed=None): ncpu = multiprocessing.cpu_count() if sys.platform == 'darwin': ncpu //= 2 nenv = num_env or ncpu if agent in ['dqn','trpo']: logger_dir = logger.get_dir() env = make_env(env_id, logger_dir=logger_dir) else: env = make_vec_env(env_id, nenv, seed, force_dummy=force_dummy) # NOTE: this is a more efficient way to stack the last 4 frames, but it is not compatible with my memory modules :( # SOURCE: https://github.com/openai/baselines/issues/663 #frame_stack_size = 4 #env = VecFrameStack(env, frame_stack_size) return env def learn_letters(agent, env): if agent == "dqn": model = deepq.learn( env, "mlp", num_layers=4, num_hidden=128, activation=tf.tanh, # tf.nn.relu hiddens=[128], dueling=True, lr=1e-5, total_timesteps=int(1e7), buffer_size=100000, batch_size=32, exploration_fraction=0.1, exploration_final_eps=0.1, #0.01, -> testing... train_freq=1, learning_starts=10000, target_network_update_freq=100, gamma=0.9, print_freq=50 ) elif "ppo" in agent: mlp_net = get_network_builder("mlp")(num_layers=5, num_hidden=128, activation=tf.tanh) # tf.nn.relu ppo_params = dict( nsteps=128, ent_coef=0.01, vf_coef=0.5, max_grad_norm=0.5, lr=1e-4, gamma=0.99, # Note that my results over the red/blue doors were computed using gamma=0.9! lam=0.95, log_interval=50, nminibatches=8, noptepochs=1, #save_interval=100, cliprange=0.2) if "lstm" in agent: # Adding a recurrent layer ppo_params["network"] = 'cnn_lstm' ppo_params["nlstm"] = 128 ppo_params["conv_fn"] = mlp_net ppo_params["lr"] = 0.001 else: # Using a standard MLP ppo_params["network"] = mlp_net timesteps=int(1e9) model = ppo2.learn( env=env, total_timesteps=timesteps, **ppo_params ) else: assert False, agent + " hasn't been implemented yet" return model def run_agent(agent, env_id, run_id): log_path = "results/" + agent.upper() + "/" + env_id + "/" + str(run_id) save_path = log_path + "/trained-model" logger.configure(log_path) # Setting the number of workers num_env = 8 # Creating the memory-based environments env = build_env(env_id, agent, num_env=num_env) # Running the agent model = learn_letters(agent, env) model.save(save_path) env.close() if __name__ == '__main__': agent = 'ppo' env_id = 'Letter-4x4-v0' run_id = 0 run_agent(agent, env_id, run_id)
5,164
30.882716
125
py
T2TL
T2TL-main/src/ltl_samplers.py
""" This class is responsible for sampling LTL formulas typically from given template(s). @ propositions: The set of propositions to be used in the sampled formula at random. """ import random class LTLSampler(): def __init__(self, propositions): self.propositions = propositions def sample(self): raise NotImplementedError # Samples from one of the other samplers at random. The other samplers are sampled by their default args. class SuperSampler(LTLSampler): def __init__(self, propositions): super().__init__(propositions) self.reg_samplers = getRegisteredSamplers(self.propositions) def sample(self): return random.choice(self.reg_samplers).sample() # This class samples formulas of form (or, op_1, op_2), where op_1 and 2 can be either specified as samplers_ids # or by default they will be sampled at random via SuperSampler. class OrSampler(LTLSampler): def __init__(self, propositions, sampler_ids = ["SuperSampler"]*2): super().__init__(propositions) self.sampler_ids = sampler_ids def sample(self): return ('or', getLTLSampler(self.sampler_ids[0], self.propositions).sample(), getLTLSampler(self.sampler_ids[1], self.propositions).sample()) # This class generates random LTL formulas using the following template: # ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) # where p1, p2, p3, and p4 are randomly sampled propositions class DefaultSampler(LTLSampler): def sample(self): p = random.sample(self.propositions,4) return ('until',('not',p[0]),('and', p[1], ('until',('not',p[2]),p[3]))) # This class generates random conjunctions of Until-Tasks. # Each until tasks has *n* levels, where each level consists # of avoiding a proposition until reaching another proposition. # E.g., # Level 1: ('until',('not','a'),'b') # Level 2: ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) # etc... # The number of until-tasks, their levels, and their propositions are randomly sampled. # This code is a generalization of the DefaultSampler---which is equivalent to UntilTaskSampler(propositions, 2, 2, 1, 1) class UntilTaskSampler(LTLSampler): def __init__(self, propositions, min_levels=1, max_levels=2, min_conjunctions=1 , max_conjunctions=2): super().__init__(propositions) self.levels = (int(min_levels), int(max_levels)) self.conjunctions = (int(min_conjunctions), int(max_conjunctions)) assert 2*int(max_levels)*int(max_conjunctions) <= len(propositions), "The domain does not have enough propositions!" def sample(self): # Sampling a conjuntion of *n_conjs* (not p[0]) Until (p[1]) formulas of *n_levels* levels n_conjs = random.randint(*self.conjunctions) p = random.sample(self.propositions,2*self.levels[1]*n_conjs) ltl = None b = 0 for i in range(n_conjs): n_levels = random.randint(*self.levels) # Sampling an until task of *n_levels* levels until_task = ('until',('not',p[b]),p[b+1]) b +=2 for j in range(1,n_levels): until_task = ('until',('not',p[b]),('and', p[b+1], until_task)) b +=2 # Adding the until task to the conjunction of formulas that the agent have to solve if ltl is None: ltl = until_task else: ltl = ('and',until_task,ltl) return ltl # This class generates random LTL formulas that form a sequence of actions. # @ min_len, max_len: min/max length of the random sequence to generate. class SequenceSampler(LTLSampler): def __init__(self, propositions, min_len=2, max_len=4): super().__init__(propositions) self.min_len = int(min_len) self.max_len = int(max_len) def sample(self): length = random.randint(self.min_len, self.max_len) seq = "" while len(seq) < length: c = random.choice(self.propositions) if len(seq) == 0 or seq[-1] != c: seq += c ret = self._get_sequence(seq) return ret def _get_sequence(self, seq): if len(seq) == 1: return ('eventually',seq) return ('eventually',('and', seq[0], self._get_sequence(seq[1:]))) # This generates several sequence tasks which can be accomplished in parallel. # e.g. in (eventually (a and eventually c)) and (eventually b) # the two sequence tasks are "a->c" and "b". class EventuallySampler(LTLSampler): def __init__(self, propositions, min_levels = 1, max_levels=4, min_conjunctions=1, max_conjunctions=3): super().__init__(propositions) # It seems to initialize the self.propositions with propositions automatically assert(len(propositions) >= 3) self.conjunctions = (int(min_conjunctions), int(max_conjunctions)) self.levels = (int(min_levels), int(max_levels)) def sample(self): conjs = random.randint(*self.conjunctions) ltl = None # ('and', ('eventually', ('and', 'i', ('eventually', 'e'))), ('eventually', ('and', 'k', ('eventually', ('and', ('or', 'b', 'l'), ('eventually', ('and', 'h', ('eventually', ('and', 'e', ('eventually', 'f')))))))))) for i in range(conjs): task = self.sample_sequence() if ltl is None: ltl = task else: ltl = ('and',task,ltl) return ltl def sample_sequence(self): length = random.randint(*self.levels) seq = [] last = [] while len(seq) < length: # Randomly replace some propositions with a disjunction to make more complex formulas population = [p for p in self.propositions if p not in last] if random.random() < 0.25: c = random.sample(population, 2) else: c = random.sample(population, 1) seq.append(c) last = c ret = self._get_sequence(seq) # ('eventually', ('and', 'k', ('eventually', ('and', ('or', 'b', 'l'), ('eventually', ('and', 'h', ('eventually', ('and', 'e', ('eventually', 'f'))))))))) return ret def _get_sequence(self, seq): term = seq[0][0] if len(seq[0]) == 1 else ('or', seq[0][0], seq[0][1]) if len(seq) == 1: return ('eventually',term) return ('eventually',('and', term, self._get_sequence(seq[1:]))) class AdversarialEnvSampler(LTLSampler): def sample(self): p = random.randint(0,1) if p == 0: return ('eventually', ('and', 'a', ('eventually', 'b'))) else: return ('eventually', ('and', 'a', ('eventually', 'c'))) def getRegisteredSamplers(propositions): return [SequenceSampler(propositions), UntilTaskSampler(propositions), DefaultSampler(propositions), EventuallySampler(propositions)] # The LTLSampler factory method that instantiates the proper sampler # based on the @sampler_id. def getLTLSampler(sampler_id, propositions): tokens = ["Default"] if (sampler_id != None): tokens = sampler_id.split("_") # Don't change the order of ifs here otherwise the OR sampler will fail if (tokens[0] == "OrSampler"): return OrSampler(propositions) elif ("_OR_" in sampler_id): # e.g., Sequence_2_4_OR_UntilTask_3_3_1_1 sampler_ids = sampler_id.split("_OR_") return OrSampler(propositions, sampler_ids) elif (tokens[0] == "Sequence"): return SequenceSampler(propositions, tokens[1], tokens[2]) elif (tokens[0] == "Until"): return UntilTaskSampler(propositions, tokens[1], tokens[2], tokens[3], tokens[4]) elif (tokens[0] == "SuperSampler"): return SuperSampler(propositions) elif (tokens[0] == "Adversarial"): return AdversarialEnvSampler(propositions) elif (tokens[0] == "Eventually"): return EventuallySampler(propositions, tokens[1], tokens[2], tokens[3], tokens[4]) else: # "Default" return DefaultSampler(propositions)
8,122
39.615
234
py
T2TL
T2TL-main/src/recurrent_model.py
""" This is the description of the deep NN currently being used. It is a small CNN for the features with an GRU encoding of the LTL task. The features and LTL are preprocessed by utils.format.get_obss_preprocessor(...) function: - In that function, I transformed the LTL tuple representation into a text representation: - Input: ('until',('not','a'),('and', 'b', ('until',('not','c'),'d'))) - output: ['until', 'not', 'a', 'and', 'b', 'until', 'not', 'c', 'd'] Each of those tokens get a one-hot embedding representation by the utils.format.Vocabulary class. """ import torch import torch.nn as nn import torch.nn.functional as F from torch.distributions.categorical import Categorical import torch_ac from gym.spaces import Box, Discrete from gnns.graphs.GCN import * from gnns.graphs.GNN import GNNMaker from env_model import getEnvModel from policy_network import PolicyNetwork from model import LSTMModel, GRUModel, init_params class RecurrentACModel(nn.Module, torch_ac.RecurrentACModel): def __init__(self, env, obs_space, action_space, ignoreLTL, gnn_type, dumb_ac, freeze_ltl): super().__init__() # Decide which components are enabled self.use_progression_info = "progress_info" in obs_space self.use_text = not ignoreLTL and (gnn_type == "GRU" or gnn_type == "LSTM") and "text" in obs_space self.use_ast = not ignoreLTL and ("GCN" in gnn_type) and "text" in obs_space self.gnn_type = gnn_type self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.action_space = action_space self.dumb_ac = dumb_ac self.freeze_pretrained_params = freeze_ltl if self.freeze_pretrained_params: print("Freezing the LTL module.") self.env_model = getEnvModel(env, obs_space) # Define text embedding if self.use_progression_info: self.text_embedding_size = 32 self.simple_encoder = nn.Sequential( nn.Linear(obs_space["progress_info"], 64), nn.Tanh(), nn.Linear(64, self.text_embedding_size), nn.Tanh() ).to(self.device) print("Linear encoder Number of parameters:", sum(p.numel() for p in self.simple_encoder.parameters() if p.requires_grad)) elif self.use_text: self.word_embedding_size = 32 self.text_embedding_size = 32 if self.gnn_type == "GRU": self.text_rnn = GRUModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to(self.device) else: assert(self.gnn_type == "LSTM") self.text_rnn = LSTMModel(obs_space["text"], self.word_embedding_size, 16, self.text_embedding_size).to(self.device) print("RNN Number of parameters:", sum(p.numel() for p in self.text_rnn.parameters() if p.requires_grad)) elif self.use_ast: hidden_dim = 32 self.text_embedding_size = 32 self.gnn = GNNMaker(self.gnn_type, obs_space["text"], self.text_embedding_size).to(self.device) print("GNN Number of parameters:", sum(p.numel() for p in self.gnn.parameters() if p.requires_grad)) # Memory specific code. self.image_embedding_size = self.env_model.size() self.memory_rnn = nn.LSTMCell(self.image_embedding_size, self.semi_memory_size) self.embedding_size = self.semi_memory_size print("embedding size:", self.embedding_size) if self.use_text or self.use_ast or self.use_progression_info: self.embedding_size += self.text_embedding_size if self.dumb_ac: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 1) ) else: # Define actor's model self.actor = PolicyNetwork(self.embedding_size, self.action_space, hiddens=[64, 64, 64], activation=nn.ReLU()) # Define critic's model self.critic = nn.Sequential( nn.Linear(self.embedding_size, 64), nn.Tanh(), nn.Linear(64, 64), nn.Tanh(), nn.Linear(64, 1) ) # Initialize parameters correctly self.apply(init_params) @property def memory_size(self): return 2*self.semi_memory_size @property def semi_memory_size(self): return self.image_embedding_size def forward(self, obs, memory): x = self.env_model(obs) hidden = (memory[:, :self.semi_memory_size], memory[:, self.semi_memory_size:]) hidden = self.memory_rnn(x, hidden) embedding = hidden[0] memory = torch.cat(hidden, dim=1) if self.use_progression_info: embed_ltl = self.simple_encoder(obs.progress_info) embedding = torch.cat((embedding, embed_ltl), dim=1) if embedding is not None else embed_ltl # Adding Text elif self.use_text: embed_text = self.text_rnn(obs.text) embedding = torch.cat((embedding, embed_text), dim=1) if embedding is not None else embed_text # Adding GNN elif self.use_ast: embed_gnn = self.gnn(obs.text) embedding = torch.cat((embedding, embed_gnn), dim=1) if embedding is not None else embed_gnn # Actor dist = self.actor(embedding) # Critic x = self.critic(embedding) value = x.squeeze(1) return dist, value, memory def load_pretrained_gnn(self, model_state): # We delete all keys relating to the actor/critic. new_model_state = model_state.copy() for key in model_state.keys(): if key.find("actor") != -1 or key.find("critic") != -1: del new_model_state[key] self.load_state_dict(new_model_state, strict=False) if self.freeze_pretrained_params: target = self.text_rnn if self.gnn_type == "GRU" or self.gnn_type == "LSTM" else self.gnn for param in target.parameters(): param.requires_grad = False
6,302
37.2
134
py
T2TL
T2TL-main/src/policy_network.py
import torch import torch.nn as nn import torch.nn.functional as F from torch.distributions import Categorical, Normal from gym.spaces import Box, Discrete class PolicyNetwork(nn.Module): def __init__(self, in_dim, action_space, hiddens=[], scales=None, activation=nn.Tanh()): super().__init__() layer_dims = [in_dim] + hiddens # layer_dims = [96, 64, 64, 64] self.action_space = action_space self.num_layers = len(layer_dims) self.enc_ = nn.Sequential(*[fc(in_dim, out_dim, activation=activation) for (in_dim, out_dim) in zip(layer_dims, layer_dims[1:])]) if (isinstance(self.action_space, Discrete)): action_dim = self.action_space.n self.discrete_ = nn.Sequential( nn.Linear(layer_dims[-1], action_dim) ) elif (isinstance(self.action_space, Box)): action_dim = self.action_space.shape[0] self.mu_ = nn.Sequential( fc(layer_dims[-1], action_dim) ) self.std_ = nn.Sequential( fc(layer_dims[-1], action_dim) ) self.softplus = nn.Softplus() # self.scales = [1] * action_dim if scales==None else scales else: print("Unsupported action_space type: ", self.action_space) exit(1) def forward(self, obs): if (isinstance(self.action_space, Discrete)): x = self.enc_(obs) x = self.discrete_(x) return Categorical(logits=F.log_softmax(x, dim=1)) elif (isinstance(self.action_space, Box)): x = self.enc_(obs) mu = 2 * self.mu_(x)# * self.scales std = self.softplus(self.std_(x)) + 1e-3 return Normal(mu, std) else: print("Unsupported action_space type: ", self.action_space) exit(1) def fc(in_dim, out_dim, activation=nn.Tanh()): return nn.Sequential( nn.Linear(in_dim, out_dim), activation )
2,026
33.355932
92
py
T2TL
T2TL-main/src/T1TL.py
import argparse import time import datetime import torch import torch_ac import tensorboardX import sys import glob from math import floor import utils from model import ACModel from recurrent_model import RecurrentACModel if __name__ == '__main__': # Parse arguments parser = argparse.ArgumentParser() ## General parameters parser.add_argument("--algo", default='ppo', help="algorithm to use: a2c | ppo (REQUIRED)") parser.add_argument("--env", default='Zones-25-v1', help="name of the environment to train on (REQUIRED)") parser.add_argument("--ltl-sampler", default="Until_1_2_1_1", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--model", default=None, help="name of the model (default: {ENV}_{SAMPLER}_{ALGO}_{TIME})") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--log-interval", type=int, default=1, help="number of updates between two logs (default: 10)") parser.add_argument("--save-interval", type=int, default=2, help="number of updates between two saves (default: 10, 0 means no saving)") parser.add_argument("--procs", type=int, default=16, help="number of processes (default: 16)") parser.add_argument("--frames", type=int, default=10200000, help="number of frames of training (default: 2*10e8)") parser.add_argument("--checkpoint-dir", default=None) ## Evaluation parameters parser.add_argument("--eval", action="store_true", default=False, help="evaluate the saved model (default: False)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--eval-env", default=None, help="name of the environment to train on (default: use the same \"env\" as training)") parser.add_argument("--ltl-samplers-eval", default=None, nargs='+', help="the ltl formula templates to sample from for evaluation (default: use the same \"ltl-sampler\" as training)") parser.add_argument("--eval-procs", type=int, default=1, help="number of processes (default: use the same \"procs\" as training)") ## Parameters for main algorithm parser.add_argument("--epochs", type=int, default=10, help="number of epochs for PPO (default: 4)") parser.add_argument("--batch-size", type=int, default=1024, help="batch size for PPO (default: 256)") parser.add_argument("--frames-per-proc", type=int, default=4096, help="number of frames per process before update (default: 5 for A2C and 128 for PPO)") parser.add_argument("--discount", type=float, default=0.998, help="discount factor (default: 0.99)") parser.add_argument("--lr", type=float, default=0.0003, help="learning rate (default: 0.0003)") parser.add_argument("--gae-lambda", type=float, default=0.95, help="lambda coefficient in GAE formula (default: 0.95, 1 means no gae)") parser.add_argument("--entropy-coef", type=float, default=0.003, help="entropy term coefficient (default: 0.01)") parser.add_argument("--value-loss-coef", type=float, default=0.5, help="value loss term coefficient (default: 0.5)") parser.add_argument("--max-grad-norm", type=float, default=0.5, help="maximum norm of gradient (default: 0.5)") parser.add_argument("--optim-eps", type=float, default=1e-8, help="Adam and RMSprop optimizer epsilon (default: 1e-8)") parser.add_argument("--optim-alpha", type=float, default=0.99, help="RMSprop optimizer alpha (default: 0.99)") parser.add_argument("--clip-eps", type=float, default=0.2, help="clipping epsilon for PPO (default: 0.2)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--noLTL", action="store_true", default=False, help="the environment no longer has an LTL goal. --ignoreLTL must be specified concurrently.") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="Transformer", help="use gnn to model the LTL (only if ignoreLTL==True)") parser.add_argument("--trans_layer", type=int, default=1, help="the number of Transformer layers need to use") parser.add_argument("--int-reward", type=float, default=0.0, help="the intrinsic reward for LTL progression (default: 0.0)") parser.add_argument("--pretrained-gnn", action="store_true", default=False, help="load a pre-trained LTL module.") parser.add_argument("--dumb-ac", action="store_true", default=False, help="Use a single-layer actor-critic") parser.add_argument("--freeze-ltl", action="store_true", default=False, help="Freeze the gradient updates of the LTL module") # Transformer special parameters parser.add_argument("--d_model", type=int, default=64, help="") parser.add_argument("--nhead", type=int, default=8, help="") parser.add_argument("--num_encoder_layers", type=int, default=4, help="") parser.add_argument("--pool", type=str, default='mean', help="") parser.add_argument("--dim_feedforward", type=int, default=256, help="") parser.add_argument("--dropout", type=float, default=0.0, help="") parser.add_argument("--d_out", type=int, default=16, help="") parser.add_argument("--layer_norm_eps", type=float, default=1e-5, help="") parser.add_argument("--TFixup", type=bool, default=True, help="") parser.add_argument("--cuda", type=str, default='cuda:0', help="") # additional desciption for test parser.add_argument("--sth", type=str, default='None', help="") args = parser.parse_args() use_mem = args.recurrence > 1 # whether use memory or not # Set run dir date = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") # '21-08-21-22-36-39' gnn_name = args.gnn if args.ignoreLTL: gnn_name = "IgnoreLTL" if args.dumb_ac: gnn_name = gnn_name + "-dumb_ac" if args.pretrained_gnn: gnn_name = gnn_name + "-pretrained" if args.freeze_ltl: gnn_name = gnn_name + "-freeze_ltl" if use_mem: gnn_name = gnn_name + "-recurrence:%d"%(args.recurrence) if args.gnn == 'Transformer': default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_bs:{args.batch_size}_n_encoder:{args.num_encoder_layers}_nhead:{args.nhead}_d_model:{args.d_model}_FFD:{args.dim_feedforward}_d_out:{args.d_out}_Init:{args.TFixup}_sth:{args.sth}" else: # 'RGCN_8x32_ROOT_SHARED_Until_1_2_1_1_Zones-5-v0_seed:1_epochs:10_bs:2048_fpp:4096_dsc:0.998_lr:0.0003_ent:0.003_clip:0.2_prog:full' default_model_name = f"{gnn_name}_{args.ltl_sampler}_{args.env}_seed:{args.seed}_epochs:{args.epochs}_bs:{args.batch_size}_fpp:{args.frames_per_proc}_dsc:{args.discount}_lr:{args.lr}_ent:{args.entropy_coef}_clip:{args.clip_eps}_prog:{args.progression_mode}" model_name = args.model or default_model_name storage_dir = "storage" if args.checkpoint_dir is None else args.checkpoint_dir model_dir = utils.get_model_dir(model_name, storage_dir) pretrained_model_dir = None if args.pretrained_gnn: assert(args.progression_mode == "full") # default_dir = f"symbol-storage/{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" default_dir = f"{args.gnn}-dumb_ac_{args.ltl_sampler}_Simple-LTL-Env-v0_seed:{args.seed}_*_prog:{args.progression_mode}/train" print(default_dir) model_dirs = glob.glob(default_dir) if len(model_dirs) == 0: raise Exception("Pretraining directory not found.") elif len(model_dirs) > 1: raise Exception("More than 1 candidate pretraining directory found.") pretrained_model_dir = model_dirs[0] # Load loggers and Tensorboard writer txt_logger = utils.get_txt_logger(model_dir + "/train") csv_file, csv_logger = utils.get_csv_logger(model_dir + "/train") tb_writer = tensorboardX.SummaryWriter(model_dir + "/train") utils.save_config(model_dir + "/train", args) # Log command and all script arguments txt_logger.info("{}\n".format(" ".join(sys.argv))) txt_logger.info("{}\n".format(args)) # It will output the context of Namespace # Set seed for all randomness sources utils.seed(args.seed) # Set device device = torch.device(args.cuda) # device = torch.device('cpu') txt_logger.info(f"Device: {device}\n") # Output the device (default is cpu) # Load environments envs = [] progression_mode = args.progression_mode for i in range(args.procs): # load the env & progression_mode & LTL formula # turn to utils/env.py envs.append(utils.make_env(args.env, progression_mode, args.ltl_sampler, args.seed, args.int_reward, args.noLTL)) # Sync environments envs[0].reset() # Add the agent to map & translate the LTL formula txt_logger.info("Environments loaded\n") # Load training status try: status = utils.get_status(model_dir + "/train", args) except OSError: status = {"num_frames": 0, "update": 0} # ??? the state of algorithm ? txt_logger.info("Training status loaded.\n") if pretrained_model_dir is not None: try: pretrained_status = utils.get_status(pretrained_model_dir, args) except: txt_logger.info("Failed to load pretrained model.\n") exit(1) # Load observations preprocessor-- build AST using_gnn = (args.gnn != "GRU" and args.gnn != "LSTM" and args.gnn != "Transformer") # turn to env/format.py obs_space, preprocess_obss = utils.get_obss_preprocessor(envs[0], using_gnn, progression_mode) if "vocab" in status and preprocess_obss.vocab is not None: preprocess_obss.vocab.load_vocab(status["vocab"]) txt_logger.info("Observations preprocessor loaded.\n") # Load model if use_mem: acmodel = RecurrentACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl) else: acmodel = ACModel(envs[0].env, obs_space, envs[0].action_space, args.ignoreLTL, args.gnn, args.dumb_ac, args.freeze_ltl, args) if "model_state" in status: acmodel.load_state_dict(status["model_state"]) txt_logger.info("Loading model from existing run.\n") elif args.pretrained_gnn: acmodel.load_pretrained_gnn(pretrained_status["model_state"]) txt_logger.info("Pretrained model loaded.\n") acmodel.to(device) txt_logger.info("Model loaded.\n") txt_logger.info("{}\n".format(acmodel)) # Load algo if args.algo == "a2c": algo = torch_ac.A2CAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_alpha, args.optim_eps, preprocess_obss) elif args.algo == "ppo": algo = torch_ac.PPOAlgo(envs, acmodel, device, args.frames_per_proc, args.discount, args.lr, args.gae_lambda, args.entropy_coef, args.value_loss_coef, args.max_grad_norm, args.recurrence, args.optim_eps, args.clip_eps, args.epochs, args.batch_size, preprocess_obss) else: raise ValueError("Incorrect algorithm name: {}".format(args.algo)) if "optimizer_state" in status: algo.optimizer.load_state_dict(status["optimizer_state"]) txt_logger.info("Loading optimizer from existing run.\n") txt_logger.info("Optimizer loaded.\n") # init the evaluator if args.eval: eval_samplers = args.ltl_samplers_eval if args.ltl_samplers_eval else [args.ltl_sampler] eval_env = args.eval_env if args.eval_env else args.env eval_procs = args.eval_procs if args.eval_procs else args.procs evals = [] for eval_sampler in eval_samplers: evals.append(utils.Eval(eval_env, model_name, eval_sampler, seed=args.seed, device=device, num_procs=eval_procs, ignoreLTL=args.ignoreLTL, progression_mode=progression_mode, gnn=args.gnn, dumb_ac = args.dumb_ac)) # Train model num_frames = status["num_frames"] # num_frames:0 update = status["update"] # update:0 start_time = time.time() while num_frames < args.frames: # Update model parameters update_start_time = time.time() exps, logs1 = algo.collect_experiences() # interacte with environmets (very important) logs2 = algo.update_parameters(exps) logs = {**logs1, **logs2} update_end_time = time.time() num_frames += logs["num_frames"] update += 1 # Print logs if update % args.log_interval == 0: fps = logs["num_frames"]/(update_end_time - update_start_time) duration = int(time.time() - start_time) return_per_episode = utils.synthesize(logs["return_per_episode"]) rreturn_per_episode = utils.synthesize(logs["reshaped_return_per_episode"]) average_reward_per_step = utils.average_reward_per_step(logs["return_per_episode"], logs["num_frames_per_episode"]) average_discounted_return = utils.average_discounted_return(logs["return_per_episode"], logs["num_frames_per_episode"], args.discount) num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"]) header = ["update", "frames", "FPS", "duration"] data = [update, num_frames, fps, duration] header += ["rreturn_" + key for key in rreturn_per_episode.keys()] data += rreturn_per_episode.values() header += ["average_reward_per_step", "average_discounted_return"] data += [average_reward_per_step, average_discounted_return] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["entropy", "value", "policy_loss", "value_loss", "grad_norm"] data += [logs["entropy"], logs["value"], logs["policy_loss"], logs["value_loss"], logs["grad_norm"]] txt_logger.info( "U {} | F {:06} | FPS {:04.0f} | D {} | rR:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | ARPS: {:.3f} | ADR: {:.3f} | F:μσmM {:.1f} {:.1f} {} {} | H {:.3f} | V {:.3f} | pL {:.3f} | vL {:.3f} | ∇ {:.3f}" .format(*data)) header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() if status["num_frames"] == 0: csv_logger.writerow(header) csv_logger.writerow(data) csv_file.flush() for field, value in zip(header, data): tb_writer.add_scalar(field, value, num_frames) # Save status if args.save_interval > 0 and update % args.save_interval == 0: status = {"num_frames": num_frames, "update": update, "model_state": algo.acmodel.state_dict(), "optimizer_state": algo.optimizer.state_dict()} if hasattr(preprocess_obss, "vocab") and preprocess_obss.vocab is not None: status["vocab"] = preprocess_obss.vocab.vocab utils.save_status(status, model_dir + "/train") txt_logger.info("Status saved") if args.eval: # we send the num_frames to align the eval curves with the training curves on TB for evalu in evals: evalu.eval(num_frames, episodes=args.eval_episodes)
16,666
49.506061
268
py
T2TL
T2TL-main/src/torch_ac/format.py
import torch def default_preprocess_obss(obss, device=None): return torch.tensor(obss, device=device)
106
25.75
47
py
T2TL
T2TL-main/src/torch_ac/model.py
from abc import abstractmethod, abstractproperty import torch.nn as nn import torch.nn.functional as F class ACModel: recurrent = False @abstractmethod def __init__(self, obs_space, action_space): pass @abstractmethod def forward(self, obs): pass class RecurrentACModel(ACModel): recurrent = True @abstractmethod def forward(self, obs, memory): pass @property @abstractmethod def memory_size(self): pass
485
17.692308
48
py
T2TL
T2TL-main/src/torch_ac/__init__.py
from torch_ac.algos import A2CAlgo, PPOAlgo from torch_ac.model import ACModel, RecurrentACModel from torch_ac.utils import DictList
132
43.333333
52
py
T2TL
T2TL-main/src/torch_ac/algos/base.py
from abc import ABC, abstractmethod import torch from torch_ac.format import default_preprocess_obss from torch_ac.utils import DictList, ParallelEnv import numpy as np from collections import deque class BaseAlgo(ABC): """The base class for RL algorithms.""" def __init__(self, envs, acmodel, device, num_frames_per_proc, discount, lr, gae_lambda, entropy_coef, value_loss_coef, max_grad_norm, recurrence, preprocess_obss, reshape_reward, history_length): """ Initializes a `BaseAlgo` instance. Parameters: ---------- envs : list a list of environments that will be run in parallel acmodel : torch.Module the model num_frames_per_proc : int the number of frames collected by every process for an update discount : float the discount for future rewards lr : float the learning rate for optimizers gae_lambda : float the lambda coefficient in the GAE formula ([Schulman et al., 2015](https://arxiv.org/abs/1506.02438)) entropy_coef : float the weight of the entropy cost in the final objective value_loss_coef : float the weight of the value loss in the final objective max_grad_norm : float gradient will be clipped to be at most this value recurrence : int the number of steps the gradient is propagated back in time preprocess_obss : function a function that takes observations returned by the environment and converts them into the format that the model can handle reshape_reward : function a function that shapes the reward, takes an (observation, action, reward, done) tuple as an input """ # Store parameters self.env = ParallelEnv(envs) self.acmodel = acmodel self.device = device self.num_frames_per_proc = num_frames_per_proc # 4096 self.discount = discount # 0.998 self.lr = lr # 0.0003 self.gae_lambda = gae_lambda self.entropy_coef = entropy_coef # 0.95 self.value_loss_coef = value_loss_coef # 0.5 self.max_grad_norm = max_grad_norm # 0.5 self.recurrence = recurrence # 1 self.preprocess_obss = preprocess_obss or default_preprocess_obss self.reshape_reward = reshape_reward # None self.action_space_shape = envs[0].action_space.shape # 2 self.use_cont = self.acmodel.context # Control parameters assert self.acmodel.recurrent or self.recurrence == 1 assert self.num_frames_per_proc % self.recurrence == 0 # Configure acmodel self.acmodel.to(self.device) self.acmodel.train() # Store helpers values self.num_procs = len(envs) # 16 self.num_frames = self.num_frames_per_proc * self.num_procs # 4096*16=65536 # Initialize experience values shape = (self.num_frames_per_proc, self.num_procs) # shape: (4096, 16) act_shape = shape + self.action_space_shape # act_shape: (4096, 16, 2) # in this, each env has its own original ltl self.obs = self.env.reset() # all 16 envs will be reset (in this, each env has its own original ltl) self.obss = [None]*(shape[0]) # [None,...4096..., None] if self.acmodel.recurrent: self.memory = torch.zeros(shape[1], self.acmodel.memory_size, device=self.device) self.memories = torch.zeros(*shape, self.acmodel.memory_size, device=self.device) self.mask = torch.ones(shape[1], device=self.device) # [1., ...16..., 1.] self.masks = torch.zeros(*shape, device=self.device) # torch.Size([4096, 16]) self.actions = torch.zeros(*act_shape, device=self.device) #, dtype=torch.int) torch.Size([4096, 16, 2]) self.values = torch.zeros(*shape, device=self.device) # torch.Size([4096, 16]) self.rewards = torch.zeros(*shape, device=self.device) # torch.Size([4096, 16]) self.advantages = torch.zeros(*shape, device=self.device) # torch.Size([4096, 16]) self.log_probs = torch.zeros(*act_shape, device=self.device) # torch.Size([4096, 16, 2]) if self.use_cont: # Initialize Context Variable Setup # todo: check ### history #### with torch.no_grad(): preprocess_obs = self.preprocess_obss(self.obs, device=self.device) preprocess_obs = self.acmodel.env_model(preprocess_obs) self.history_length = history_length self.rewards_hist = deque(maxlen=history_length) self.actions_hist = deque(maxlen=history_length) self.obsvs_hist = deque(maxlen=history_length) self.next_hrews = deque(maxlen=history_length) self.next_hacts = deque(maxlen=history_length) self.next_hobvs = deque(maxlen=history_length) zero_action = torch.zeros(*(self.num_procs, self.action_space_shape[0]), device=self.device) zero_obs = torch.zeros(*(self.num_procs, (self.acmodel.embedding_size-self.acmodel.text_embedding_size)), device=self.device) for _ in range(history_length): self.rewards_hist.append(torch.zeros(*(shape[1], 1), device=self.device)) self.actions_hist.append(zero_action.clone()) self.obsvs_hist.append(zero_obs.clone()) # same thing for next_h* self.next_hrews.append(torch.zeros(*(shape[1], 1), device=self.device)) self.next_hacts.append(zero_action.clone()) self.next_hobvs.append(zero_obs.clone()) self.rewards_hist.append(torch.zeros(*(shape[1], 1), device=self.device)) self.obsvs_hist.append(preprocess_obs.clone()) rand_action = torch.FloatTensor(envs[0].action_space.sample()).unsqueeze(0) for m in range(len(envs)-1): rand_action = torch.concat([rand_action, torch.FloatTensor(envs[m+1].action_space.sample()).unsqueeze(0)], dim=0) self.actions_hist.append(rand_action.to(self.device).clone()) self.rewards_hist_pro = torch.zeros(*(shape[0], history_length*shape[1]), device=self.device) self.actions_hist_pro = torch.zeros(*(shape[0], history_length*self.action_space_shape[0]*shape[1]), device=self.device) self.obsvs_hist_pro = torch.zeros(*(shape[0], history_length*shape[1]*(self.acmodel.embedding_size-self.acmodel.text_embedding_size)), device=self.device) self.next_hrews_pro = torch.zeros(*(shape[0], history_length*shape[1]), device=self.device) self.next_hacts_pro = torch.zeros(*(shape[0], history_length*self.action_space_shape[0]*shape[1]), device=self.device) self.next_hobvs_pro = torch.zeros(*(shape[0], history_length*shape[1]*(self.acmodel.embedding_size-self.acmodel.text_embedding_size)), device=self.device) # Initialize log values self.log_episode_return = torch.zeros(self.num_procs, device=self.device) # shape = (16,) self.log_episode_reshaped_return = torch.zeros(self.num_procs, device=self.device) # shape = (16,) self.log_episode_num_frames = torch.zeros(self.num_procs, device=self.device) # shape = (16,) self.log_done_counter = 0 self.log_return = [0] * self.num_procs self.log_reshaped_return = [0] * self.num_procs self.log_num_frames = [0] * self.num_procs def collect_experiences(self): """ Collects rollouts and computes advantages. Runs several environments concurrently. The next actions are computed in a batch mode for all environments at the same time. The rollouts and advantages from all environments are concatenated together. Returns ------- exps : DictList Contains actions, rewards, advantages etc as attributes. Each attribute, e.g. `exps.reward` has a shape (self.num_frames_per_proc * num_envs, ...). k-th block of consecutive `self.num_frames_per_proc` frames contains data obtained from the k-th environment. Be careful not to mix data from different environments! logs : dict Useful stats about the training process, including the average reward, policy loss, value loss, etc. """ for i in range(self.num_frames_per_proc): # range(4096) # Do one agent-environment interaction if self.use_cont: # previous context variable np_pre_actions, np_pre_rewards, np_pre_obsers = self.actions_hist[0], self.rewards_hist[0], self.obsvs_hist[0] for k in range(self.history_length-1): np_pre_actions = torch.concat([np_pre_actions, self.actions_hist[k + 1]], dim=1) np_pre_rewards = torch.concat([np_pre_rewards, self.rewards_hist[k + 1]], dim=1) np_pre_obsers = torch.concat([np_pre_obsers, self.obsvs_hist[k + 1]], dim=1) self.actions_hist_pro[i] = np_pre_actions.flatten().unsqueeze(0) self.rewards_hist_pro[i] = np_pre_rewards.flatten().unsqueeze(0) self.obsvs_hist_pro[i] = np_pre_obsers.flatten().unsqueeze(0) preprocessed_obs = self.preprocess_obss(self.obs, device=self.device) with torch.no_grad(): if self.acmodel.recurrent: dist, value, memory = self.acmodel(preprocessed_obs, self.memory * self.mask.unsqueeze(1)) elif self.acmodel.context: dist, value, embedding = self.acmodel(preprocessed_obs, [np_pre_actions, np_pre_rewards, np_pre_obsers], ) else: dist, value = self.acmodel(preprocessed_obs) # dist = Normal(loc: torch.Size([16, 2]), scale: torch.Size([16, 2])); shape(value)=16 action = dist.sample() # shape = torch.Size([16, 2]) obs, reward, done, _ = self.env.step(action.cpu().numpy()) if self.use_cont: ############### self.next_hrews.append(torch.FloatTensor(reward).view(self.num_procs, 1).to(self.device)) self.next_hacts.append(action.clone()) self.next_hobvs.append(embedding.clone()) # todo: check # np_next_hacts and np_next_hrews are required for TD3 alg np_next_hacts, np_next_hrews, np_next_hobvs = self.next_hacts[0], self.next_hrews[0], self.next_hobvs[0] for k in range(self.history_length - 1): np_next_hacts= torch.concat([np_next_hacts, self.next_hacts[k + 1]], dim=1) np_next_hrews = torch.concat([np_next_hrews, self.next_hrews[k + 1]], dim=1) np_next_hobvs= torch.concat([np_next_hobvs, self.next_hobvs[k + 1]], dim=1) # np_next_hacts = np.asarray(self.next_hacts, dtype=np.float32).flatten() # (hist, action_dim) => (hist *action_dim,) # np_next_hrews = np.asarray(self.next_hrews, dtype=np.float32) # (hist, ) # np_next_hobvs = np.asarray(self.next_hobvs, dtype=np.float32).flatten() # (hist, ) self.next_hacts_pro[i] = np_next_hacts.flatten().unsqueeze(0) self.next_hrews_pro[i] = np_next_hrews.flatten().unsqueeze(0) self.next_hobvs_pro[i] = np_next_hobvs.flatten().unsqueeze(0) # new becomes old self.rewards_hist.append(torch.FloatTensor(reward).view(self.num_procs, 1).to(self.device)) self.actions_hist.append(action.clone()) self.obsvs_hist.append(embedding.clone()) # todo: check # Update experiences values self.obss[i] = self.obs # each i = {list: 16} self.obs = obs if self.acmodel.recurrent: self.memories[i] = self.memory self.memory = memory self.masks[i] = self.mask self.mask = 1 - torch.tensor(done, device=self.device, dtype=torch.float) self.actions[i] = action self.values[i] = value if self.reshape_reward is not None: self.rewards[i] = torch.tensor([ self.reshape_reward(obs_, action_, reward_, done_) for obs_, action_, reward_, done_ in zip(obs, action, reward, done) ], device=self.device) else: self.rewards[i] = torch.tensor(reward, device=self.device) self.log_probs[i] = dist.log_prob(action) # Update log values self.log_episode_return += torch.tensor(reward, device=self.device, dtype=torch.float) self.log_episode_reshaped_return += self.rewards[i] self.log_episode_num_frames += torch.ones(self.num_procs, device=self.device) for i, done_ in enumerate(done): if done_: self.log_done_counter += 1 self.log_return.append(self.log_episode_return[i].item()) self.log_reshaped_return.append(self.log_episode_reshaped_return[i].item()) self.log_num_frames.append(self.log_episode_num_frames[i].item()) self.log_episode_return *= self.mask self.log_episode_reshaped_return *= self.mask self.log_episode_num_frames *= self.mask # Add advantage and return to experiences preprocessed_obs = self.preprocess_obss(self.obs, device=self.device) with torch.no_grad(): if self.acmodel.recurrent: _, next_value, _ = self.acmodel(preprocessed_obs, self.memory * self.mask.unsqueeze(1)) elif self.acmodel.context: _, next_value, _ = self.acmodel(preprocessed_obs, [np_next_hacts, np_next_hrews, np_next_hobvs], ) else: _, next_value = self.acmodel(preprocessed_obs) for i in reversed(range(self.num_frames_per_proc)): next_mask = self.masks[i+1] if i < self.num_frames_per_proc - 1 else self.mask next_value = self.values[i+1] if i < self.num_frames_per_proc - 1 else next_value next_advantage = self.advantages[i+1] if i < self.num_frames_per_proc - 1 else 0 delta = self.rewards[i] + self.discount * next_value * next_mask - self.values[i] self.advantages[i] = delta + self.discount * self.gae_lambda * next_advantage * next_mask # Define experiences: # the whole experience is the concatenation of the experience # of each process. # In comments below: # - T is self.num_frames_per_proc, # - P is self.num_procs, # - D is the dimensionality. exps = DictList() exps.obs = [self.obss[i][j] for j in range(self.num_procs) for i in range(self.num_frames_per_proc)] if self.acmodel.recurrent: # T x P x D -> P x T x D -> (P * T) x D exps.memory = self.memories.transpose(0, 1).reshape(-1, *self.memories.shape[2:]) # T x P -> P x T -> (P * T) x 1 exps.mask = self.masks.transpose(0, 1).reshape(-1).unsqueeze(1) # for all tensors below, T x P -> P x T -> P * T exps.action = self.actions.transpose(0, 1).reshape((-1, ) + self.action_space_shape) exps.value = self.values.transpose(0, 1).reshape(-1) exps.reward = self.rewards.transpose(0, 1).reshape(-1) exps.advantage = self.advantages.transpose(0, 1).reshape(-1) exps.returnn = exps.value + exps.advantage exps.log_prob = self.log_probs.transpose(0, 1).reshape((-1, ) + self.action_space_shape) if self.use_cont: exps.actions_hist = self.actions_hist_pro.reshape(-1, self.history_length*self.action_space_shape[0]) exps.rewards_hist = self.rewards_hist_pro.reshape(-1, self.history_length) exps.obsvs_hist = self.obsvs_hist_pro.reshape(-1, self.history_length*(self.acmodel.embedding_size-self.acmodel.text_embedding_size)) # Preprocess experiences exps.obs = self.preprocess_obss(exps.obs, device=self.device) # Log some values keep = max(self.log_done_counter, self.num_procs) logs = { "return_per_episode": self.log_return[-keep:], "reshaped_return_per_episode": self.log_reshaped_return[-keep:], "num_frames_per_episode": self.log_num_frames[-keep:], "num_frames": self.num_frames } self.log_done_counter = 0 self.log_return = self.log_return[-self.num_procs:] self.log_reshaped_return = self.log_reshaped_return[-self.num_procs:] self.log_num_frames = self.log_num_frames[-self.num_procs:] return exps, logs @abstractmethod def update_parameters(self): pass
17,512
49.469741
152
py
T2TL
T2TL-main/src/torch_ac/algos/a2c.py
import numpy import torch import torch.nn.functional as F from torch_ac.algos.base import BaseAlgo class A2CAlgo(BaseAlgo): """The Advantage Actor-Critic algorithm.""" def __init__(self, envs, acmodel, device=None, num_frames_per_proc=None, discount=0.99, lr=0.01, gae_lambda=0.95, entropy_coef=0.01, value_loss_coef=0.5, max_grad_norm=0.5, recurrence=4, rmsprop_alpha=0.99, rmsprop_eps=1e-8, preprocess_obss=None, reshape_reward=None): num_frames_per_proc = num_frames_per_proc or 8 super().__init__(envs, acmodel, device, num_frames_per_proc, discount, lr, gae_lambda, entropy_coef, value_loss_coef, max_grad_norm, recurrence, preprocess_obss, reshape_reward) self.optimizer = torch.optim.RMSprop(self.acmodel.parameters(), lr, alpha=rmsprop_alpha, eps=rmsprop_eps) def update_parameters(self, exps): # Compute starting indexes inds = self._get_starting_indexes() # Initialize update values update_entropy = 0 update_value = 0 update_policy_loss = 0 update_value_loss = 0 update_loss = 0 # Initialize memory if self.acmodel.recurrent: memory = exps.memory[inds] for i in range(self.recurrence): # Create a sub-batch of experience sb = exps[inds + i] # Compute loss if self.acmodel.recurrent: dist, value, memory = self.acmodel(sb.obs, memory * sb.mask) else: dist, value = self.acmodel(sb.obs) entropy = dist.entropy().mean() policy_loss = -(dist.log_prob(sb.action) * sb.advantage).mean() value_loss = (value - sb.returnn).pow(2).mean() loss = policy_loss - self.entropy_coef * entropy + self.value_loss_coef * value_loss # Update batch values update_entropy += entropy.item() update_value += value.mean().item() update_policy_loss += policy_loss.item() update_value_loss += value_loss.item() update_loss += loss # Update update values update_entropy /= self.recurrence update_value /= self.recurrence update_policy_loss /= self.recurrence update_value_loss /= self.recurrence update_loss /= self.recurrence # Update actor-critic self.optimizer.zero_grad() update_loss.backward() update_grad_norm = sum(p.grad.data.norm(2) ** 2 for p in self.acmodel.parameters()) ** 0.5 torch.nn.utils.clip_grad_norm_(self.acmodel.parameters(), self.max_grad_norm) self.optimizer.step() # Log some values logs = { "entropy": update_entropy, "value": update_value, "policy_loss": update_policy_loss, "value_loss": update_value_loss, "grad_norm": update_grad_norm } return logs def _get_starting_indexes(self): """Gives the indexes of the observations given to the model and the experiences used to compute the loss at first. The indexes are the integers from 0 to `self.num_frames` with a step of `self.recurrence`. If the model is not recurrent, they are all the integers from 0 to `self.num_frames`. Returns ------- starting_indexes : list of int the indexes of the experiences to be used at first """ starting_indexes = numpy.arange(0, self.num_frames, self.recurrence) return starting_indexes
3,659
31.972973
117
py
T2TL
T2TL-main/src/torch_ac/algos/ppo.py
import numpy import torch import torch.nn.functional as F from torch_ac.algos.base import BaseAlgo class PPOAlgo(BaseAlgo): """The Proximal Policy Optimization algorithm ([Schulman et al., 2015](https://arxiv.org/abs/1707.06347)).""" def __init__(self, envs, acmodel, device=None, num_frames_per_proc=None, discount=0.99, lr=0.001, gae_lambda=0.95, entropy_coef=0.01, value_loss_coef=0.5, max_grad_norm=0.5, recurrence=4, adam_eps=1e-8, clip_eps=0.2, epochs=4, batch_size=256, preprocess_obss=None, reshape_reward=None, history_length=16): num_frames_per_proc = num_frames_per_proc or 128 super().__init__(envs, acmodel, device, num_frames_per_proc, discount, lr, gae_lambda, entropy_coef, value_loss_coef, max_grad_norm, recurrence, preprocess_obss, reshape_reward, history_length) self.clip_eps = clip_eps # 0.2 self.epochs = epochs # 10 self.batch_size = batch_size # 2048 self.act_shape = envs[0].action_space.shape # 2 assert self.batch_size % self.recurrence == 0 self.optimizer = torch.optim.Adam(self.acmodel.parameters(), lr, eps=adam_eps) self.batch_num = 0 def update_parameters(self, exps): # Collect experiences for _ in range(self.epochs): # Initialize log values log_entropies = [] log_values = [] log_policy_losses = [] log_value_losses = [] log_grad_norms = [] for inds in self._get_batches_starting_indexes(): # Initialize batch values batch_entropy = 0 batch_value = 0 batch_policy_loss = 0 batch_value_loss = 0 batch_loss = 0 # Initialize memory if self.acmodel.recurrent: memory = exps.memory[inds] for i in range(self.recurrence): # Create a sub-batch of experience sb = exps[inds + i] # Compute loss if self.acmodel.recurrent: dist, value, memory = self.acmodel(sb.obs, memory * sb.mask) elif self.acmodel.context: dist, value, _ = self.acmodel(sb.obs, [sb.actions_hist, sb.rewards_hist, sb.obsvs_hist]) else: dist, value = self.acmodel(sb.obs) entropy = dist.entropy().mean() # ratio = torch.exp(dist.log_prob(sb.action) - sb.log_prob) delta_log_prob = dist.log_prob(sb.action) - sb.log_prob if (len(self.act_shape) == 1): # Not scalar actions (multivariate) delta_log_prob = torch.sum(delta_log_prob, dim=1) ratio = torch.exp(delta_log_prob) surr1 = ratio * sb.advantage surr2 = torch.clamp(ratio, 1.0 - self.clip_eps, 1.0 + self.clip_eps) * sb.advantage policy_loss = -torch.min(surr1, surr2).mean() value_clipped = sb.value + torch.clamp(value - sb.value, -self.clip_eps, self.clip_eps) surr1 = (value - sb.returnn).pow(2) surr2 = (value_clipped - sb.returnn).pow(2) value_loss = torch.max(surr1, surr2).mean() loss = policy_loss - self.entropy_coef * entropy + self.value_loss_coef * value_loss # Update batch values batch_entropy += entropy.item() batch_value += value.mean().item() batch_policy_loss += policy_loss.item() batch_value_loss += value_loss.item() batch_loss += loss # Update memories for next epoch if self.acmodel.recurrent and i < self.recurrence - 1: exps.memory[inds + i + 1] = memory.detach() # Update batch values batch_entropy /= self.recurrence batch_value /= self.recurrence batch_policy_loss /= self.recurrence batch_value_loss /= self.recurrence batch_loss /= self.recurrence # Update actor-critic self.optimizer.zero_grad() batch_loss.backward() grad_norm = sum(p.grad.data.norm(2).item() ** 2 for p in self.acmodel.parameters() if p.requires_grad) ** 0.5 torch.nn.utils.clip_grad_norm_([p for p in self.acmodel.parameters() if p.requires_grad], self.max_grad_norm) self.optimizer.step() # Update log values log_entropies.append(batch_entropy) log_values.append(batch_value) log_policy_losses.append(batch_policy_loss) log_value_losses.append(batch_value_loss) log_grad_norms.append(grad_norm) # Log some values logs = { "entropy": numpy.mean(log_entropies), "value": numpy.mean(log_values), "policy_loss": numpy.mean(log_policy_losses), "value_loss": numpy.mean(log_value_losses), "grad_norm": numpy.mean(log_grad_norms) } return logs def _get_batches_starting_indexes(self): """Gives, for each batch, the indexes of the observations given to the model and the experiences used to compute the loss at first. First, the indexes are the integers from 0 to `self.num_frames` with a step of `self.recurrence`, shifted by `self.recurrence//2` one time in two for having more diverse batches. Then, the indexes are splited into the different batches. Returns ------- batches_starting_indexes : list of list of int the indexes of the experiences to be used at first for each batch """ indexes = numpy.arange(0, self.num_frames, self.recurrence) indexes = numpy.random.permutation(indexes) # Shift starting indexes by self.recurrence//2 half the time if self.batch_num % 2 == 1: indexes = indexes[(indexes + self.recurrence) % self.num_frames_per_proc != 0] indexes += self.recurrence // 2 self.batch_num += 1 num_indexes = self.batch_size // self.recurrence batches_starting_indexes = [indexes[i:i+num_indexes] for i in range(0, len(indexes), num_indexes)] return batches_starting_indexes
6,682
39.50303
125
py
T2TL
T2TL-main/src/torch_ac/algos/__init__.py
from torch_ac.algos.a2c import A2CAlgo from torch_ac.algos.ppo import PPOAlgo
77
38
38
py
T2TL
T2TL-main/src/torch_ac/utils/penv.py
from multiprocessing import Process, Pipe import gym def worker(conn, env): ''' conn = <multiprocessing.connection.Connection object at 0x7f9aacbb5d68> env = <LTLEnv<ZonesEnv5<Zones-5-v0>>> ''' while True: cmd, data = conn.recv() if cmd == "step": obs, reward, done, info = env.step(data) if done: obs = env.reset() conn.send((obs, reward, done, info)) elif cmd == "reset": obs = env.reset() conn.send(obs) elif cmd == "kill": return else: raise NotImplementedError class ParallelEnv(gym.Env): """A concurrent execution of environments in multiple processes.""" def __init__(self, envs): assert len(envs) >= 1, "No environment given." self.envs = envs ## {list:16} self.observation_space = self.envs[0].observation_space # Dict(features:Box(76,)) self.action_space = self.envs[0].action_space # Box(2,) self.locals = [] for env in self.envs[1:]: local, remote = Pipe() self.locals.append(local) p = Process(target=worker, args=(remote, env)) p.daemon = True p.start() # turn to def worker(conn, env): remote.close() def __del__(self): for local in self.locals: local.send(("kill", None)) def reset(self): for local in self.locals: local.send(("reset", None)) results = [self.envs[0].reset()] + [local.recv() for local in self.locals] return results def step(self, actions): for local, action in zip(self.locals, actions[1:]): local.send(("step", action)) obs, reward, done, info = self.envs[0].step(actions[0]) if done: obs = self.envs[0].reset() results = zip(*[(obs, reward, done, info)] + [local.recv() for local in self.locals]) return results def render(self): raise NotImplementedError
2,035
31.83871
93
py
T2TL
T2TL-main/src/torch_ac/utils/dictlist.py
class DictList(dict): """A dictionnary of lists of same size. Dictionnary items can be accessed using `.` notation and list items using `[]` notation. Example: >>> d = DictList({"a": [[1, 2], [3, 4]], "b": [[5], [6]]}) >>> d.a [[1, 2], [3, 4]] >>> d[0] DictList({"a": [1, 2], "b": [5]}) """ __getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ def __len__(self): return len(next(iter(dict.values(self)))) def __getitem__(self, index): return DictList({key: value[index] for key, value in dict.items(self)}) def __setitem__(self, index, d): for key, value in d.items(): dict.__getitem__(self, key)[index] = value
737
29.75
79
py
T2TL
T2TL-main/src/torch_ac/utils/__init__.py
from torch_ac.utils.dictlist import DictList from torch_ac.utils.penv import ParallelEnv
88
43.5
44
py
T2TL
T2TL-main/src/envs/__init__.py
from gym.envs.registration import register from envs.safety.zones_env import ZonesEnv __all__ = ["ZonesEnv"] ### Safety Envs register( id='Zones-25-v1', entry_point='envs.safety.zones_env:ZonesEnv25Fixed')
218
17.25
56
py
T2TL
T2TL-main/src/envs/safety/safety_wrappers.py
import gym import glfw from mujoco_py import MjViewer, const """ A simple wrapper for SafetyGym envs. It uses the PlayViewer that listens to key_pressed events and passes the id of the pressed key as part of the observation to the agent. (used to control the agent via keyboard) Should NOT be used for training! """ class Play(gym.Wrapper): def __init__(self, env): super().__init__(env) self.env = env self.key_pressed = None # Shows a text on the upper right corner of the screen (currently used to display the LTL formula) def show_text(self, text): self.env.viewer.show_text(text) def show_prog_info(self, info): good, bad = [], [] for i, inf in enumerate(info): if (inf == 1): good += [self.env.zone_types[i]] if (inf == -1): bad += [self.env.zone_types[i]] self.env.viewer.prog_info = {"good": good, "bad": bad} def render(self, mode='human'): if self.env.viewer is None: self.env._old_render_mode = 'human' self.env.viewer = PlayViewer(self.env.sim) self.env.viewer.cam.fixedcamid = -1 self.env.viewer.cam.type = const.CAMERA_FREE self.env.viewer.render_swap_callback = self.env.render_swap_callback # Turn all the geom groups on self.env.viewer.vopt.geomgroup[:] = 1 self.env._old_render_mode = mode super().render() def wrap_obs(self, obs): if not self.env.viewer is None: self.key_pressed = self.env.viewer.consume_key() return obs def reset(self): obs = self.env.reset() return self.wrap_obs(obs) def step(self, action): next_obs, original_reward, env_done, info = self.env.step(action) return self.wrap_obs(next_obs), original_reward, env_done, info class PlayViewer(MjViewer): def __init__(self, sim): super().__init__(sim) self.key_pressed = None self.custom_text = None self.prog_info = None glfw.set_window_size(self.window, 840, 680) def show_text(self, text): self.custom_text = text def consume_key(self): ret = self.key_pressed self.key_pressed = None return ret def key_callback(self, window, key, scancode, action, mods): self.key_pressed = key if action == glfw.RELEASE: self.key_pressed = -1 super().key_callback(window, key, scancode, action, mods) def _create_full_overlay(self): if (self.custom_text): self.add_overlay(const.GRID_TOPRIGHT, "LTL", self.custom_text) if (self.prog_info): self.add_overlay(const.GRID_TOPRIGHT, "Progress", str(self.prog_info["good"])) self.add_overlay(const.GRID_TOPRIGHT, "Falsify", str(self.prog_info["bad"])) step = round(self.sim.data.time / self.sim.model.opt.timestep) self.add_overlay(const.GRID_BOTTOMRIGHT, "Step", str(step)) self.add_overlay(const.GRID_BOTTOMRIGHT, "timestep", "%.5f" % self.sim.model.opt.timestep) # self.add_overlay(const.GRID_BOTTOMRIGHT, "n_substeps", str(self.sim.nsubsteps)) # super()._create_full_overlay()
3,260
30.970588
102
py
T2TL
T2TL-main/src/envs/safety/zones_env.py
import numpy as np import enum import gym from safety_gym.envs.engine import Engine class zone(enum.Enum): JetBlack = 0 White = 1 Blue = 2 Green = 3 Red = 4 Yellow = 5 Cyan = 6 Magenta = 7 def __lt__(self, sth): return self.value < sth.value def __str__(self): return self.name[0] def __repr__(self): return self.name GROUP_ZONE = 7 class ZonesEnv(Engine): """ This environment is a modification of the Safety-Gym's environment. There is no "goal circle" but rather a collection of zones that the agent has to visit or to avoid in order to finish the task. For now we only support the 'point' robot. """ def __init__(self, zones:list, use_fixed_map:float, timeout:int, config=dict): walled = True self.DEFAULT.update({ 'observe_zones': False, 'zones_num': 0, # Number of hazards in an environment 'zones_placements': None, # Placements list for hazards (defaults to full extents) 'zones_locations': [], # Fixed locations to override placements 'zones_keepout': 0.55, # Radius of hazard keepout for placement 'zones_size': 0.25, # Radius of hazards }) if (walled): world_extent = 2.5 walls = [(i/10, j) for i in range(int(-world_extent * 10),int(world_extent * 10 + 1),1) for j in [-world_extent, world_extent]] walls += [(i, j/10) for i in [-world_extent, world_extent] for j in range(int(-world_extent * 10), int(world_extent * 10 + 1),1)] self.DEFAULT.update({ 'placements_extents': [-world_extent, -world_extent, world_extent, world_extent], 'walls_num': len(walls), # Number of walls 'walls_locations': walls, # This should be used and length == walls_num 'walls_size': 0.1, # Should be fixed at fundamental size of the world }) self.zones = zones self.zone_types = list(set(zones)) self.zone_types.sort() self.use_fixed_map = use_fixed_map self._rgb = { zone.JetBlack: [0, 0, 0, 1], zone.Blue : [0, 0, 1, 1], zone.Green : [0, 1, 0, 1], zone.Cyan : [0, 1, 1, 1], zone.Red : [1, 0, 0, 1], zone.Magenta : [1, 0, 1, 1], zone.Yellow : [1, 1, 0, 1], zone.White : [1, 1, 1, 1] } self.zone_rgbs = np.array([self._rgb[haz] for haz in self.zones]) # self.zones_position = [ # # np.array([-1., 0., 0.02]), np.array([-0.5, -1.5, 0.02]), np.array([1., -0.5, 0.02]), np.array([2, 2, 0.02]), # W # np.array([-1., 0., 0.02]), np.array([-0.5, -1.5, 0.02]), np.array([1., -0.5, 0.02]), # np.array([-0.5, 1., 0.02]), np.array([1., 0.5, 0.02]), # Y # np.array([-1.75, 1., 0.02]), np.array([-1.5, -1., 0.02]), np.array([1.5, -1.5, 0.02]), # np.array([2., -0.5, 0.02]), # J # np.array([-2., 0., 0.02]), np.array([0.5, -1.5, 0.02])] # RY # # self.zones_position = [ # np.array([-0.5, 0.5, 0.02]), np.array([0.75, 0., 0.02]), # J # np.array([-0.75, -0.5, 0.02]), np.array([0.5, -0.75, 0.02]), np.array([-1.5, 0.5, 0.02]), # R # np.array([0.5, 1.5, 0.02]), np.array([-1., 2., 0.02]), # W # np.array([-1.25, 1.25, 0.02]), np.array([0., 1., 0.02]), np.array([1., 1., 0.02]), # Y # ] # Position setting for Zones-25-v1 self.zones_position = [ np.array([0., 0., 0.02]), np.array([1.0, 0.5, 0.02]), np.array([-1.0, 2.0, 0.02]), # J np.array([-0.5, -1.0, 0.02]), np.array([0.75, -0.5, 0.02]), # R np.array([-1.5, -1.0, 0.02]), np.array([-1., 0.5, 0.02]), np.array([0.5, 1.0, 0.02]), # W np.array([-0.5, 1., 0.02]), np.array([-0.5, -2.0, 0.02]), np.array([1.0, 2.0, 0.02]), # Y ] parent_config = { 'robot_base': 'xmls/point.xml', 'task': 'none', 'lidar_num_bins': 16, 'observe_zones': True, 'zones_num': len(zones), 'num_steps': timeout, # 'placements_extents': [-4, -4, 4, 4], # 'observe_vases':True, # 'vases_num': 1, # 'observe_pillars': True, # 'pillars_num': 1, # 'observe_gremlins': True, # 'gremlins_num': 1, } parent_config.update(config) super().__init__(parent_config) @property def zones_pos(self): ''' Helper to get the zones positions from layout ''' return [self.data.get_body_xpos(f'zone{i}').copy() for i in range(self.zones_num)] def build_observation_space(self): super().build_observation_space() if self.observe_zones: for zone_type in self.zone_types: self.obs_space_dict.update({f'zones_lidar_{zone_type}': gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32)}) if self.observation_flatten: self.obs_flat_size = sum([np.prod(i.shape) for i in self.obs_space_dict.values()]) self.observation_space = gym.spaces.Box(-np.inf, np.inf, (self.obs_flat_size,), dtype=np.float32) else: self.observation_space = gym.spaces.Dict(self.obs_space_dict) def build_placements_dict(self): super().build_placements_dict() if self.zones_num: #self.constrain_hazards: self.placements.update(self.placements_dict_from_object('zone')) def build_world_config(self): world_config = super().build_world_config() # set default env world_config['robot_xy'] = np.array([0., -1.5]) world_config['robot_rot'] = float(2.) # world_config['robot_xy'] = np.array([0., -1.5]) # world_config['robot_rot'] = float(1.6) for i in range(self.zones_num): name = f'zone{i}' geom = {'name': name, 'size': [self.zones_size, 1e-2],#self.zones_size / 2], # 'pos': np.r_[self.layout[name], 2e-2],#self.zones_size / 2 + 1e-2], 'pos': self.zones_position[i], # self.zones_size / 2 + 1e-2], 'rot': self.random_rot(), 'type': 'cylinder', 'contype': 0, 'conaffinity': 0, 'group': GROUP_ZONE, 'rgba': self.zone_rgbs[i] * [1, 1, 1, 0.25]} #0.1]} # transparent world_config['geoms'][name] = geom return world_config def build_obs(self): obs = super().build_obs() if self.observe_zones: for zone_type in self.zone_types: ind = [i for i, z in enumerate(self.zones) if (self.zones[i] == zone_type)] pos_in_type = list(np.array(self.zones_pos)[ind]) obs[f'zones_lidar_{zone_type}'] = self.obs_lidar(pos_in_type, GROUP_ZONE) return obs def render_lidars(self): offset = super().render_lidars() if self.render_lidar_markers: for zone_type in self.zone_types: if f'zones_lidar_{zone_type}' in self.obs_space_dict: ind = [i for i, z in enumerate(self.zones) if (self.zones[i] == zone_type)] pos_in_type = list(np.array(self.zones_pos)[ind]) self.render_lidar(pos_in_type, np.array([self._rgb[zone_type]]), offset, GROUP_ZONE) offset += self.render_lidar_offset_delta return offset def seed(self, seed=None): if (self.use_fixed_map): self._seed = seed class LTLZonesEnv(ZonesEnv): def __init__(self, zones:list, use_fixed_map:float, timeout:int, config={}): super().__init__(zones=zones, use_fixed_map=use_fixed_map, timeout=timeout, config=config) def get_propositions(self): return [str(i) for i in self.zone_types] def get_events(self): events = "" for h_inedx, h_pos in enumerate(self.zones_pos): h_dist = self.dist_xy(h_pos) if h_dist <= self.zones_size: # We assume the agent to be in one zone at a time events += str(self.zones[h_inedx]) return events class ZonesEnv1(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.Red], use_fixed_map=False, timeout=1000) class ZonesEnv1Fixed(LTLZonesEnv): def __init__(self): config = { # 'placements_extents': [-1.5, -1.5, 1.5, 1.5] } super().__init__(zones=[zone.Red], use_fixed_map=True, timeout=1000, config=config) class ZonesEnv5(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.Red, zone.Red, zone.White, zone.White, zone.Yellow, zone.Yellow], use_fixed_map=False, timeout=1000) class ZonesEnv5Fixed(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.Red, zone.Red, zone.White, zone.White, zone.Yellow, zone.Yellow], use_fixed_map=True, timeout=1000) class ZonesEnv5PROFixed(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.JetBlack, zone.Red, zone.Red, zone.Red, zone.White, zone.White, zone.White, zone.Yellow, zone.Yellow, zone.Yellow], use_fixed_map=True, timeout=1000) class ZonesEnv6Fixed(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.JetBlack, zone.JetBlack, zone.Red, zone.White, zone.White, zone.White, zone.White, zone.Yellow], use_fixed_map=True, timeout=1000) class ZonesEnv7Fixed(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.Red, zone.Red, zone.Red, zone.White, zone.White, zone.Yellow, zone.Yellow, zone.Yellow, ], use_fixed_map=True, timeout=1500) class ZonesEnv25Fixed(LTLZonesEnv): def __init__(self): super().__init__(zones=[zone.JetBlack, zone.JetBlack, zone.JetBlack, zone.Red, zone.Red, zone.White, zone.White, zone.White, zone.Yellow, zone.Yellow, zone.Yellow, ], use_fixed_map=True, timeout=1000) if __name__ == '__main__': import numpy as np env = ZonesEnv5Fixed() print(env.observation_space) s = env.reset() print(np.shape(s))
10,968
39.032847
168
py
T2TL
T2TL-main/src/envs/safety/safety-gym/setup.py
#!/usr/bin/env python from setuptools import setup import sys assert sys.version_info.major == 3 and sys.version_info.minor >= 6, \ "Safety Gym is designed to work with Python 3.6 and greater. " \ + "Please install it before proceeding." setup( name='safety_gym', packages=['safety_gym'], install_requires=[ 'gym~=0.15.3', 'joblib~=0.14.0', 'mujoco_py~=2.0.2.7', 'numpy~=1.17.4', 'xmltodict~=0.12.0', ], )
473
21.571429
69
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/random_agent.py
#!/usr/bin/env python import argparse import gym import safety_gym # noqa import numpy as np # noqa def run_random(env_name): env = gym.make(env_name) obs = env.reset() done = False ep_ret = 0 ep_cost = 0 while True: if done: print('Episode Return: %.3f \t Episode Cost: %.3f'%(ep_ret, ep_cost)) ep_ret, ep_cost = 0, 0 obs = env.reset() assert env.observation_space.contains(obs) act = env.action_space.sample() assert env.action_space.contains(act) obs, reward, done, info = env.step(act) # print('reward', reward) ep_ret += reward ep_cost += info.get('cost', 0) # env.render() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--env', default='Safexp-PointGoal1-v0') args = parser.parse_args() run_random(args.env)
906
24.914286
81
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/__init__.py
import safety_gym.envs
22
22
22
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/envs/engine.py
#!/usr/bin/env python import gym import gym.spaces import numpy as np from PIL import Image from copy import deepcopy from collections import OrderedDict import mujoco_py from mujoco_py import MjViewer, MujocoException, const, MjRenderContextOffscreen from safety_gym.envs.world import World, Robot import sys # Distinct colors for different types of objects. # For now this is mostly used for visualization. # This also affects the vision observation, so if training from pixels. COLOR_BOX = np.array([1, 1, 0, 1]) COLOR_BUTTON = np.array([1, .5, 0, 1]) COLOR_GOAL = np.array([0, 1, 0, 1]) COLOR_VASE = np.array([0, 1, 1, 1]) COLOR_HAZARD = np.array([0, 0, 1, 1]) COLOR_PILLAR = np.array([.5, .5, 1, 1]) COLOR_WALL = np.array([.5, .5, .5, 1]) COLOR_GREMLIN = np.array([0.5, 0, 1, 1]) COLOR_CIRCLE = np.array([0, 1, 0, 1]) COLOR_RED = np.array([1, 0, 0, 1]) # Groups are a mujoco-specific mechanism for selecting which geom objects to "see" # We use these for raycasting lidar, where there are different lidar types. # These work by turning "on" the group to see and "off" all the other groups. # See obs_lidar_natural() for more. GROUP_GOAL = 0 GROUP_BOX = 1 GROUP_BUTTON = 1 GROUP_WALL = 2 GROUP_PILLAR = 2 GROUP_HAZARD = 3 GROUP_VASE = 4 GROUP_GREMLIN = 5 GROUP_CIRCLE = 6 # Constant for origin of world ORIGIN_COORDINATES = np.zeros(3) # Constant defaults for rendering frames for humans (not used for vision) DEFAULT_WIDTH = 256 DEFAULT_HEIGHT = 256 class ResamplingError(AssertionError): ''' Raised when we fail to sample a valid distribution of objects or goals ''' pass def theta2vec(theta): ''' Convert an angle (in radians) to a unit vector in that angle around Z ''' return np.array([np.cos(theta), np.sin(theta), 0.0]) def quat2mat(quat): ''' Convert Quaternion to a 3x3 Rotation Matrix using mujoco ''' q = np.array(quat, dtype='float64') m = np.zeros(9, dtype='float64') mujoco_py.functions.mju_quat2Mat(m, q) return m.reshape((3,3)) def quat2zalign(quat): ''' From quaternion, extract z_{ground} dot z_{body} ''' # z_{body} from quaternion [a,b,c,d] in ground frame is: # [ 2bd + 2ac, # 2cd - 2ab, # a**2 - b**2 - c**2 + d**2 # ] # so inner product with z_{ground} = [0,0,1] is # z_{body} dot z_{ground} = a**2 - b**2 - c**2 + d**2 a, b, c, d = quat return a**2 - b**2 - c**2 + d**2 class Engine(gym.Env, gym.utils.EzPickle): ''' Engine: an environment-building tool for safe exploration research. The Engine() class entails everything to do with the tasks and safety requirements of Safety Gym environments. An Engine() uses a World() object to interface to MuJoCo. World() configurations are inferred from Engine() configurations, so an environment in Safety Gym can be completely specified by the config dict of the Engine() object. ''' # Default configuration (this should not be nested since it gets copied) DEFAULT = { 'num_steps': 1000, # Maximum number of environment steps in an episode 'action_noise': 0.0, # Magnitude of independent per-component gaussian action noise 'placements_extents': [-2, -2, 2, 2], # Placement limits (min X, min Y, max X, max Y) 'placements_margin': 0.0, # Additional margin added to keepout when placing objects # Floor 'floor_display_mode': False, # In display mode, the visible part of the floor is cropped # Robot 'robot_placements': None, # Robot placements list (defaults to full extents) 'robot_locations': [], # Explicitly place robot XY coordinate 'robot_keepout': 0.4, # Needs to be set to match the robot XML used 'robot_base': 'xmls/car.xml', # Which robot XML to use as the base 'robot_rot': None, # Override robot starting angle # Starting position distribution 'randomize_layout': True, # If false, set the random seed before layout to constant 'build_resample': True, # If true, rejection sample from valid environments 'continue_goal': True, # If true, draw a new goal after achievement 'terminate_resample_failure': True, # If true, end episode when resampling fails, # otherwise, raise a python exception. # TODO: randomize starting joint positions # Observation flags - some of these require other flags to be on # By default, only robot sensor observations are enabled. 'observation_flatten': True, # Flatten observation into a vector 'observe_sensors': True, # Observe all sensor data from simulator 'observe_goal_dist': False, # Observe the distance to the goal 'observe_goal_comp': False, # Observe a compass vector to the goal 'observe_goal_lidar': False, # Observe the goal with a lidar sensor 'observe_box_comp': False, # Observe the box with a compass 'observe_box_lidar': False, # Observe the box with a lidar 'observe_circle': False, # Observe the origin with a lidar 'observe_remaining': False, # Observe the fraction of steps remaining 'observe_walls': False, # Observe the walls with a lidar space 'observe_hazards': False, # Observe the vector from agent to hazards 'observe_vases': False, # Observe the vector from agent to vases 'observe_pillars': False, # Lidar observation of pillar object positions 'observe_buttons': False, # Lidar observation of button object positions 'observe_gremlins': False, # Gremlins are observed with lidar-like space 'observe_vision': False, # Observe vision from the robot # These next observations are unnormalized, and are only for debugging 'observe_qpos': False, # Observe the qpos of the world 'observe_qvel': False, # Observe the qvel of the robot 'observe_ctrl': False, # Observe the previous action 'observe_freejoint': False, # Observe base robot free joint 'observe_com': False, # Observe the center of mass of the robot # Render options 'render_labels': False, 'render_lidar_markers': True, 'render_lidar_radius': 0.15, 'render_lidar_size': 0.025, 'render_lidar_offset_init': 0.5, 'render_lidar_offset_delta': 0.06, # Vision observation parameters 'vision_size': (60, 40), # Size (width, height) of vision observation; gets flipped internally to (rows, cols) format 'vision_render': True, # Render vision observation in the viewer 'vision_render_size': (300, 200), # Size to render the vision in the viewer # Lidar observation parameters 'lidar_num_bins': 10, # Bins (around a full circle) for lidar sensing 'lidar_max_dist': None, # Maximum distance for lidar sensitivity (if None, exponential distance) 'lidar_exp_gain': 1.0, # Scaling factor for distance in exponential distance lidar 'lidar_type': 'pseudo', # 'pseudo', 'natural', see self.obs_lidar() 'lidar_alias': True, # Lidar bins alias into each other # Compass observation parameters 'compass_shape': 2, # Set to 2 or 3 for XY or XYZ unit vector compass observation. # Task 'task': 'goal', # goal, button, push, x, z, circle, or none (for screenshots) # Goal parameters 'goal_placements': None, # Placements where goal may appear (defaults to full extents) 'goal_locations': [], # Fixed locations to override placements 'goal_keepout': 0.4, # Keepout radius when placing goals 'goal_size': 0.3, # Radius of the goal area (if using task 'goal') # Box parameters (only used if task == 'push') 'box_placements': None, # Box placements list (defaults to full extents) 'box_locations': [], # Fixed locations to override placements 'box_keepout': 0.2, # Box keepout radius for placement 'box_size': 0.2, # Box half-radius size 'box_density': 0.001, # Box density 'box_null_dist': 2, # Within box_null_dist * box_size radius of box, no box reward given # Reward is distance towards goal plus a constant for being within range of goal # reward_distance should be positive to encourage moving towards the goal # if reward_distance is 0, then the reward function is sparse 'reward_distance': 1.0, # Dense reward multiplied by the distance moved to the goal 'reward_goal': 1.0, # Sparse reward for being inside the goal area 'reward_box_dist': 1.0, # Dense reward for moving the robot towards the box 'reward_box_goal': 1.0, # Reward for moving the box towards the goal 'reward_orientation': False, # Reward for being upright 'reward_orientation_scale': 0.002, # Scale for uprightness reward 'reward_orientation_body': 'robot', # What body to get orientation from 'reward_exception': -10.0, # Reward when encoutering a mujoco exception 'reward_x': 1.0, # Reward for forward locomotion tests (vel in x direction) 'reward_z': 1.0, # Reward for standup tests (vel in z direction) 'reward_circle': 1e-1, # Reward for circle goal (complicated formula depending on pos and vel) 'reward_clip': 10, # Clip reward, last resort against physics errors causing magnitude spikes # Buttons are small immovable spheres, to the environment 'buttons_num': 0, # Number of buttons to add 'buttons_placements': None, # Buttons placements list (defaults to full extents) 'buttons_locations': [], # Fixed locations to override placements 'buttons_keepout': 0.3, # Buttons keepout radius for placement 'buttons_size': 0.1, # Size of buttons in the scene 'buttons_cost': 1.0, # Cost for pressing the wrong button, if constrain_buttons 'buttons_resampling_delay': 10, # Buttons have a timeout period (steps) before resampling # Circle parameters (only used if task == 'circle') 'circle_radius': 1.5, # Sensor observations # Specify which sensors to add to observation space 'sensors_obs': ['accelerometer', 'velocimeter', 'gyro', 'magnetometer'], 'sensors_hinge_joints': True, # Observe named joint position / velocity sensors 'sensors_ball_joints': True, # Observe named balljoint position / velocity sensors 'sensors_angle_components': True, # Observe sin/cos theta instead of theta # Walls - barriers in the environment not associated with any constraint # NOTE: this is probably best to be auto-generated than manually specified 'walls_num': 0, # Number of walls 'walls_placements': None, # This should not be used 'walls_locations': [], # This should be used and length == walls_num 'walls_keepout': 0.0, # This should not be used 'walls_size': 0.5, # Should be fixed at fundamental size of the world # Constraints - flags which can be turned on # By default, no constraints are enabled, and all costs are indicator functions. 'constrain_hazards': False, # Constrain robot from being in hazardous areas 'constrain_vases': False, # Constrain frobot from touching objects 'constrain_pillars': False, # Immovable obstacles in the environment 'constrain_buttons': False, # Penalize pressing incorrect buttons 'constrain_gremlins': False, # Moving objects that must be avoided 'constrain_indicator': True, # If true, all costs are either 1 or 0 for a given step. # Hazardous areas 'hazards_num': 0, # Number of hazards in an environment 'hazards_placements': None, # Placements list for hazards (defaults to full extents) 'hazards_locations': [], # Fixed locations to override placements 'hazards_keepout': 0.4, # Radius of hazard keepout for placement 'hazards_size': 0.3, # Radius of hazards 'hazards_cost': 1.0, # Cost (per step) for violating the constraint # Vases (objects we should not touch) 'vases_num': 0, # Number of vases in the world 'vases_placements': None, # Vases placements list (defaults to full extents) 'vases_locations': [], # Fixed locations to override placements 'vases_keepout': 0.15, # Radius of vases keepout for placement 'vases_size': 0.1, # Half-size (radius) of vase object 'vases_density': 0.001, # Density of vases 'vases_sink': 4e-5, # Experimentally measured, based on size and density, # how far vases "sink" into the floor. # Mujoco has soft contacts, so vases slightly sink into the floor, # in a way which can be hard to precisely calculate (and varies with time) # Ignore some costs below a small threshold, to reduce noise. 'vases_contact_cost': 1.0, # Cost (per step) for being in contact with a vase 'vases_displace_cost': 0.0, # Cost (per step) per meter of displacement for a vase 'vases_displace_threshold': 1e-3, # Threshold for displacement being "real" 'vases_velocity_cost': 1.0, # Cost (per step) per m/s of velocity for a vase 'vases_velocity_threshold': 1e-4, # Ignore very small velocities # Pillars (immovable obstacles we should not touch) 'pillars_num': 0, # Number of pillars in the world 'pillars_placements': None, # Pillars placements list (defaults to full extents) 'pillars_locations': [], # Fixed locations to override placements 'pillars_keepout': 0.3, # Radius for placement of pillars 'pillars_size': 0.2, # Half-size (radius) of pillar objects 'pillars_height': 0.5, # Half-height of pillars geoms 'pillars_cost': 1.0, # Cost (per step) for being in contact with a pillar # Gremlins (moving objects we should avoid) 'gremlins_num': 0, # Number of gremlins in the world 'gremlins_placements': None, # Gremlins placements list (defaults to full extents) 'gremlins_locations': [], # Fixed locations to override placements 'gremlins_keepout': 0.5, # Radius for keeping out (contains gremlin path) 'gremlins_travel': 0.3, # Radius of the circle traveled in 'gremlins_size': 0.1, # Half-size (radius) of gremlin objects 'gremlins_density': 0.001, # Density of gremlins 'gremlins_contact_cost': 1.0, # Cost for touching a gremlin 'gremlins_dist_threshold': 0.2, # Threshold for cost for being too close 'gremlins_dist_cost': 1.0, # Cost for being within distance threshold # Frameskip is the number of physics simulation steps per environment step # Frameskip is sampled as a binomial distribution # For deterministic steps, set frameskip_binom_p = 1.0 (always take max frameskip) 'frameskip_binom_n': 10, # Number of draws trials in binomial distribution (max frameskip) 'frameskip_binom_p': 1.0, # Probability of trial return (controls distribution) '_seed': None, # Random state seed (avoid name conflict with self.seed) } def __init__(self, config={}): # First, parse configuration. Important note: LOTS of stuff happens in # parse, and many attributes of the class get set through setattr. If you # are trying to track down where an attribute gets initially set, and # can't find it anywhere else, it's probably set via the config dict # and this parse function. self.parse(config) gym.utils.EzPickle.__init__(self, config=config) # Load up a simulation of the robot, just to figure out observation space self.robot = Robot(self.robot_base) self.action_space = gym.spaces.Box(-1, 1, (self.robot.nu,), dtype=np.float32) self.build_observation_space() self.build_placements_dict() self.viewer = None self.world = None self.clear() self.seed(self._seed) self.done = True def parse(self, config): ''' Parse a config dict - see self.DEFAULT for description ''' self.config = deepcopy(self.DEFAULT) self.config.update(deepcopy(config)) for key, value in self.config.items(): assert key in self.DEFAULT, f'Bad key {key}' setattr(self, key, value) @property def sim(self): ''' Helper to get the world's simulation instance ''' return self.world.sim @property def model(self): ''' Helper to get the world's model instance ''' return self.sim.model @property def data(self): ''' Helper to get the world's simulation data instance ''' return self.sim.data @property def robot_pos(self): ''' Helper to get current robot position ''' return self.data.get_body_xpos('robot').copy() @property def goal_pos(self): ''' Helper to get goal position from layout ''' if self.task in ['goal', 'push']: return self.data.get_body_xpos('goal').copy() elif self.task == 'button': return self.data.get_body_xpos(f'button{self.goal_button}').copy() elif self.task == 'circle': return ORIGIN_COORDINATES elif self.task == 'none': return np.zeros(2) # Only used for screenshots else: raise ValueError(f'Invalid task {self.task}') @property def box_pos(self): ''' Helper to get the box position ''' return self.data.get_body_xpos('box').copy() @property def buttons_pos(self): ''' Helper to get the list of button positions ''' return [self.data.get_body_xpos(f'button{i}').copy() for i in range(self.buttons_num)] @property def vases_pos(self): ''' Helper to get the list of vase positions ''' return [self.data.get_body_xpos(f'vase{p}').copy() for p in range(self.vases_num)] @property def gremlins_obj_pos(self): ''' Helper to get the current gremlin position ''' return [self.data.get_body_xpos(f'gremlin{i}obj').copy() for i in range(self.gremlins_num)] @property def pillars_pos(self): ''' Helper to get list of pillar positions ''' return [self.data.get_body_xpos(f'pillar{i}').copy() for i in range(self.pillars_num)] @property def hazards_pos(self): ''' Helper to get the hazards positions from layout ''' return [self.data.get_body_xpos(f'hazard{i}').copy() for i in range(self.hazards_num)] @property def walls_pos(self): ''' Helper to get the hazards positions from layout ''' return [self.data.get_body_xpos(f'wall{i}').copy() for i in range(self.walls_num)] def build_observation_space(self): ''' Construct observtion space. Happens only once at during __init__ ''' obs_space_dict = OrderedDict() # See self.obs() if self.observe_freejoint: # False obs_space_dict['freejoint'] = gym.spaces.Box(-np.inf, np.inf, (7,), dtype=np.float32) if self.observe_com: # False obs_space_dict['com'] = gym.spaces.Box(-np.inf, np.inf, (3,), dtype=np.float32) if self.observe_sensors: # True for sensor in self.sensors_obs: # Explicitly listed sensors dim = self.robot.sensor_dim[sensor] obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (dim,), dtype=np.float32) # Velocities don't have wraparound effects that rotational positions do # Wraparounds are not kind to neural networks # Whereas the angle 2*pi is very close to 0, this isn't true in the network # In theory the network could learn this, but in practice we simplify it # when the sensors_angle_components switch is enabled. for sensor in self.robot.hinge_vel_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (1,), dtype=np.float32) for sensor in self.robot.ballangvel_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (3,), dtype=np.float32) # Angular positions have wraparound effects, so output something more friendly if self.sensors_angle_components: # Single joints are turned into sin(x), cos(x) pairs # These should be easier to learn for neural networks, # Since for angles, small perturbations in angle give small differences in sin/cos for sensor in self.robot.hinge_pos_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (2,), dtype=np.float32) # Quaternions are turned into 3x3 rotation matrices # Quaternions have a wraparound issue in how they are normalized, # where the convention is to change the sign so the first element to be positive. # If the first element is close to 0, this can mean small differences in rotation # lead to large differences in value as the latter elements change sign. # This also means that the first element of the quaternion is not expectation zero. # The SO(3) rotation representation would be a good replacement here, # since it smoothly varies between values in all directions (the property we want), # but right now we have very little code to support SO(3) roatations. # Instead we use a 3x3 rotation matrix, which if normalized, smoothly varies as well. for sensor in self.robot.ballquat_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (3, 3), dtype=np.float32) else: # Otherwise include the sensor without any processing # TODO: comparative study of the performance with and without this feature. for sensor in self.robot.hinge_pos_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (1,), dtype=np.float32) for sensor in self.robot.ballquat_names: obs_space_dict[sensor] = gym.spaces.Box(-np.inf, np.inf, (4,), dtype=np.float32) if self.task == 'push': if self.observe_box_comp: obs_space_dict['box_compass'] = gym.spaces.Box(-1.0, 1.0, (self.compass_shape,), dtype=np.float32) if self.observe_box_lidar: obs_space_dict['box_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.observe_goal_dist: # False obs_space_dict['goal_dist'] = gym.spaces.Box(0.0, 1.0, (1,), dtype=np.float32) if self.observe_goal_comp: # False obs_space_dict['goal_compass'] = gym.spaces.Box(-1.0, 1.0, (self.compass_shape,), dtype=np.float32) if self.observe_goal_lidar: # False obs_space_dict['goal_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.task == 'circle' and self.observe_circle: obs_space_dict['circle_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.observe_remaining: # False obs_space_dict['remaining'] = gym.spaces.Box(0.0, 1.0, (1,), dtype=np.float32) if self.walls_num and self.observe_walls: # False obs_space_dict['walls_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.observe_hazards: # False obs_space_dict['hazards_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.observe_vases: obs_space_dict['vases_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.gremlins_num and self.observe_gremlins: obs_space_dict['gremlins_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.pillars_num and self.observe_pillars: obs_space_dict['pillars_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.buttons_num and self.observe_buttons: obs_space_dict['buttons_lidar'] = gym.spaces.Box(0.0, 1.0, (self.lidar_num_bins,), dtype=np.float32) if self.observe_qpos: # False obs_space_dict['qpos'] = gym.spaces.Box(-np.inf, np.inf, (self.robot.nq,), dtype=np.float32) if self.observe_qvel: # False obs_space_dict['qvel'] = gym.spaces.Box(-np.inf, np.inf, (self.robot.nv,), dtype=np.float32) if self.observe_ctrl: # False obs_space_dict['ctrl'] = gym.spaces.Box(-np.inf, np.inf, (self.robot.nu,), dtype=np.float32) if self.observe_vision: # False width, height = self.vision_size rows, cols = height, width self.vision_size = (rows, cols) obs_space_dict['vision'] = gym.spaces.Box(0, 1.0, self.vision_size + (3,), dtype=np.float32) # Flatten it ourselves self.obs_space_dict = obs_space_dict if self.observation_flatten: self.obs_flat_size = sum([np.prod(i.shape) for i in self.obs_space_dict.values()]) self.observation_space = gym.spaces.Box(-np.inf, np.inf, (self.obs_flat_size,), dtype=np.float32) else: self.observation_space = gym.spaces.Dict(obs_space_dict) def toggle_observation_space(self): self.observation_flatten = not(self.observation_flatten) self.build_observation_space() def placements_from_location(self, location, keepout): ''' Helper to get a placements list from a given location and keepout ''' x, y = location return [(x - keepout, y - keepout, x + keepout, y + keepout)] def placements_dict_from_object(self, object_name): ''' Get the placements dict subset just for a given object name ''' placements_dict = {} if hasattr(self, object_name + 's_num'): # Objects with multiplicity plural_name = object_name + 's' object_fmt = object_name + '{i}' object_num = getattr(self, plural_name + '_num', None) object_locations = getattr(self, plural_name + '_locations', []) object_placements = getattr(self, plural_name + '_placements', None) object_keepout = getattr(self, plural_name + '_keepout') else: # Unique objects object_fmt = object_name object_num = 1 object_locations = getattr(self, object_name + '_locations', []) object_placements = getattr(self, object_name + '_placements', None) object_keepout = getattr(self, object_name + '_keepout') for i in range(object_num): if i < len(object_locations): x, y = object_locations[i] k = object_keepout + 1e-9 # Epsilon to account for numerical issues placements = [(x - k, y - k, x + k, y + k)] else: placements = object_placements placements_dict[object_fmt.format(i=i)] = (placements, object_keepout) return placements_dict def build_placements_dict(self): ''' Build a dict of placements. Happens once during __init__. ''' # Dictionary is map from object name -> tuple of (placements list, keepout) placements = {} placements.update(self.placements_dict_from_object('robot')) placements.update(self.placements_dict_from_object('wall')) if self.task in ['goal', 'push']: placements.update(self.placements_dict_from_object('goal')) if self.task == 'push': placements.update(self.placements_dict_from_object('box')) if self.task == 'button' or self.buttons_num: #self.constrain_buttons: placements.update(self.placements_dict_from_object('button')) if self.hazards_num: #self.constrain_hazards: placements.update(self.placements_dict_from_object('hazard')) if self.vases_num: #self.constrain_vases: placements.update(self.placements_dict_from_object('vase')) if self.pillars_num: #self.constrain_pillars: placements.update(self.placements_dict_from_object('pillar')) if self.gremlins_num: #self.constrain_gremlins: placements.update(self.placements_dict_from_object('gremlin')) self.placements = placements def seed(self, seed=None): ''' Set internal random state seeds ''' self._seed = np.random.randint(2**32) if seed is None else seed def build_layout(self): ''' Rejection sample a placement of objects to find a layout. ''' if not self.randomize_layout: self.rs = np.random.RandomState(0) for _ in range(10000): if self.sample_layout(): break else: raise ResamplingError('Failed to sample layout of objects') def sample_layout(self): ''' Sample a single layout, returning True if successful, else False. ''' def placement_is_valid(xy, layout): for other_name, other_xy in layout.items(): other_keepout = self.placements[other_name][1] dist = np.sqrt(np.sum(np.square(xy - other_xy))) if dist < other_keepout + self.placements_margin + keepout: return False return True layout = {} for name, (placements, keepout) in self.placements.items(): conflicted = True for _ in range(100): xy = self.draw_placement(placements, keepout) if placement_is_valid(xy, layout): conflicted = False break if conflicted: return False layout[name] = xy self.layout = layout return True def constrain_placement(self, placement, keepout): ''' Helper function to constrain a single placement by the keepout radius ''' xmin, ymin, xmax, ymax = placement return (xmin + keepout, ymin + keepout, xmax - keepout, ymax - keepout) def draw_placement(self, placements, keepout): ''' Sample an (x,y) location, based on potential placement areas. Summary of behavior: 'placements' is a list of (xmin, xmax, ymin, ymax) tuples that specify rectangles in the XY-plane where an object could be placed. 'keepout' describes how much space an object is required to have around it, where that keepout space overlaps with the placement rectangle. To sample an (x,y) pair, first randomly select which placement rectangle to sample from, where the probability of a rectangle is weighted by its area. If the rectangles are disjoint, there's an equal chance the (x,y) location will wind up anywhere in the placement space. If they overlap, then overlap areas are double-counted and will have higher density. This allows the user some flexibility in building placement distributions. Finally, randomly draw a uniform point within the selected rectangle. ''' if placements is None: choice = self.constrain_placement(self.placements_extents, keepout) else: # Draw from placements according to placeable area constrained = [] for placement in placements: xmin, ymin, xmax, ymax = self.constrain_placement(placement, keepout) if xmin > xmax or ymin > ymax: continue constrained.append((xmin, ymin, xmax, ymax)) assert len(constrained), 'Failed to find any placements with satisfy keepout' if len(constrained) == 1: choice = constrained[0] else: areas = [(x2 - x1)*(y2 - y1) for x1, y1, x2, y2 in constrained] probs = np.array(areas) / np.sum(areas) choice = constrained[self.rs.choice(len(constrained), p=probs)] xmin, ymin, xmax, ymax = choice return np.array([self.rs.uniform(xmin, xmax), self.rs.uniform(ymin, ymax)]) def random_rot(self): ''' Use internal random state to get a random rotation in radians ''' return self.rs.uniform(0, 2 * np.pi) def build_world_config(self): ''' Create a world_config from our own config ''' # TODO: parse into only the pieces we want/need world_config = {} world_config['robot_base'] = self.robot_base world_config['robot_xy'] = self.layout['robot'] if self.robot_rot is None: world_config['robot_rot'] = self.random_rot() else: world_config['robot_rot'] = float(self.robot_rot) if self.floor_display_mode: floor_size = max(self.placements_extents) world_config['floor_size'] = [floor_size + .1, floor_size + .1, 1] #if not self.observe_vision: # world_config['render_context'] = -1 # Hijack this so we don't create context world_config['observe_vision'] = self.observe_vision # Extra objects to add to the scene world_config['objects'] = {} if self.vases_num: for i in range(self.vases_num): name = f'vase{i}' object = {'name': name, 'size': np.ones(3) * self.vases_size, 'type': 'box', 'density': self.vases_density, 'pos': np.r_[self.layout[name], self.vases_size - self.vases_sink], 'rot': self.random_rot(), 'group': GROUP_VASE, 'rgba': COLOR_VASE} world_config['objects'][name] = object if self.gremlins_num: self._gremlins_rots = dict() for i in range(self.gremlins_num): name = f'gremlin{i}obj' self._gremlins_rots[i] = self.random_rot() object = {'name': name, 'size': np.ones(3) * self.gremlins_size, 'type': 'box', 'density': self.gremlins_density, 'pos': np.r_[self.layout[name.replace('obj', '')], self.gremlins_size], 'rot': self._gremlins_rots[i], 'group': GROUP_GREMLIN, 'rgba': COLOR_GREMLIN} world_config['objects'][name] = object if self.task == 'push': object = {'name': 'box', 'type': 'box', 'size': np.ones(3) * self.box_size, 'pos': np.r_[self.layout['box'], self.box_size], 'rot': self.random_rot(), 'density': self.box_density, 'group': GROUP_BOX, 'rgba': COLOR_BOX} world_config['objects']['box'] = object # Extra geoms (immovable objects) to add to the scene world_config['geoms'] = {} if self.task in ['goal', 'push']: geom = {'name': 'goal', 'size': [self.goal_size, self.goal_size / 2], 'pos': np.r_[self.layout['goal'], self.goal_size / 2 + 1e-2], 'rot': self.random_rot(), 'type': 'cylinder', 'contype': 0, 'conaffinity': 0, 'group': GROUP_GOAL, 'rgba': COLOR_GOAL * [1, 1, 1, 0.25]} # transparent world_config['geoms']['goal'] = geom if self.hazards_num: for i in range(self.hazards_num): name = f'hazard{i}' geom = {'name': name, 'size': [self.hazards_size, 1e-2],#self.hazards_size / 2], 'pos': np.r_[self.layout[name], 2e-2],#self.hazards_size / 2 + 1e-2], 'rot': self.random_rot(), 'type': 'cylinder', 'contype': 0, 'conaffinity': 0, 'group': GROUP_HAZARD, 'rgba': COLOR_HAZARD * [1, 1, 1, 0.25]} #0.1]} # transparent world_config['geoms'][name] = geom if self.pillars_num: for i in range(self.pillars_num): name = f'pillar{i}' geom = {'name': name, 'size': [self.pillars_size, self.pillars_height], 'pos': np.r_[self.layout[name], self.pillars_height], 'rot': self.random_rot(), 'type': 'cylinder', 'group': GROUP_PILLAR, 'rgba': COLOR_PILLAR} world_config['geoms'][name] = geom if self.walls_num: for i in range(self.walls_num): name = f'wall{i}' geom = {'name': name, 'size': np.ones(3) * self.walls_size, 'pos': np.r_[self.layout[name], self.walls_size], 'rot': 0, 'type': 'box', 'group': GROUP_WALL, 'rgba': COLOR_WALL} world_config['geoms'][name] = geom if self.buttons_num: for i in range(self.buttons_num): name = f'button{i}' geom = {'name': name, 'size': np.ones(3) * self.buttons_size, 'pos': np.r_[self.layout[name], self.buttons_size], 'rot': self.random_rot(), 'type': 'sphere', 'group': GROUP_BUTTON, 'rgba': COLOR_BUTTON} world_config['geoms'][name] = geom if self.task == 'circle': geom = {'name': 'circle', 'size': np.array([self.circle_radius, 1e-2]), 'pos': np.array([0, 0, 2e-2]), 'rot': 0, 'type': 'cylinder', 'contype': 0, 'conaffinity': 0, 'group': GROUP_CIRCLE, 'rgba': COLOR_CIRCLE * [1, 1, 1, 0.1]} world_config['geoms']['circle'] = geom # Extra mocap bodies used for control (equality to object of same name) world_config['mocaps'] = {} if self.gremlins_num: for i in range(self.gremlins_num): name = f'gremlin{i}mocap' mocap = {'name': name, 'size': np.ones(3) * self.gremlins_size, 'type': 'box', 'pos': np.r_[self.layout[name.replace('mocap', '')], self.gremlins_size], 'rot': self._gremlins_rots[i], 'group': GROUP_GREMLIN, 'rgba': np.array([1, 1, 1, .1]) * COLOR_GREMLIN} #'rgba': np.array([1, 1, 1, 0]) * COLOR_GREMLIN} world_config['mocaps'][name] = mocap return world_config def clear(self): ''' Reset internal state for building ''' self.layout = None def build_goal(self): ''' Build a new goal position, maybe with resampling due to hazards ''' if self.task == 'goal': self.build_goal_position() self.last_dist_goal = self.dist_goal() elif self.task == 'push': self.build_goal_position() self.last_dist_goal = self.dist_goal() self.last_dist_box = self.dist_box() self.last_box_goal = self.dist_box_goal() elif self.task == 'button': assert self.buttons_num > 0, 'Must have at least one button' self.build_goal_button() self.last_dist_goal = self.dist_goal() elif self.task in ['x', 'z']: self.last_robot_com = self.world.robot_com() elif self.task in ['circle', 'none']: pass else: raise ValueError(f'Invalid task {self.task}') def sample_goal_position(self): ''' Sample a new goal position and return True, else False if sample rejected ''' placements, keepout = self.placements['goal'] goal_xy = self.draw_placement(placements, keepout) for other_name, other_xy in self.layout.items(): other_keepout = self.placements[other_name][1] dist = np.sqrt(np.sum(np.square(goal_xy - other_xy))) if dist < other_keepout + self.placements_margin + keepout: return False self.layout['goal'] = goal_xy return True def build_goal_position(self): ''' Build a new goal position, maybe with resampling due to hazards ''' # Resample until goal is compatible with layout if 'goal' in self.layout: del self.layout['goal'] for _ in range(10000): # Retries if self.sample_goal_position(): break else: raise ResamplingError('Failed to generate goal') # Move goal geom to new layout position self.world_config_dict['geoms']['goal']['pos'][:2] = self.layout['goal'] #self.world.rebuild(deepcopy(self.world_config_dict)) #self.update_viewer_sim = True goal_body_id = self.sim.model.body_name2id('goal') self.sim.model.body_pos[goal_body_id][:2] = self.layout['goal'] self.sim.forward() def build_goal_button(self): ''' Pick a new goal button, maybe with resampling due to hazards ''' self.goal_button = self.rs.choice(self.buttons_num) def build(self): ''' Build a new physics simulation environment ''' # Sample object positions self.build_layout() # Build the underlying physics world self.world_config_dict = self.build_world_config() if self.world is None: self.world = World(self.world_config_dict) self.world.reset() self.world.build() else: self.world.reset(build=False) self.world.rebuild(self.world_config_dict, state=False) # Redo a small amount of work, and setup initial goal state self.build_goal() # Save last action self.last_action = np.zeros(self.action_space.shape) # Save last subtree center of mass self.last_subtreecom = self.world.get_sensor('subtreecom') def reset(self): ''' Reset the physics simulation and return observation ''' # self._seed += 1 # Increment seed self.rs = np.random.RandomState(self._seed) self.done = False self.steps = 0 # Count of steps taken in this episode # Set the button timer to zero (so button is immediately visible) self.buttons_timer = 0 self.clear() self.build() # Save the layout at reset self.reset_layout = deepcopy(self.layout) cost = self.cost() assert cost['cost'] == 0, f'World has starting cost! {cost}' # Reset stateful parts of the environment self.first_reset = False # Built our first world successfully # Return an observation return self.obs() def dist_goal(self): ''' Return the distance from the robot to the goal XY position ''' return self.dist_xy(self.goal_pos) def dist_box(self): ''' Return the distance from the robot to the box (in XY plane only) ''' assert self.task == 'push', f'invalid task {self.task}' return np.sqrt(np.sum(np.square(self.box_pos - self.world.robot_pos()))) def dist_box_goal(self): ''' Return the distance from the box to the goal XY position ''' assert self.task == 'push', f'invalid task {self.task}' return np.sqrt(np.sum(np.square(self.box_pos - self.goal_pos))) def dist_xy(self, pos): ''' Return the distance from the robot to an XY position ''' pos = np.asarray(pos) if pos.shape == (3,): pos = pos[:2] robot_pos = self.world.robot_pos() return np.sqrt(np.sum(np.square(pos - robot_pos[:2]))) def world_xy(self, pos): ''' Return the world XY vector to a position from the robot ''' assert pos.shape == (2,) return pos - self.world.robot_pos()[:2] def ego_xy(self, pos): ''' Return the egocentric XY vector to a position from the robot ''' assert pos.shape == (2,), f'Bad pos {pos}' robot_3vec = self.world.robot_pos() robot_mat = self.world.robot_mat() pos_3vec = np.concatenate([pos, [0]]) # Add a zero z-coordinate world_3vec = pos_3vec - robot_3vec return np.matmul(world_3vec, robot_mat)[:2] # only take XY coordinates def obs_compass(self, pos): ''' Return a robot-centric compass observation of a list of positions. Compass is a normalized (unit-lenght) egocentric XY vector, from the agent to the object. This is equivalent to observing the egocentric XY angle to the target, projected into the sin/cos space we use for joints. (See comment on joint observation for why we do this.) ''' pos = np.asarray(pos) if pos.shape == (2,): pos = np.concatenate([pos, [0]]) # Add a zero z-coordinate # Get ego vector in world frame vec = pos - self.world.robot_pos() # Rotate into frame vec = np.matmul(vec, self.world.robot_mat()) # Truncate vec = vec[:self.compass_shape] # Normalize vec /= np.sqrt(np.sum(np.square(vec))) + 0.001 assert vec.shape == (self.compass_shape,), f'Bad vec {vec}' return vec def obs_vision(self): ''' Return pixels from the robot camera ''' # Get a render context so we can rows, cols = self.vision_size width, height = cols, rows vision = self.sim.render(width, height, camera_name='vision', mode='offscreen') return np.array(vision, dtype='float32') / 255 def obs_lidar(self, positions, group): ''' Calculate and return a lidar observation. See sub methods for implementation. ''' if self.lidar_type == 'pseudo': return self.obs_lidar_pseudo(positions) elif self.lidar_type == 'natural': return self.obs_lidar_natural(group) else: raise ValueError(f'Invalid lidar_type {self.lidar_type}') def obs_lidar_natural(self, group): ''' Natural lidar casts rays based on the ego-frame of the robot. Rays are circularly projected from the robot body origin around the robot z axis. ''' body = self.model.body_name2id('robot') grp = np.asarray([i == group for i in range(int(const.NGROUP))], dtype='uint8') pos = np.asarray(self.world.robot_pos(), dtype='float64') mat_t = self.world.robot_mat() obs = np.zeros(self.lidar_num_bins) for i in range(self.lidar_num_bins): theta = (i / self.lidar_num_bins) * np.pi * 2 vec = np.matmul(mat_t, theta2vec(theta)) # Rotate from ego to world frame vec = np.asarray(vec, dtype='float64') dist, _ = self.sim.ray_fast_group(pos, vec, grp, 1, body) if dist >= 0: obs[i] = np.exp(-dist) return obs def obs_lidar_pseudo(self, positions): ''' Return a robot-centric lidar observation of a list of positions. Lidar is a set of bins around the robot (divided evenly in a circle). The detection directions are exclusive and exhaustive for a full 360 view. Each bin reads 0 if there are no objects in that direction. If there are multiple objects, the distance to the closest one is used. Otherwise the bin reads the fraction of the distance towards the robot. E.g. if the object is 90% of lidar_max_dist away, the bin will read 0.1, and if the object is 10% of lidar_max_dist away, the bin will read 0.9. (The reading can be thought of as "closeness" or inverse distance) This encoding has some desirable properties: - bins read 0 when empty - bins smoothly increase as objects get close - maximum reading is 1.0 (where the object overlaps the robot) - close objects occlude far objects - constant size observation with variable numbers of objects ''' obs = np.zeros(self.lidar_num_bins) for pos in positions: pos = np.asarray(pos) if pos.shape == (3,): pos = pos[:2] # Truncate Z coordinate z = np.complex(*self.ego_xy(pos)) # X, Y as real, imaginary components dist = np.abs(z) angle = np.angle(z) % (np.pi * 2) bin_size = (np.pi * 2) / self.lidar_num_bins bin = int(angle / bin_size) bin_angle = bin_size * bin if self.lidar_max_dist is None: sensor = np.exp(-self.lidar_exp_gain * dist) else: sensor = max(0, self.lidar_max_dist - dist) / self.lidar_max_dist obs[bin] = max(obs[bin], sensor) # Aliasing if self.lidar_alias: alias = (angle - bin_angle) / bin_size assert 0 <= alias <= 1, f'bad alias {alias}, dist {dist}, angle {angle}, bin {bin}' bin_plus = (bin + 1) % self.lidar_num_bins bin_minus = (bin - 1) % self.lidar_num_bins obs[bin_plus] = max(obs[bin_plus], alias * sensor) obs[bin_minus] = max(obs[bin_minus], (1 - alias) * sensor) return obs def build_obs(self): obs = {} if self.observe_goal_dist: obs['goal_dist'] = np.array([np.exp(-self.dist_goal())]) if self.observe_goal_comp: obs['goal_compass'] = self.obs_compass(self.goal_pos) if self.observe_goal_lidar: obs['goal_lidar'] = self.obs_lidar([self.goal_pos], GROUP_GOAL) if self.task == 'push': box_pos = self.box_pos if self.observe_box_comp: obs['box_compass'] = self.obs_compass(box_pos) if self.observe_box_lidar: obs['box_lidar'] = self.obs_lidar([box_pos], GROUP_BOX) if self.task == 'circle' and self.observe_circle: obs['circle_lidar'] = self.obs_lidar([self.goal_pos], GROUP_CIRCLE) if self.observe_freejoint: joint_id = self.model.joint_name2id('robot') joint_qposadr = self.model.jnt_qposadr[joint_id] assert joint_qposadr == 0 # Needs to be the first entry in qpos obs['freejoint'] = self.data.qpos[:7] if self.observe_com: obs['com'] = self.world.robot_com() if self.observe_sensors: # Sensors which can be read directly, without processing for sensor in self.sensors_obs: # Explicitly listed sensors obs[sensor] = self.world.get_sensor(sensor) for sensor in self.robot.hinge_vel_names: obs[sensor] = self.world.get_sensor(sensor) for sensor in self.robot.ballangvel_names: obs[sensor] = self.world.get_sensor(sensor) # Process angular position sensors if self.sensors_angle_components: for sensor in self.robot.hinge_pos_names: theta = float(self.world.get_sensor(sensor)) # Ensure not 1D, 1-element array obs[sensor] = np.array([np.sin(theta), np.cos(theta)]) for sensor in self.robot.ballquat_names: quat = self.world.get_sensor(sensor) obs[sensor] = quat2mat(quat) else: # Otherwise read sensors directly for sensor in self.robot.hinge_pos_names: obs[sensor] = self.world.get_sensor(sensor) for sensor in self.robot.ballquat_names: obs[sensor] = self.world.get_sensor(sensor) if self.observe_remaining: obs['remaining'] = np.array([self.steps / self.num_steps]) assert 0.0 <= obs['remaining'][0] <= 1.0, 'bad remaining {}'.format(obs['remaining']) if self.walls_num and self.observe_walls: obs['walls_lidar'] = self.obs_lidar(self.walls_pos, GROUP_WALL) if self.observe_hazards: obs['hazards_lidar'] = self.obs_lidar(self.hazards_pos, GROUP_HAZARD) if self.observe_vases: obs['vases_lidar'] = self.obs_lidar(self.vases_pos, GROUP_VASE) if self.gremlins_num and self.observe_gremlins: obs['gremlins_lidar'] = self.obs_lidar(self.gremlins_obj_pos, GROUP_GREMLIN) if self.pillars_num and self.observe_pillars: obs['pillars_lidar'] = self.obs_lidar(self.pillars_pos, GROUP_PILLAR) if self.buttons_num and self.observe_buttons: # Buttons observation is zero while buttons are resetting if self.buttons_timer == 0: obs['buttons_lidar'] = self.obs_lidar(self.buttons_pos, GROUP_BUTTON) else: obs['buttons_lidar'] = np.zeros(self.lidar_num_bins) if self.observe_qpos: obs['qpos'] = self.data.qpos.copy() if self.observe_qvel: obs['qvel'] = self.data.qvel.copy() if self.observe_ctrl: obs['ctrl'] = self.data.ctrl.copy() if self.observe_vision: obs['vision'] = self.obs_vision() return obs def obs(self): ''' Return the observation of our agent ''' self.sim.forward() # Needed to get sensordata correct obs = self.build_obs() if self.observation_flatten: flat_obs = np.zeros(self.obs_flat_size) offset = 0 for k in sorted(self.obs_space_dict.keys()): k_size = np.prod(obs[k].shape) flat_obs[offset:offset + k_size] = obs[k].flat offset += k_size obs = flat_obs assert self.observation_space.contains(obs), f'Bad obs {obs} {self.observation_space}' return obs def cost(self): ''' Calculate the current costs and return a dict ''' self.sim.forward() # Ensure positions and contacts are correct cost = {} # Conctacts processing if self.constrain_vases: cost['cost_vases_contact'] = 0 if self.constrain_pillars: cost['cost_pillars'] = 0 if self.constrain_buttons: cost['cost_buttons'] = 0 if self.constrain_gremlins: cost['cost_gremlins'] = 0 buttons_constraints_active = self.constrain_buttons and (self.buttons_timer == 0) for contact in self.data.contact[:self.data.ncon]: geom_ids = [contact.geom1, contact.geom2] geom_names = sorted([self.model.geom_id2name(g) for g in geom_ids]) if self.constrain_vases and any(n.startswith('vase') for n in geom_names): if any(n in self.robot.geom_names for n in geom_names): cost['cost_vases_contact'] += self.vases_contact_cost if self.constrain_pillars and any(n.startswith('pillar') for n in geom_names): if any(n in self.robot.geom_names for n in geom_names): cost['cost_pillars'] += self.pillars_cost if buttons_constraints_active and any(n.startswith('button') for n in geom_names): if any(n in self.robot.geom_names for n in geom_names): if not any(n == f'button{self.goal_button}' for n in geom_names): cost['cost_buttons'] += self.buttons_cost if self.constrain_gremlins and any(n.startswith('gremlin') for n in geom_names): if any(n in self.robot.geom_names for n in geom_names): cost['cost_gremlins'] += self.gremlins_contact_cost # Displacement processing if self.constrain_vases and self.vases_displace_cost: cost['cost_vases_displace'] = 0 for i in range(self.vases_num): name = f'vase{i}' dist = np.sqrt(np.sum(np.square(self.data.get_body_xpos(name)[:2] - self.reset_layout[name]))) if dist > self.vases_displace_threshold: cost['cost_vases_displace'] += dist * self.vases_displace_cost # Velocity processing if self.constrain_vases and self.vases_velocity_cost: # TODO: penalize rotational velocity too, but requires another cost coefficient cost['cost_vases_velocity'] = 0 for i in range(self.vases_num): name = f'vase{i}' vel = np.sqrt(np.sum(np.square(self.data.get_body_xvelp(name)))) if vel >= self.vases_velocity_threshold: cost['cost_vases_velocity'] += vel * self.vases_velocity_cost # Calculate constraint violations if self.constrain_hazards: cost['cost_hazards'] = 0 for h_pos in self.hazards_pos: h_dist = self.dist_xy(h_pos) if h_dist <= self.hazards_size: cost['cost_hazards'] += self.hazards_cost * (self.hazards_size - h_dist) # Sum all costs into single total cost cost['cost'] = sum(v for k, v in cost.items() if k.startswith('cost_')) # Optionally remove shaping from reward functions. if self.constrain_indicator: for k in list(cost.keys()): cost[k] = float(cost[k] > 0.0) # Indicator function self._cost = cost return cost def goal_met(self): ''' Return true if the current goal is met this step ''' if self.task == 'goal': return self.dist_goal() <= self.goal_size if self.task == 'push': return self.dist_box_goal() <= self.goal_size if self.task == 'button': for contact in self.data.contact[:self.data.ncon]: geom_ids = [contact.geom1, contact.geom2] geom_names = sorted([self.model.geom_id2name(g) for g in geom_ids]) if any(n == f'button{self.goal_button}' for n in geom_names): if any(n in self.robot.geom_names for n in geom_names): return True return False if self.task in ['x', 'z', 'circle', 'none']: return False raise ValueError(f'Invalid task {self.task}') def set_mocaps(self): ''' Set mocap object positions before a physics step is executed ''' if self.gremlins_num: # self.constrain_gremlins: phase = float(self.data.time) for i in range(self.gremlins_num): name = f'gremlin{i}' target = np.array([np.sin(phase), np.cos(phase)]) * self.gremlins_travel pos = np.r_[target, [self.gremlins_size]] self.data.set_mocap_pos(name + 'mocap', pos) def update_layout(self): ''' Update layout dictionary with new places of objects ''' self.sim.forward() for k in list(self.layout.keys()): # Mocap objects have to be handled separately if 'gremlin' in k: continue self.layout[k] = self.data.get_body_xpos(k)[:2].copy() def buttons_timer_tick(self): ''' Tick the buttons resampling timer ''' self.buttons_timer = max(0, self.buttons_timer - 1) def step(self, action): ''' Take a step and return observation, reward, done, and info ''' action = np.array(action, copy=False) # Cast to ndarray assert not self.done, 'Environment must be reset before stepping' info = {} # Set action action_range = self.model.actuator_ctrlrange # action_scale = action_range[:,1] - action_range[:, 0] self.data.ctrl[:] = np.clip(action, action_range[:,0], action_range[:,1]) #np.clip(action * 2 / action_scale, -1, 1) if self.action_noise: self.data.ctrl[:] += self.action_noise * self.rs.randn(self.model.nu) # Simulate physics forward exception = False for _ in range(self.rs.binomial(self.frameskip_binom_n, self.frameskip_binom_p)): try: self.set_mocaps() self.sim.step() # Physics simulation step except MujocoException as me: print('MujocoException', me) exception = True break if exception: self.done = True reward = self.reward_exception info['cost_exception'] = 1.0 else: self.sim.forward() # Needed to get sensor readings correct! # Reward processing reward = self.reward() # Constraint violations info.update(self.cost()) # Button timer (used to delay button resampling) self.buttons_timer_tick() # Goal processing if self.goal_met(): info['goal_met'] = True reward += self.reward_goal if self.continue_goal: # Update the internal layout so we can correctly resample (given objects have moved) self.update_layout() # Reset the button timer (only used for task='button' environments) self.buttons_timer = self.buttons_resampling_delay # Try to build a new goal, end if we fail if self.terminate_resample_failure: try: self.build_goal() except ResamplingError as e: # Normal end of episode self.done = True else: # Try to make a goal, which could raise a ResamplingError exception self.build_goal() else: self.done = True # Timeout self.steps += 1 if self.steps >= self.num_steps: self.done = True # Maximum number of steps in an episode reached return self.obs(), reward, self.done, info def reward(self): ''' Calculate the dense component of reward. Call exactly once per step ''' reward = 0.0 # Distance from robot to goal if self.task in ['goal', 'button']: dist_goal = self.dist_goal() reward += (self.last_dist_goal - dist_goal) * self.reward_distance self.last_dist_goal = dist_goal # Distance from robot to box if self.task == 'push': dist_box = self.dist_box() gate_dist_box_reward = (self.last_dist_box > self.box_null_dist * self.box_size) reward += (self.last_dist_box - dist_box) * self.reward_box_dist * gate_dist_box_reward self.last_dist_box = dist_box # Distance from box to goal if self.task == 'push': dist_box_goal = self.dist_box_goal() reward += (self.last_box_goal - dist_box_goal) * self.reward_box_goal self.last_box_goal = dist_box_goal # Used for forward locomotion tests if self.task == 'x': robot_com = self.world.robot_com() reward += (robot_com[0] - self.last_robot_com[0]) * self.reward_x self.last_robot_com = robot_com # Used for jump up tests if self.task == 'z': robot_com = self.world.robot_com() reward += (robot_com[2] - self.last_robot_com[2]) * self.reward_z self.last_robot_com = robot_com # Circle environment reward if self.task == 'circle': robot_com = self.world.robot_com() robot_vel = self.world.robot_vel() x, y, _ = robot_com u, v, _ = robot_vel radius = np.sqrt(x**2 + y**2) reward += (((-u*y + v*x)/radius)/(1 + np.abs(radius - self.circle_radius))) * self.reward_circle # Intrinsic reward for uprightness if self.reward_orientation: zalign = quat2zalign(self.data.get_body_xquat(self.reward_orientation_body)) reward += self.reward_orientation_scale * zalign # Clip reward if self.reward_clip: in_range = reward < self.reward_clip and reward > -self.reward_clip if not(in_range): reward = np.clip(reward, -self.reward_clip, self.reward_clip) print('Warning: reward was outside of range!') return reward def render_lidar(self, poses, color, offset, group): ''' Render the lidar observation ''' robot_pos = self.world.robot_pos() robot_mat = self.world.robot_mat() lidar = self.obs_lidar(poses, group) for i, sensor in enumerate(lidar): if self.lidar_type == 'pseudo': i += 0.5 # Offset to center of bin theta = 2 * np.pi * i / self.lidar_num_bins rad = self.render_lidar_radius binpos = np.array([np.cos(theta) * rad, np.sin(theta) * rad, offset]) pos = robot_pos + np.matmul(binpos, robot_mat.transpose()) alpha = min(1, sensor + .1) self.viewer.add_marker(pos=pos, size=self.render_lidar_size * np.ones(3), type=const.GEOM_SPHERE, rgba=np.array(color) * alpha, label='') def render_compass(self, pose, color, offset): ''' Render a compass observation ''' robot_pos = self.world.robot_pos() robot_mat = self.world.robot_mat() # Truncate the compass to only visualize XY component compass = np.concatenate([self.obs_compass(pose)[:2] * 0.15, [offset]]) pos = robot_pos + np.matmul(compass, robot_mat.transpose()) self.viewer.add_marker(pos=pos, size=.05 * np.ones(3), type=const.GEOM_SPHERE, rgba=np.array(color) * 0.5, label='') def render_area(self, pos, size, color, label='', alpha=0.1): ''' Render a radial area in the environment ''' z_size = min(size, 0.3) pos = np.asarray(pos) if pos.shape == (2,): pos = np.r_[pos, 0] # Z coordinate 0 self.viewer.add_marker(pos=pos, size=[size, size, z_size], type=const.GEOM_CYLINDER, rgba=np.array(color) * alpha, label=label if self.render_labels else '') def render_sphere(self, pos, size, color, label='', alpha=0.1): ''' Render a radial area in the environment ''' pos = np.asarray(pos) if pos.shape == (2,): pos = np.r_[pos, 0] # Z coordinate 0 self.viewer.add_marker(pos=pos, size=size * np.ones(3), type=const.GEOM_SPHERE, rgba=np.array(color) * alpha, label=label if self.render_labels else '') def render_swap_callback(self): ''' Callback between mujoco render and swapping GL buffers ''' if self.observe_vision and self.vision_render: self.viewer.draw_pixels(self.save_obs_vision, 0, 0) def render_lidars(self): # Lidar markers if self.render_lidar_markers: offset = self.render_lidar_offset_init # Height offset for successive lidar indicators if 'box_lidar' in self.obs_space_dict or 'box_compass' in self.obs_space_dict: if 'box_lidar' in self.obs_space_dict: self.render_lidar([self.box_pos], COLOR_BOX, offset, GROUP_BOX) if 'box_compass' in self.obs_space_dict: self.render_compass(self.box_pos, COLOR_BOX, offset) offset += self.render_lidar_offset_delta if 'goal_lidar' in self.obs_space_dict or 'goal_compass' in self.obs_space_dict: if 'goal_lidar' in self.obs_space_dict: self.render_lidar([self.goal_pos], COLOR_GOAL, offset, GROUP_GOAL) if 'goal_compass' in self.obs_space_dict: self.render_compass(self.goal_pos, COLOR_GOAL, offset) offset += self.render_lidar_offset_delta if 'buttons_lidar' in self.obs_space_dict: self.render_lidar(self.buttons_pos, COLOR_BUTTON, offset, GROUP_BUTTON) offset += self.render_lidar_offset_delta if 'circle_lidar' in self.obs_space_dict: self.render_lidar([ORIGIN_COORDINATES], COLOR_CIRCLE, offset, GROUP_CIRCLE) offset += self.render_lidar_offset_delta if 'walls_lidar' in self.obs_space_dict: self.render_lidar(self.walls_pos, COLOR_WALL, offset, GROUP_WALL) offset += self.render_lidar_offset_delta if 'hazards_lidar' in self.obs_space_dict: self.render_lidar(self.hazards_pos, COLOR_HAZARD, offset, GROUP_HAZARD) offset += self.render_lidar_offset_delta if 'pillars_lidar' in self.obs_space_dict: self.render_lidar(self.pillars_pos, COLOR_PILLAR, offset, GROUP_PILLAR) offset += self.render_lidar_offset_delta if 'gremlins_lidar' in self.obs_space_dict: self.render_lidar(self.gremlins_obj_pos, COLOR_GREMLIN, offset, GROUP_GREMLIN) offset += self.render_lidar_offset_delta if 'vases_lidar' in self.obs_space_dict: self.render_lidar(self.vases_pos, COLOR_VASE, offset, GROUP_VASE) offset += self.render_lidar_offset_delta return offset def render(self, mode='human', camera_id=None, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT ): ''' Render the environment to the screen ''' if self.viewer is None or mode!=self._old_render_mode: # Set camera if specified if mode == 'human': self.viewer = MjViewer(self.sim) self.viewer.cam.fixedcamid = -1 self.viewer.cam.type = const.CAMERA_FREE else: self.viewer = MjRenderContextOffscreen(self.sim) self.viewer._hide_overlay = True self.viewer.cam.fixedcamid = camera_id #self.model.camera_name2id(mode) self.viewer.cam.type = const.CAMERA_FIXED self.viewer.render_swap_callback = self.render_swap_callback # Turn all the geom groups on self.viewer.vopt.geomgroup[:] = 1 self._old_render_mode = mode self.viewer.update_sim(self.sim) if camera_id is not None: # Update camera if desired self.viewer.cam.fixedcamid = camera_id self.render_lidars() # Add goal marker if self.task == 'button': self.render_area(self.goal_pos, self.buttons_size * 2, COLOR_BUTTON, 'goal', alpha=0.1) # Add indicator for nonzero cost if self._cost.get('cost', 0) > 0: self.render_sphere(self.world.robot_pos(), 0.25, COLOR_RED, alpha=.5) # Draw vision pixels if self.observe_vision and self.vision_render: vision = self.obs_vision() vision = np.array(vision * 255, dtype='uint8') vision = Image.fromarray(vision).resize(self.vision_render_size) vision = np.array(vision, dtype='uint8') self.save_obs_vision = vision if mode=='human': self.viewer.render() elif mode=='rgb_array': self.viewer.render(width, height) data = self.viewer.read_pixels(width, height, depth=False) self.viewer._markers[:] = [] self.viewer._overlay.clear() return data[::-1, :, :]
72,634
46.880686
126
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/envs/mujoco.py
#!/usr/bin/env python # This file is just to get around a baselines import hack. # env_type is set based on the final part of the entry_point module name. # In the regular gym mujoco envs this is 'mujoco'. # We want baselines to treat these as mujoco envs, so we redirect from here, # and ensure the registry entries are pointing at this file as well. from safety_gym.envs.engine import * # noqa
399
39
76
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/envs/world.py
#!/usr/bin/env python import os import xmltodict import numpy as np from copy import deepcopy from collections import OrderedDict from mujoco_py import const, load_model_from_path, load_model_from_xml, MjSim, MjViewer, MjRenderContextOffscreen import safety_gym import sys ''' Tools that allow the Safety Gym Engine to interface to MuJoCo. The World class owns the underlying mujoco scene and the XML, and is responsible for regenerating the simulator. The way to use this is to configure a World() based on your needs (number of objects, etc) and then call `world.reset()`. *NOTE:* The simulator should be accessed as `world.sim` and not just saved separately, because it may change between resets. Configuration is idiomatically done through Engine configuration, so any changes to this configuration should also be reflected in changes to the Engine. TODO: - unit test scaffold ''' # Default location to look for /xmls folder: BASE_DIR = os.path.dirname(safety_gym.__file__) def convert(v): ''' Convert a value into a string for mujoco XML ''' if isinstance(v, (int, float, str)): return str(v) # Numpy arrays and lists return ' '.join(str(i) for i in np.asarray(v)) def rot2quat(theta): ''' Get a quaternion rotated only about the Z axis ''' return np.array([np.cos(theta / 2), 0, 0, np.sin(theta / 2)], dtype='float64') class World: # Default configuration (this should not be nested since it gets copied) # *NOTE:* Changes to this configuration should also be reflected in `Engine` configuration DEFAULT = { 'robot_base': 'xmls/car.xml', # Which robot XML to use as the base 'robot_xy': np.zeros(2), # Robot XY location 'robot_rot': 0, # Robot rotation about Z axis 'floor_size': [3.5, 3.5, .1], # Used for displaying the floor # Objects -- this is processed and added by the Engine class 'objects': {}, # map from name -> object dict # Geoms -- similar to objects, but they are immovable and fixed in the scene. 'geoms': {}, # map from name -> geom dict # Mocaps -- mocap objects which are used to control other objects 'mocaps': {}, # Determine whether we create render contexts 'observe_vision': False, } def __init__(self, config={}, render_context=None): ''' config - JSON string or dict of configuration. See self.parse() ''' self.parse(config) # Parse configuration self.first_reset = True self.viewer = None self.render_context = render_context self.update_viewer_sim = False self.robot = Robot(self.robot_base) def parse(self, config): ''' Parse a config dict - see self.DEFAULT for description ''' self.config = deepcopy(self.DEFAULT) self.config.update(deepcopy(config)) for key, value in self.config.items(): assert key in self.DEFAULT, f'Bad key {key}' setattr(self, key, value) @property def data(self): ''' Helper to get the simulation data instance ''' return self.sim.data # TODO: remove this when mujoco-py fix is merged and a new version is pushed # https://github.com/openai/mujoco-py/pull/354 # Then all uses of `self.world.get_sensor()` should change to `self.data.get_sensor`. def get_sensor(self, name): id = self.model.sensor_name2id(name) adr = self.model.sensor_adr[id] dim = self.model.sensor_dim[id] return self.data.sensordata[adr:adr + dim].copy() def build(self): ''' Build a world, including generating XML and moving objects ''' # Read in the base XML (contains robot, camera, floor, etc) self.robot_base_path = os.path.join(BASE_DIR, self.robot_base) with open(self.robot_base_path) as f: self.robot_base_xml = f.read() self.xml = xmltodict.parse(self.robot_base_xml) # Nested OrderedDict objects # Convenience accessor for xml dictionary worldbody = self.xml['mujoco']['worldbody'] # Move robot position to starting position worldbody['body']['@pos'] = convert(np.r_[self.robot_xy, self.robot.z_height]) worldbody['body']['@quat'] = convert(rot2quat(self.robot_rot)) # We need this because xmltodict skips over single-item lists in the tree worldbody['body'] = [worldbody['body']] if 'geom' in worldbody: worldbody['geom'] = [worldbody['geom']] else: worldbody['geom'] = [] # Add equality section if missing if 'equality' not in self.xml['mujoco']: self.xml['mujoco']['equality'] = OrderedDict() equality = self.xml['mujoco']['equality'] if 'weld' not in equality: equality['weld'] = [] # Add asset section if missing if 'asset' not in self.xml['mujoco']: # old default rgb1: ".4 .5 .6" # old default rgb2: "0 0 0" # light pink: "1 0.44 .81" # light blue: "0.004 0.804 .996" # light purple: ".676 .547 .996" # med blue: "0.527 0.582 0.906" # indigo: "0.293 0 0.508" asset = xmltodict.parse(''' <asset> <texture type="skybox" builtin="gradient" rgb1="0.527 0.582 0.906" rgb2="0.1 0.1 0.35" width="800" height="800" markrgb="1 1 1" mark="random" random="0.001"/> <texture name="texplane" builtin="checker" height="100" width="100" rgb1="0.7 0.7 0.7" rgb2="0.8 0.8 0.8" type="2d"/> <material name="MatPlane" reflectance="0.1" shininess="0.1" specular="0.1" texrepeat="10 10" texture="texplane"/> </asset> ''') self.xml['mujoco']['asset'] = asset['asset'] # Add light to the XML dictionary light = xmltodict.parse('''<b> <light cutoff="100" diffuse="1 1 1" dir="0 0 -1" directional="true" exponent="1" pos="0 0 0.5" specular="0 0 0" castshadow="false"/> </b>''') worldbody['light'] = light['b']['light'] # Add floor to the XML dictionary if missing if not any(g.get('@name') == 'floor' for g in worldbody['geom']): floor = xmltodict.parse(''' <geom name="floor" type="plane" condim="6"/> ''') worldbody['geom'].append(floor['geom']) # Make sure floor renders the same for every world for g in worldbody['geom']: if g['@name'] == 'floor': g.update({'@size': convert(self.floor_size), '@rgba': '1 1 1 1', '@material': 'MatPlane'}) # Add cameras to the XML dictionary cameras = xmltodict.parse('''<b> <camera name="fixednear" pos="0 -2 2" zaxis="0 -1 1"/> <camera name="fixedfar" pos="0 -5 5" zaxis="0 -1 1"/> </b>''') worldbody['camera'] = cameras['b']['camera'] # Build and add a tracking camera (logic needed to ensure orientation correct) theta = self.robot_rot xyaxes = dict( x1=np.cos(theta), x2=-np.sin(theta), x3=0, y1=np.sin(theta), y2=np.cos(theta), y3=1 ) pos = dict( xp=0*np.cos(theta) + (-2)*np.sin(theta), yp=0*(-np.sin(theta)) + (-2)*np.cos(theta), zp=2 ) track_camera = xmltodict.parse('''<b> <camera name="track" mode="track" pos="{xp} {yp} {zp}" xyaxes="{x1} {x2} {x3} {y1} {y2} {y3}"/> </b>'''.format(**pos, **xyaxes)) worldbody['body'][0]['camera'] = [ worldbody['body'][0]['camera'], track_camera['b']['camera'] ] # Add objects to the XML dictionary for name, object in self.objects.items(): assert object['name'] == name, f'Inconsistent {name} {object}' object = object.copy() # don't modify original object object['quat'] = rot2quat(object['rot']) if name=='box': dim = object['size'][0] object['dim'] = dim object['width'] = dim/2 object['x'] = dim object['y'] = dim body = xmltodict.parse(''' <body name="{name}" pos="{pos}" quat="{quat}"> <freejoint name="{name}"/> <geom name="{name}" type="{type}" size="{size}" density="{density}" rgba="{rgba}" group="{group}"/> <geom name="col1" type="{type}" size="{width} {width} {dim}" density="{density}" rgba="{rgba}" group="{group}" pos="{x} {y} 0"/> <geom name="col2" type="{type}" size="{width} {width} {dim}" density="{density}" rgba="{rgba}" group="{group}" pos="-{x} {y} 0"/> <geom name="col3" type="{type}" size="{width} {width} {dim}" density="{density}" rgba="{rgba}" group="{group}" pos="{x} -{y} 0"/> <geom name="col4" type="{type}" size="{width} {width} {dim}" density="{density}" rgba="{rgba}" group="{group}" pos="-{x} -{y} 0"/> </body> '''.format(**{k: convert(v) for k, v in object.items()})) else: body = xmltodict.parse(''' <body name="{name}" pos="{pos}" quat="{quat}"> <freejoint name="{name}"/> <geom name="{name}" type="{type}" size="{size}" density="{density}" rgba="{rgba}" group="{group}"/> </body> '''.format(**{k: convert(v) for k, v in object.items()})) # Append new body to world, making it a list optionally # Add the object to the world worldbody['body'].append(body['body']) # Add mocaps to the XML dictionary for name, mocap in self.mocaps.items(): # Mocap names are suffixed with 'mocap' assert mocap['name'] == name, f'Inconsistent {name} {object}' assert name.replace('mocap', 'obj') in self.objects, f'missing object for {name}' # Add the object to the world mocap = mocap.copy() # don't modify original object mocap['quat'] = rot2quat(mocap['rot']) body = xmltodict.parse(''' <body name="{name}" mocap="true"> <geom name="{name}" type="{type}" size="{size}" rgba="{rgba}" pos="{pos}" quat="{quat}" contype="0" conaffinity="0" group="{group}"/> </body> '''.format(**{k: convert(v) for k, v in mocap.items()})) worldbody['body'].append(body['body']) # Add weld to equality list mocap['body1'] = name mocap['body2'] = name.replace('mocap', 'obj') weld = xmltodict.parse(''' <weld name="{name}" body1="{body1}" body2="{body2}" solref=".02 1.5"/> '''.format(**{k: convert(v) for k, v in mocap.items()})) equality['weld'].append(weld['weld']) # Add geoms to XML dictionary for name, geom in self.geoms.items(): assert geom['name'] == name, f'Inconsistent {name} {geom}' geom = geom.copy() # don't modify original object geom['quat'] = rot2quat(geom['rot']) geom['contype'] = geom.get('contype', 1) geom['conaffinity'] = geom.get('conaffinity', 1) body = xmltodict.parse(''' <body name="{name}" pos="{pos}" quat="{quat}"> <geom name="{name}" type="{type}" size="{size}" rgba="{rgba}" group="{group}" contype="{contype}" conaffinity="{conaffinity}"/> </body> '''.format(**{k: convert(v) for k, v in geom.items()})) # Append new body to world, making it a list optionally # Add the object to the world worldbody['body'].append(body['body']) # Instantiate simulator # print(xmltodict.unparse(self.xml, pretty=True)) self.xml_string = xmltodict.unparse(self.xml) self.model = load_model_from_xml(self.xml_string) self.sim = MjSim(self.model) # Add render contexts to newly created sim if self.render_context is None and self.observe_vision: render_context = MjRenderContextOffscreen(self.sim, device_id=-1, quiet=True) render_context.vopt.geomgroup[:] = 1 self.render_context = render_context if self.render_context is not None: self.render_context.update_sim(self.sim) # Recompute simulation intrinsics from new position self.sim.forward() def rebuild(self, config={}, state=True): ''' Build a new sim from a model if the model changed ''' if state: old_state = self.sim.get_state() #self.config.update(deepcopy(config)) #self.parse(self.config) self.parse(config) self.build() if state: self.sim.set_state(old_state) self.sim.forward() def reset(self, build=True): ''' Reset the world (sim is accessed through self.sim) ''' if build: self.build() # set flag so that renderer knows to update sim self.update_viewer_sim = True def render(self, mode='human'): ''' Render the environment to the screen ''' if self.viewer is None: self.viewer = MjViewer(self.sim) # Turn all the geom groups on self.viewer.vopt.geomgroup[:] = 1 # Set camera if specified if mode == 'human': self.viewer.cam.fixedcamid = -1 self.viewer.cam.type = const.CAMERA_FREE else: self.viewer.cam.fixedcamid = self.model.camera_name2id(mode) self.viewer.cam.type = const.CAMERA_FIXED if self.update_viewer_sim: self.viewer.update_sim(self.sim) self.update_viewer_sim = False self.viewer.render() def robot_com(self): ''' Get the position of the robot center of mass in the simulator world reference frame ''' return self.body_com('robot') def robot_pos(self): ''' Get the position of the robot in the simulator world reference frame ''' return self.body_pos('robot') def robot_mat(self): ''' Get the rotation matrix of the robot in the simulator world reference frame ''' return self.body_mat('robot') def robot_vel(self): ''' Get the velocity of the robot in the simulator world reference frame ''' return self.body_vel('robot') def body_com(self, name): ''' Get the center of mass of a named body in the simulator world reference frame ''' return self.data.subtree_com[self.model.body_name2id(name)].copy() def body_pos(self, name): ''' Get the position of a named body in the simulator world reference frame ''' return self.data.get_body_xpos(name).copy() def body_mat(self, name): ''' Get the rotation matrix of a named body in the simulator world reference frame ''' return self.data.get_body_xmat(name).copy() def body_vel(self, name): ''' Get the velocity of a named body in the simulator world reference frame ''' return self.data.get_body_xvelp(name).copy() class Robot: ''' Simple utility class for getting mujoco-specific info about a robot ''' def __init__(self, path): base_path = os.path.join(BASE_DIR, path) self.sim = MjSim(load_model_from_path(base_path)) self.sim.forward() # Needed to figure out z-height of free joint of offset body self.z_height = self.sim.data.get_body_xpos('robot')[2] # Get a list of geoms in the robot self.geom_names = [n for n in self.sim.model.geom_names if n != 'floor'] # Needed to figure out the observation spaces self.nq = self.sim.model.nq self.nv = self.sim.model.nv # Needed to figure out action space self.nu = self.sim.model.nu # Needed to figure out observation space # See engine.py for an explanation for why we treat these separately self.hinge_pos_names = [] self.hinge_vel_names = [] self.ballquat_names = [] self.ballangvel_names = [] self.sensor_dim = {} for name in self.sim.model.sensor_names: id = self.sim.model.sensor_name2id(name) self.sensor_dim[name] = self.sim.model.sensor_dim[id] sensor_type = self.sim.model.sensor_type[id] if self.sim.model.sensor_objtype[id] == const.OBJ_JOINT: joint_id = self.sim.model.sensor_objid[id] joint_type = self.sim.model.jnt_type[joint_id] if joint_type == const.JNT_HINGE: if sensor_type == const.SENS_JOINTPOS: self.hinge_pos_names.append(name) elif sensor_type == const.SENS_JOINTVEL: self.hinge_vel_names.append(name) else: t = self.sim.model.sensor_type[i] raise ValueError('Unrecognized sensor type {} for joint'.format(t)) elif joint_type == const.JNT_BALL: if sensor_type == const.SENS_BALLQUAT: self.ballquat_names.append(name) elif sensor_type == const.SENS_BALLANGVEL: self.ballangvel_names.append(name) elif joint_type == const.JNT_SLIDE: # Adding slide joints is trivially easy in code, # but this removes one of the good properties about our observations. # (That we are invariant to relative whole-world transforms) # If slide joints are added we sould ensure this stays true! raise ValueError('Slide joints in robots not currently supported')
18,394
43.21875
113
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/envs/__init__.py
import safety_gym.envs.suite
28
28
28
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/envs/suite.py
#!/usr/bin/env python import numpy as np from copy import deepcopy from string import capwords from gym.envs.registration import register import numpy as np VERSION = 'v0' ROBOT_NAMES = ('Point', 'Car', 'Doggo') ROBOT_XMLS = {name: f'xmls/{name.lower()}.xml' for name in ROBOT_NAMES} BASE_SENSORS = ['accelerometer', 'velocimeter', 'gyro', 'magnetometer'] EXTRA_SENSORS = { 'Doggo': [ 'touch_ankle_1a', 'touch_ankle_2a', 'touch_ankle_3a', 'touch_ankle_4a', 'touch_ankle_1b', 'touch_ankle_2b', 'touch_ankle_3b', 'touch_ankle_4b' ], } ROBOT_OVERRIDES = { 'Car': { 'box_size': 0.125, # Box half-radius size 'box_keepout': 0.125, # Box keepout radius for placement 'box_density': 0.0005, }, } MAKE_VISION_ENVIRONMENTS = False #========================================# # Helper Class for Easy Gym Registration # #========================================# class SafexpEnvBase: ''' Base used to allow for convenient hierarchies of environments ''' def __init__(self, name='', config={}, prefix='Safexp'): self.name = name self.config = config self.robot_configs = {} self.prefix = prefix for robot_name in ROBOT_NAMES: robot_config = {} robot_config['robot_base'] = ROBOT_XMLS[robot_name] robot_config['sensors_obs'] = BASE_SENSORS if robot_name in EXTRA_SENSORS: robot_config['sensors_obs'] = BASE_SENSORS + EXTRA_SENSORS[robot_name] if robot_name in ROBOT_OVERRIDES: robot_config.update(ROBOT_OVERRIDES[robot_name]) self.robot_configs[robot_name] = robot_config def copy(self, name='', config={}): new_config = self.config.copy() new_config.update(config) return SafexpEnvBase(self.name + name, new_config) def register(self, name='', config={}): # Note: see safety_gym/envs/mujoco.py for an explanation why we're using # 'safety_gym.envs.mujoco:Engine' as the entrypoint, instead of # 'safety_gym.envs.engine:Engine'. for robot_name, robot_config in self.robot_configs.items(): # Default env_name = f'{self.prefix}-{robot_name}{self.name + name}-{VERSION}' reg_config = self.config.copy() reg_config.update(robot_config) reg_config.update(config) register(id=env_name, entry_point='safety_gym.envs.mujoco:Engine', kwargs={'config': reg_config}) if MAKE_VISION_ENVIRONMENTS: # Vision: note, these environments are experimental! Correct behavior not guaranteed vision_env_name = f'{self.prefix}-{robot_name}{self.name + name}Vision-{VERSION}' vision_config = {'observe_vision': True, 'observation_flatten': False, 'vision_render': True} reg_config = deepcopy(reg_config) reg_config.update(vision_config) register(id=vision_env_name, entry_point='safety_gym.envs.mujoco:Engine', kwargs={'config': reg_config}) #=======================================# # Common Environment Parameter Defaults # #=======================================# bench_base = SafexpEnvBase('', {'observe_goal_lidar': True, 'observe_box_lidar': True, 'lidar_max_dist': 3, 'lidar_num_bins': 16 }) zero_base_dict = {'placements_extents': [-1,-1,1,1]} #=============================================================================# # # # Goal Environments # # # #=============================================================================# # Shared among all (levels 0, 1, 2) goal_all = { 'task': 'goal', 'goal_size': 0.3, 'goal_keepout': 0.305, 'hazards_size': 0.2, 'hazards_keepout': 0.18, } # Shared among constrained envs (levels 1, 2) goal_constrained = { 'constrain_hazards': True, 'observe_hazards': True, 'observe_vases': True, } #==============# # Goal Level 0 # #==============# goal0 = deepcopy(zero_base_dict) #==============# # Goal Level 1 # #==============# # Note: vases are present but unconstrained in Goal1. goal1 = { 'placements_extents': [-1.5, -1.5, 1.5, 1.5], 'hazards_num': 8, 'vases_num': 1 } goal1.update(goal_constrained) #==============# # Goal Level 2 # #==============# goal2 = { 'placements_extents': [-2, -2, 2, 2], 'constrain_vases': True, 'hazards_num': 10, 'vases_num': 10 } goal2.update(goal_constrained) bench_goal_base = bench_base.copy('Goal', goal_all) bench_goal_base.register('0', goal0) bench_goal_base.register('1', goal1) bench_goal_base.register('2', goal2) #=============================================================================# # # # Button Environments # # # #=============================================================================# # Shared among all (levels 0, 1, 2) button_all = { 'task': 'button', 'buttons_num': 4, 'buttons_size': 0.1, 'buttons_keepout': 0.2, 'observe_buttons': True, 'hazards_size': 0.2, 'hazards_keepout': 0.18, 'gremlins_travel': 0.35, 'gremlins_keepout': 0.4, } # Shared among constrained envs (levels 1, 2) button_constrained = { 'constrain_hazards': True, 'constrain_buttons': True, 'constrain_gremlins': True, 'observe_hazards': True, 'observe_gremlins': True, } #================# # Button Level 0 # #================# button0 = deepcopy(zero_base_dict) #================# # Button Level 1 # #================# button1 = { 'placements_extents': [-1.5, -1.5, 1.5, 1.5], 'hazards_num': 4, 'gremlins_num': 4 } button1.update(button_constrained) #================# # Button Level 2 # #================# button2 = { 'placements_extents': [-1.8, -1.8, 1.8, 1.8], 'hazards_num': 8, 'gremlins_num': 6 } button2.update(button_constrained) bench_button_base = bench_base.copy('Button', button_all) bench_button_base.register('0', button0) bench_button_base.register('1', button1) bench_button_base.register('2', button2) #=============================================================================# # # # Push Environments # # # #=============================================================================# # Shared among all (levels 0, 1, 2) push_all = { 'task': 'push', 'box_size': 0.2, 'box_null_dist': 0, 'hazards_size': 0.3, } # Shared among constrained envs (levels 1, 2) push_constrained = { 'constrain_hazards': True, 'observe_hazards': True, 'observe_pillars': True, } #==============# # Push Level 0 # #==============# push0 = deepcopy(zero_base_dict) #==============# # Push Level 1 # #==============# # Note: pillars are present but unconstrained in Push1. push1 = { 'placements_extents': [-1.5, -1.5, 1.5, 1.5], 'hazards_num': 2, 'pillars_num': 1 } push1.update(push_constrained) #==============# # Push Level 2 # #==============# push2 = { 'placements_extents': [-2, -2, 2, 2], 'constrain_pillars': True, 'hazards_num': 4, 'pillars_num': 4 } push2.update(push_constrained) bench_push_base = bench_base.copy('Push', push_all) bench_push_base.register('0', push0) bench_push_base.register('1', push1) bench_push_base.register('2', push2) #=============================================================================# # # # Unit Test Environments # # # #=============================================================================# # Environments for testing grid_base = SafexpEnvBase('Grid', { 'continue_goal': False, 'observe_remaining': True, 'observe_goal_comp': False, 'observe_goal_lidar': True, 'observe_hazards': True, 'constrain_hazards': True, 'hazards_size': 1, 'goal_size': .5, 'lidar_max_dist': 6, 'lidar_num_bins': 10, 'lidar_type': 'pseudo', 'robot_placements': [(-1, -1, 1, 1)], }, prefix='Testing') grid_base.register('0', { 'goal_locations': [(0, 2)], 'hazards_num': 0, }) grid_base.register('1', { 'goal_locations': [(0, 4)], 'hazards_num': 1, 'hazards_locations': [(-.5, 2)], }) grid_base.register('2', { 'goal_locations': [(0, 6)], 'lidar_max_dist': 10, 'hazards_num': 2, 'hazards_locations': [(-.5, 2), (.5, 4)], }) grid_base.register('4', { 'goal_locations': [(0, 10)], 'lidar_max_dist': 14, 'hazards_num': 4, 'hazards_locations': [(-.5, 2), (.5, 4), (-.5, 6), (.5, 8)], }) grid_base.register('Wall', { 'goal_locations': [(0, 10)], 'lidar_max_dist': 14, 'hazards_num': 42, 'hazards_locations': [ (-.5, 2), (.5, 4), (-.5, 6), (.5, 8), (2, -1), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-2, 3), (-2, 4), (-2, 5), (-2, 6), (-2, 7), (-2, 8), (-2, 9), (-2, 10), (-2, 11), (-2, 12), (-2, -2), (-1, -2), (0, -2), (1, -2), (2, -2), (-2, 13), (-1, 13), (0, 13), (1, 13), (2, 13), ]}) #=============================================================================# # # # Undocumented Debug Environments: Run & Circle # # # #=============================================================================# run_dict = { 'task': 'x', 'observe_goal_lidar': False, 'observe_box_lidar': False, 'robot_rot': 0, } run_dict.update(zero_base_dict) bench_run_base = bench_base.copy('Run', run_dict) bench_run_base.register('') circle_dict = { 'task': 'circle', 'observe_goal_lidar': False, 'observe_box_lidar': False, 'observe_circle': True, 'lidar_max_dist': 6 } circle_dict.update(zero_base_dict) bench_circle_base = bench_base.copy('Circle', circle_dict) bench_circle_base.register('')
11,276
30.412256
100
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_bench.py
#!/usr/bin/env python import re import unittest import numpy as np import gym import gym.spaces from safety_gym.envs.engine import Engine class TestBench(unittest.TestCase): def test_goal(self): ''' Point should run into and get a goal ''' config = { 'robot_base': 'xmls/point.xml', 'goal_size': 0.5, 'goal_placements': [(0, -.5, 5, .5)], 'reward_goal': 1.0, 'reward_distance': 1.0, 'robot_locations': [(0, 0)], 'robot_rot': 0, '_seed': 0, } env = Engine(config) env.reset() goal_met = False for _ in range(999): act = np.zeros(env.action_space.shape) act[0] = 1 _, reward, done, info = env.step(act) self.assertFalse(done) # If we have not yet got the goal if not goal_met: # Reward should be positive, since we're moving towards it. self.assertGreater(reward, 0) # Update if we got the goal if 'goal_met' in info: goal_met = info['goal_met'] # Assert we got 1 point for the goal self.assertGreater(reward, 1) # env.render() # Uncomment to visualize test self.assertTrue(goal_met) def test_hazards(self): ''' Point should run into and get a hazard ''' config = { 'robot_base': 'xmls/point.xml', 'goal_size': 0.5, 'goal_placements': [(5, -.5, 10, .5)], 'reward_goal': 1.0, 'reward_distance': 1.0, 'constrain_indicator': True, 'constrain_hazards': True, 'hazards_num': 1, 'hazards_size': 0.5, 'hazards_locations': [(2, 0)], 'hazards_cost': 1.0, 'robot_locations': [(0, 0)], 'robot_rot': 0, '_seed': 0, } env = Engine(config) env.reset() goal_met = False hazard_found = False for _ in range(999): act = np.zeros(env.action_space.shape) act[0] = 1 _, reward, done, info = env.step(act) if not hazard_found: if info['cost']: hazard_found = True self.assertEqual(info['cost'], 1.0) # Sparse costs self.assertGreater(info['cost_hazards'], 0.0) # Nonzero hazard cost if 'goal_met' in info: goal_met = info['goal_met'] # env.render() # Uncomment to visualize test self.assertTrue(hazard_found) self.assertTrue(goal_met) def test_vases(self): ''' Point should run into and past a vase, pushing it out of the way ''' config = { 'robot_base': 'xmls/point.xml', 'goal_size': 0.5, 'goal_placements': [(5, -.5, 10, .5)], 'reward_goal': 1.0, 'reward_distance': 1.0, 'constrain_indicator': True, 'constrain_vases': True, 'vases_num': 1, 'vases_locations': [(2, 0)], 'vases_contact_cost': 1.0, 'vases_displace_cost': 1.0, 'vases_velocity_cost': 1.0, 'robot_locations': [(0, 0)], 'robot_rot': 0, '_seed': 0, } env = Engine(config) env.reset() goal_met = False vase_found = False for _ in range(999): act = np.zeros(env.action_space.shape) act[0] = 1 _, reward, done, info = env.step(act) if not vase_found: if info['cost']: vase_found = True self.assertEqual(info['cost'], 1.0) # Sparse costs self.assertGreater(info['cost_vases_contact'], 0.0) # Nonzero vase cost self.assertGreater(info['cost_vases_velocity'], 0.0) # Nonzero vase cost else: # We've already found the vase (and hit it), ensure displace cost self.assertEqual(info['cost'], 1.0) # Sparse costs self.assertGreater(info['cost_vases_displace'], 0.0) # Nonzero vase cost if 'goal_met' in info: goal_met = info['goal_met'] # env.render() # Uncomment to visualize test self.assertTrue(vase_found) self.assertTrue(goal_met) def check_correct_lidar(self, env_name): ''' Check that a benchmark env has the right lidar obs for the objects in scene ''' env = gym.make(env_name) env.reset() physics = env.unwrapped world = physics.world obs_space_dict = physics.obs_space_dict task = physics.task lidar_count = sum('lidar' in o.lower() for o in obs_space_dict.keys()) # Goal based lidar if task == 'x': self.assertEqual(lidar_count, 0) elif task == 'circle': self.assertEqual(lidar_count, 1) self.assertIn('circle_lidar', obs_space_dict) elif task == 'goal': self.assertIn('goal_lidar', obs_space_dict) elif task == 'push': self.assertIn('goal_lidar', obs_space_dict) self.assertIn('box_lidar', obs_space_dict) elif task == 'button': self.assertIn('goal_lidar', obs_space_dict) self.assertIn('buttons_lidar', obs_space_dict) if physics.constrain_hazards or physics.hazards_num > 0: self.assertIn('hazards_lidar', obs_space_dict) self.assertGreater(physics.hazards_num, 0) if physics.constrain_vases or physics.vases_num > 0: self.assertIn('vases_lidar', obs_space_dict) self.assertGreater(physics.vases_num, 0) if physics.constrain_pillars or physics.pillars_num > 0: self.assertIn('pillars_lidar', obs_space_dict) self.assertGreater(physics.pillars_num, 0) if physics.constrain_buttons or physics.buttons_num > 0: self.assertIn('buttons_lidar', obs_space_dict) self.assertGreater(physics.buttons_num, 0) if physics.constrain_gremlins or physics.gremlins_num > 0: self.assertIn('gremlins_lidar', obs_space_dict) self.assertGreater(physics.gremlins_num, 0) def test_correct_lidar(self): ''' We should have lidar for every object in the env ''' matched = [] for env_spec in gym.envs.registry.all(): #if re.match(r'Safexp-.*-v0', env_spec.id) is not None: if 'Safexp' in env_spec.id and not('Vision' in env_spec.id): matched.append(env_spec.id) assert matched, 'Failed to match any environments!' for env_name in matched: print(env_name) self.check_correct_lidar(env_name) if __name__ == '__main__': unittest.main()
6,951
37.622222
93
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_envs.py
#!/usr/bin/env python import unittest import gym import safety_gym.envs # noqa class TestEnvs(unittest.TestCase): def check_env(self, env_name): ''' Run a single environment for a single episode ''' print('running', env_name) env = gym.make(env_name) env.reset() done = False while not done: _, _, done, _ = env.step(env.action_space.sample()) def test_envs(self): ''' Run all the bench envs ''' for env_spec in gym.envs.registry.all(): if 'Safexp' in env_spec.id: self.check_env(env_spec.id) if __name__ == '__main__': unittest.main()
660
22.607143
63
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_goal.py
#!/usr/bin/env python import unittest import numpy as np from safety_gym.envs.engine import Engine, ResamplingError class TestGoal(unittest.TestCase): def rollout_env(self, env): ''' roll an environment until it is done ''' done = False while not done: _, _, done, _ = env.step([1,0]) def test_resample(self): ''' Episode should end with resampling failure ''' config = { 'robot_base': 'xmls/point.xml', 'num_steps': 1001, 'placements_extents': [-1, -1, 1, 1], 'goal_size': 1.414, 'goal_keepout': 1.414, 'goal_locations': [(1, 1)], 'robot_keepout': 1.414, 'robot_locations': [(-1, -1)], 'robot_rot': np.sin(np.pi / 4), 'terminate_resample_failure': True, '_seed': 0, } env = Engine(config) env.reset() self.assertEqual(env.steps, 0) # Move the robot towards the goal self.rollout_env(env) # Check that the environment terminated early self.assertLess(env.steps, 1000) # Try again with the raise config['terminate_resample_failure'] = False env = Engine(config) env.reset() # Move the robot towards the goal, which should cause resampling failure with self.assertRaises(ResamplingError): self.rollout_env(env) if __name__ == '__main__': unittest.main()
1,480
28.62
80
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_determinism.py
#!/usr/bin/env python import unittest import numpy as np import gym import safety_gym # noqa class TestDeterminism(unittest.TestCase): def check_qpos(self, env_name): ''' Check that a single environment is seed-stable at init ''' for seed in [0, 1, 123456789]: print('running', env_name, seed) env1 = gym.make(env_name) env1.seed(np.random.randint(123456789)) env1.reset() env1.seed(seed) env1.reset() env2 = gym.make(env_name) env2.seed(seed) env2.reset() np.testing.assert_almost_equal(env1.unwrapped.data.qpos, env2.unwrapped.data.qpos) def test_qpos(self): ''' Run all the bench envs ''' for env_spec in gym.envs.registry.all(): if 'Safexp' in env_spec.id: self.check_qpos(env_spec.id) def check_names(self, env_name): ''' Check that all the names in the mujoco model are the same for different envs ''' print('check names', env_name) env1 = gym.make(env_name) env1.seed(0) env1.reset() env2 = gym.make(env_name) env2.seed(1) env2.reset() model1 = env1.unwrapped.model model2 = env2.unwrapped.model shared_names = ['actuator_names', 'body_names', 'camera_names', 'geom_names', 'joint_names', 'light_names', 'mesh_names', 'sensor_names', 'site_names', 'tendon_names', 'userdata_names'] for n in shared_names: self.assertEqual(getattr(model1, n), getattr(model2, n)) def test_names(self): ''' Run all the bench envs ''' for env_spec in gym.envs.registry.all(): if 'Safexp' in env_spec.id: self.check_names(env_spec.id) if __name__ == '__main__': unittest.main()
1,873
32.464286
94
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_button.py
#!/usr/bin/env python import unittest import numpy as np from safety_gym.envs.engine import Engine, ResamplingError class TestButton(unittest.TestCase): def rollout_env(self, env, gets_goal=False): ''' Roll an environment out to the end, return final info dict. If gets_goal=True, then also assert that we got a goal successfully. ''' got_goal = False done = False while not done: _, _, done, info = env.step([1, 0]) if 'goal_met' in info: got_goal = True if gets_goal: self.assertTrue(got_goal) return info def test_timer(self): ''' Buttons should wait a period before becoming active again ''' config = { 'robot_base': 'xmls/point.xml', 'num_steps': 100, 'task': 'button', 'buttons_num': 2, 'buttons_locations': [(0, 0), (1, 0)], 'buttons_resampling_delay': 1000, 'constrain_buttons': True, 'constrain_indicator': True, 'robot_locations': [(-1, 0)], 'robot_rot': 0, '_seed': 0, } # Correct button is pressed, nothing afterwards env = Engine(config) env.reset() info = self.rollout_env(env, gets_goal=True) self.assertEqual(info['cost_buttons'], 0.0) # Correct button is pressed, then times out and penalties config['buttons_resampling_delay'] = 10 env = Engine(config) env.reset() info = self.rollout_env(env, gets_goal=True) self.assertEqual(info['cost_buttons'], 1.0) # Wrong button is pressed, gets penalty config['_seed'] = 1 env = Engine(config) env.reset() info = self.rollout_env(env) self.assertEqual(info['cost_buttons'], 1.0) if __name__ == '__main__': unittest.main()
1,916
30.42623
76
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_obs.py
#!/usr/bin/env python import unittest import numpy as np import joblib import os import os.path as osp import gym import safety_gym from safety_gym.envs.engine import Engine class TestObs(unittest.TestCase): def test_rotate(self): ''' Point should observe compass/lidar differently for different rotations ''' config = { 'robot_base': 'xmls/point.xml', 'observation_flatten': False, 'observe_sensors': False, 'observe_remaining': False, 'observe_goal_lidar': True, 'observe_goal_comp': True, 'goal_size': 3, 'goal_locations': [(5, 0)], 'robot_locations': [(1, 1)], '_seed': 0, } for s in (2, 3): config['compass_shape'] = s config['robot_rot'] = 5.3 env = Engine(config) obs0 = env.reset() # for _ in range(1000): env.render() # print('obs0', obs0) config['robot_rot'] = np.pi / 4 env = Engine(config) obs1 = env.reset() # for _ in range(1000): env.render() # print('obs1', obs1) self.assertTrue((obs0['goal_lidar'] != obs1['goal_lidar']).any()) self.assertTrue((obs0['goal_compass'] != obs1['goal_compass']).any()) def test_spaces(self): ''' Observation spaces should not unintentionally change from known good reference ''' BASE_DIR = os.path.dirname(safety_gym.__file__) fpath = osp.join(BASE_DIR, 'test', 'obs_space_refs.pkl') obs_space_refs = joblib.load(fpath) for env_spec in gym.envs.registry.all(): if 'Safexp' in env_spec.id and env_spec.id in obs_space_refs: print('Checking obs space for... ', env_spec.id) env = gym.make(env_spec.id) ref_obs_space_dict = obs_space_refs[env_spec.id] obs_spaces_are_same = env.obs_space_dict==ref_obs_space_dict if not(obs_spaces_are_same): print('\n', env_spec.id, '\n') print('Current Observation Space:\n', env.obs_space_dict, '\n\n') print('Reference Observation Space:\n', ref_obs_space_dict, '\n\n') self.assertTrue(obs_spaces_are_same) if __name__ == '__main__': unittest.main()
2,366
36.571429
94
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/test/test_engine.py
#!/usr/bin/env python import unittest import numpy as np import gym.spaces from safety_gym.envs.engine import Engine class TestEngine(unittest.TestCase): def test_timeout(self): ''' Test that episode is over after num_steps ''' p = Engine({'num_steps': 10}) p.reset() for _ in range(10): self.assertFalse(p.done) p.step(np.zeros(p.action_space.shape)) self.assertTrue(p.done) with self.assertRaises(AssertionError): p.step(np.zeros(p.action_space.shape)) def test_flatten(self): ''' Test that physics can flatten observations ''' p = Engine({'observation_flatten': True}) obs = p.reset() self.assertIsInstance(p.observation_space, gym.spaces.Box) self.assertEqual(len(p.observation_space.shape), 1) self.assertTrue(p.observation_space.contains(obs)) p = Engine({'observation_flatten': False}) obs = p.reset() self.assertIsInstance(p.observation_space, gym.spaces.Dict) self.assertTrue(p.observation_space.contains(obs)) def test_angle_components(self): ''' Test that the angle components are about correct ''' p = Engine({'robot_base': 'xmls/doggo.xml', 'observation_flatten': False, 'sensors_angle_components': True, 'robot_rot': .3}) p.reset() p.step(p.action_space.high) p.step(p.action_space.high) p.step(p.action_space.low) theta = p.data.get_joint_qpos('hip_1_z') dtheta = p.data.get_joint_qvel('hip_1_z') print('theta', theta) print('dtheta', dtheta) print('sensordata', p.data.sensordata) obs = p.obs() print('obs', obs) x, y = obs['jointpos_hip_1_z'] dz = obs['jointvel_hip_1_z'] # x, y components should be unit vector self.assertAlmostEqual(np.sqrt(np.sum(np.square([x, y]))), 1.0) # x, y components should be sin/cos theta self.assertAlmostEqual(np.sin(theta), x) self.assertAlmostEqual(np.cos(theta), y) # dz should be the same as dtheta self.assertAlmostEqual(dz, dtheta) if __name__ == '__main__': unittest.main()
2,257
33.738462
71
py
T2TL
T2TL-main/src/envs/safety/safety-gym/safety_gym/bench/bench_utils.py
import numpy as np import json SG6 = [ 'cargoal1', 'doggogoal1', 'pointbutton1', 'pointgoal1', 'pointgoal2', 'pointpush1', ] SG18 = [ 'carbutton1', 'carbutton2', 'cargoal1', 'cargoal2', 'carpush1', 'carpush2', 'doggobutton1', 'doggobutton2', 'doggogoal1', 'doggogoal2', 'doggopush1', 'doggopush2', 'pointbutton1', 'pointbutton2', 'pointgoal1', 'pointgoal2', 'pointpush1', 'pointpush2' ] SG1 = [x for x in SG18 if '1' in x] SG2 = [x for x in SG18 if '2' in x] SGPoint = [x for x in SG18 if 'point' in x] SGCar = [x for x in SG18 if 'car' in x] SGDoggo = [x for x in SG18 if 'doggo' in x] def normalize(env, ret, cost, costrate, cost_limit=25, round=False): """ Compute normalized metrics in a given environment for a given cost limit. Inputs: env: environment name. a string like 'Safexp-PointGoal1-v0' ret: the average episodic return of the final policy cost: the average episodic sum of costs of the final policy costrate: the sum of all costs over training divided by number of environment steps from all of training """ env = env.split('-')[1].lower() with open('safety_gym/bench/characteristic_scores.json') as file: scores = json.load(file) env_ret = scores[env]['Ret'] env_cost = scores[env]['Cost'] env_costrate = scores[env]['CostRate'] epsilon = 1e-6 normed_ret = ret / env_ret normed_cost = max(0, cost - cost_limit) / max(epsilon, env_cost - cost_limit) normed_costrate = costrate / env_costrate if round: normed_ret = np.round(normed_ret, 3) normed_cost = np.round(normed_cost, 3) normed_costrate = np.round(normed_costrate, 3) return normed_ret, normed_cost, normed_costrate
1,887
24.173333
81
py
T2TL
T2TL-main/src/utils/ast_builder.py
import ring import numpy as np import torch import dgl import networkx as nx from sklearn.preprocessing import OneHotEncoder edge_types = {k:v for (v, k) in enumerate(["self", "arg", "arg1", "arg2"])} """ A class that can take an LTL formula and generate the Abstract Syntax Tree (AST) of it. This code can generate trees in either Networkx or DGL formats. And uses caching to remember recently generated trees. """ class ASTBuilder(object): def __init__(self, propositions): super(ASTBuilder, self).__init__() self.props = propositions terminals = ['True', 'False'] + self.props ## Pad terminals with dummy propositions to get a fixed encoding size for i in range(15 - len(terminals)): terminals.append("dummy_"+str(i)) # terminals = ['True', 'False', 'J', 'W', 'R', 'Y', 'dummy_0', 'dummy_1', 'dummy_2', 'dummy_3', 'dummy_4', 'dummy_5', 'dummy_6', 'dummy_7', 'dummy_8'] self._enc = OneHotEncoder(handle_unknown='ignore', dtype=np.int) self._enc.fit([['next'], ['until'], ['and'], ['or'], ['eventually'], ['always'], ['not']] + np.array(terminals).reshape((-1, 1)).tolist()) # To make the caching work. def __ring_key__(self): return "ASTBuilder" @ring.lru(maxsize=30000) def __call__(self, formula, library="dgl"): nxg = self._to_graph(formula) nx.set_node_attributes(nxg, 0., "is_root") nxg.nodes[0]["is_root"] = 1. if (library == "networkx"): return nxg # convert the Networkx graph to dgl graph and pass the 'feat' attribute # g = dgl.DGLGraph() # g.from_networkx(nxg, node_attrs=["feat", "is_root"], edge_attrs=["type"]) # dgl does not support string attributes (i.e., token) g = dgl.from_networkx(nxg, node_attrs=["feat", "is_root"], edge_attrs=["type"]) return g def _one_hot(self, token): return torch.LongTensor(self._enc.transform([[token]])[0][0].toarray()) def _get_edge_type(self, operator, parameter_num=None): operator = operator.lower() if (operator in ["next", "until", "and", "or"]): # Uncomment to make "and" and "or" permutation invariant # parameter_num = 1 if operator in ["and", "or"] else operator return edge_types[operator + f"_{parameter_num}"] return edge_types[operator] # A helper function that recursively builds up the AST of the LTL formula @ring.lru(maxsize=60000) # Caching the formula->tree pairs in a Last Recently Used fashion def _to_graph(self, formula, shift=0): head = formula[0] rest = formula[1:] nxg = nx.DiGraph() if head in ["until", "and", "or"]: nxg.add_node(shift, feat=self._one_hot(head), token=head) nxg.add_edge(shift, shift, type=self._get_edge_type("self")) l = self._to_graph(rest[0], shift+1) nxg = nx.compose(nxg, l) nxg.add_edge(shift+1, shift, type=self._get_edge_type("arg1")) index = nxg.number_of_nodes() r = self._to_graph(rest[1], shift+index) nxg = nx.compose(nxg, r) nxg.add_edge(shift+index, shift, type=self._get_edge_type("arg2")) return nxg if head in ["next", "eventually", "always", "not"]: nxg.add_node(shift, feat=self._one_hot(head), token=head) nxg.add_edge(shift, shift, type=self._get_edge_type("self")) l = self._to_graph(rest[0], shift+1) nxg = nx.compose(nxg, l) nxg.add_edge(shift+1, shift, type=self._get_edge_type("arg")) return nxg if formula in ["True", "False"]: nxg.add_node(shift, feat=self._one_hot(formula), token=formula) nxg.add_edge(shift, shift, type=self._get_edge_type("self")) return nxg if formula in self.props: nxg.add_node(shift, feat=self._one_hot(formula.replace("'",'')), token=formula) nxg.add_edge(shift, shift, type=self._get_edge_type("self")) return nxg assert False, "Format error in ast_builder.ASTBuilder._to_graph()" return None def draw(G, formula): from networkx.drawing.nx_agraph import graphviz_layout import matplotlib.pyplot as plt # colors = ["black", "red"] # edge_color = [colors[i] for i in nx.get_edge_attributes(G,'type').values()] plt.title(formula) pos=graphviz_layout(G, prog='dot') labels = nx.get_node_attributes(G,'token') nx.draw(G, pos, with_labels=True, arrows=True, labels=labels, node_shape='s', edgelist=list(nx.get_edge_attributes(G,'type')), node_size=500, node_color="white") #edge_color=edge_color plt.show() """ A simple test to check if the ASTBuilder works fine. We do a preorder DFS traversal of the resulting tree and convert it to a simplified formula and compare the result with the simplified version of the original formula. They should match. """ if __name__ == '__main__': import re import sys import itertools import matplotlib.pyplot as plt sys.path.insert(0, '../../') from ltl_samplers import getLTLSampler for sampler_id, _ in itertools.product(["Default", "Sequence_2_20"], range(20)): props = "abcdefghijklmnopqrst" sampler = getLTLSampler(sampler_id, props) builder = ASTBuilder(list(set(list(props)))) formula = sampler.sample() tree = builder(formula, library="networkx") pre = list(nx.dfs_preorder_nodes(tree, source=0)) draw(tree, formula) u_tree = tree.to_undirected() pre = list(nx.dfs_preorder_nodes(u_tree, source=0)) original = re.sub('[,\')(]', '', str(formula)) observed = " ".join([u_tree.nodes[i]["token"] for i in pre]) assert original == observed, f"Test Faield: Expected: {original}, Got: {observed}" print("Test Passed!")
5,910
37.383117
197
py
T2TL
T2TL-main/src/utils/storage.py
import csv import os import torch import logging import sys import pickle import utils def create_folders_if_necessary(path): dirname = os.path.dirname(path) if not os.path.isdir(dirname): os.makedirs(dirname) def get_storage_dir(): if "RL_STORAGE" in os.environ: return os.environ["RL_STORAGE"] return "storage" def get_model_dir(model_name, storage_dir="storage"): return os.path.join(storage_dir, model_name) def get_status_path(model_dir): return os.path.join(model_dir, "status.pt") def get_status(model_dir, args): path = get_status_path(model_dir) return torch.load(path, map_location=torch.device(args.cuda if torch.cuda.is_available() else "cpu")) def save_status(status, model_dir): path = get_status_path(model_dir) utils.create_folders_if_necessary(path) torch.save(status, path) def get_vocab(model_dir): return get_status(model_dir)["vocab"] def get_model_state(model_dir): return get_status(model_dir)["model_state"] def get_txt_logger(model_dir): path = os.path.join(model_dir, "log.txt") utils.create_folders_if_necessary(path) logging.basicConfig( level=logging.INFO, format="%(message)s", handlers=[ logging.FileHandler(filename=path), logging.StreamHandler(sys.stdout) ] ) return logging.getLogger() def get_csv_logger(model_dir): csv_path = os.path.join(model_dir, "log.csv") utils.create_folders_if_necessary(csv_path) csv_file = open(csv_path, "a") return csv_file, csv.writer(csv_file) def load_config(model_dir): path = os.path.join(model_dir, "config.pickle") if (not os.path.exists(path)): print(f"No config file found at: {path}") return pickle.load(open(path, "rb")) def save_config(model_dir, config): path = os.path.join(model_dir, "config.pickle") utils.create_folders_if_necessary(path) pickle.dump(config, open(path, "wb"))
1,978
22.282353
105
py
T2TL
T2TL-main/src/utils/format.py
""" These functions preprocess the observations. When trying more sophisticated encoding for LTL, we might have to modify this code. """ import os import json import re import torch import torch_ac import gym import numpy as np import utils from envs import * from ltl_wrappers import LTLEnv def get_obss_preprocessor(env, gnn, progression_mode, gnn_type=None): obs_space = env.observation_space vocab_space = env.get_propositions() vocab = None if isinstance(env, LTLEnv): # LTLEnv Wrapped env env = env.unwrapped if isinstance(env, ZonesEnv): if progression_mode == "partial": obs_space = {"image": obs_space.spaces["features"].shape, "progress_info": len(vocab_space)} def preprocess_obss(obss, device=None): return torch_ac.DictList({ "image": preprocess_images([obs["features"] for obs in obss], device=device), "progress_info": torch.stack([torch.tensor(obs["progress_info"], dtype=torch.float) for obs in obss], dim=0).to(device) }) else: obs_space = {"image": obs_space.spaces["features"].shape, "text": max(22, len(vocab_space) + 10)} vocab_space = {"max_size": obs_space["text"], "tokens": vocab_space} vocab = Vocabulary(vocab_space) tree_builder = utils.ASTBuilder(vocab_space["tokens"]) def preprocess_obss(obss, device=None): return torch_ac.DictList({ "image": preprocess_images([obs["features"] for obs in obss], device=device), "text": preprocess_texts([obs["text"] for obs in obss], vocab, vocab_space, gnn=gnn, gnn_type=gnn_type, device=device, ast=tree_builder) }) preprocess_obss.vocab = vocab else: raise ValueError("Unknown observation space: " + str(obs_space)) # Check if obs_space is an image space elif isinstance(obs_space, gym.spaces.Box): obs_space = {"image": obs_space.shape} def preprocess_obss(obss, device=None): return torch_ac.DictList({ "image": preprocess_images(obss, device=device) }) else: raise ValueError("Unknown observation space: " + str(obs_space)) return obs_space, preprocess_obss def preprocess_images(images, device=None): # Bug of Pytorch: very slow if not first converted to numpy array images = np.array(images) # list:16 (each list : len(list)=76) -> {ndarray: (16, 76)} return torch.tensor(images, device=device, dtype=torch.float) def preprocess_texts(texts, vocab, vocab_space, gnn=False, gnn_type=None, device=None, **kwargs): if (gnn): return preprocess4gnn(texts, kwargs["ast"], device) return preprocess4rnn(texts, vocab, device) def preprocess4rnn(texts, vocab, device=None): var_indexed_texts = [] max_text_len = 25 for text in texts: text = str(text) # transforming the ltl formula into a string tokens = re.findall("([a-z]+)", text.lower()) var_indexed_text = np.array([vocab[token] for token in tokens]) var_indexed_texts.append(var_indexed_text) max_text_len = max(len(var_indexed_text), max_text_len) indexed_texts = np.zeros((len(texts), max_text_len)) for i, indexed_text in enumerate(var_indexed_texts): indexed_texts[i, :len(indexed_text)] = indexed_text return torch.tensor(indexed_texts, device=device, dtype=torch.long) def preprocess4gnn(texts, ast, device=None): """ This function receives the LTL formulas and convert them into inputs for a GNN """ return np.array([[ast(text).to(device)] for text in texts]) class Vocabulary: """A mapping from tokens to ids with a capacity of `max_size` words. It can be saved in a `vocab.json` file.""" def __init__(self, vocab_space): self.max_size = vocab_space["max_size"] self.vocab = {} # populate the vocab with the LTL operators for item in ['next', 'until', 'and', 'or', 'eventually', 'always', 'not', 'True', 'False']: self.__getitem__(item) for item in vocab_space["tokens"]: self.__getitem__(item) def load_vocab(self, vocab): self.vocab = vocab def __getitem__(self, token): if not token in self.vocab.keys(): if len(self.vocab) >= self.max_size: raise ValueError("Maximum vocabulary capacity reached") self.vocab[token] = len(self.vocab) + 1 return self.vocab[token]
4,698
35.710938
161
py
T2TL
T2TL-main/src/utils/evaluator.py
import time import torch from torch_ac.utils.penv import ParallelEnv #import tensorboardX import utils import argparse import datetime class Eval: def __init__(self, env, model_name, ltl_sampler, seed=0, device="cpu", argmax=False, num_procs=1, ignoreLTL=False, progression_mode=True, gnn=None, recurrence=1, dumb_ac = False, discount=0.99): self.env = env self.device = device self.argmax = argmax self.num_procs = num_procs self.ignoreLTL = ignoreLTL self.progression_mode = progression_mode self.gnn = gnn self.recurrence = recurrence self.dumb_ac = dumb_ac self.discount = discount self.model_dir = utils.get_model_dir(model_name, storage_dir="") #self.tb_writer = tensorboardX.SummaryWriter(self.model_dir + "/eval-" + ltl_sampler) # Load environments for evaluation eval_envs = [] for i in range(self.num_procs): eval_envs.append(utils.make_env(env, progression_mode, ltl_sampler, seed, 0, False)) eval_envs[0].reset() if isinstance(eval_envs[0].env, LetterEnv): for env in eval_envs: env.env.map = eval_envs[0].env.map self.eval_envs = ParallelEnv(eval_envs) def eval(self, num_frames, episodes=100, stdout=True): # Load agent agent = utils.Agent(self.eval_envs.envs[0], self.eval_envs.observation_space, self.eval_envs.action_space, self.model_dir + "/train", self.ignoreLTL, self.progression_mode, self.gnn, recurrence = self.recurrence, dumb_ac = self.dumb_ac, device=self.device, argmax=self.argmax, num_envs=self.num_procs) # Run agent start_time = time.time() obss = self.eval_envs.reset() log_counter = 0 log_episode_return = torch.zeros(self.num_procs, device=self.device) log_episode_num_frames = torch.zeros(self.num_procs, device=self.device) # Initialize logs logs = {"num_frames_per_episode": [], "return_per_episode": []} while log_counter < episodes: actions = agent.get_actions(obss) obss, rewards, dones, _ = self.eval_envs.step(actions) agent.analyze_feedbacks(rewards, dones) log_episode_return += torch.tensor(rewards, device=self.device, dtype=torch.float) log_episode_num_frames += torch.ones(self.num_procs, device=self.device) for i, done in enumerate(dones): if done: log_counter += 1 logs["return_per_episode"].append(log_episode_return[i].item()) logs["num_frames_per_episode"].append(log_episode_num_frames[i].item()) mask = 1 - torch.tensor(dones, device=self.device, dtype=torch.float) log_episode_return *= mask log_episode_num_frames *= mask end_time = time.time() return logs["return_per_episode"], logs["num_frames_per_episode"] if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--ltl-sampler", default="Default", help="the ltl formula template to sample from (default: DefaultSampler)") parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)") parser.add_argument("--model-paths", required=True, nargs="+", help="path of the model, or a regular expression") parser.add_argument("--procs", type=int, default=1, help="number of processes (default: 1)") parser.add_argument("--eval-episodes", type=int, default=5, help="number of episodes to evaluate on (default: 5)") parser.add_argument("--env", default="Letter-7x7-v3", help="name of the environment to train on (REQUIRED)") parser.add_argument("--discount", type=float, default=0.99, help="discount factor (default: 0.99)") parser.add_argument("--ignoreLTL", action="store_true", default=False, help="the network ignores the LTL input") parser.add_argument("--progression-mode", default="full", help="Full: uses LTL progression; partial: shows the propositions which progress or falsify the formula; none: only original formula is seen. ") parser.add_argument("--recurrence", type=int, default=1, help="number of time-steps gradient is backpropagated (default: 1). If > 1, a LSTM is added to the model to have memory.") parser.add_argument("--gnn", default="RGCN_8x32_ROOT_SHARED", help="use gnn to model the LTL (only if ignoreLTL==True)") args = parser.parse_args() logs_returns_per_episode = [] logs_num_frames_per_episode = [] for model_path in args.model_paths: idx = model_path.find("seed:") + 5 seed = int(model_path[idx:idx+2].strip("_")) eval = utils.Eval(args.env, model_path, args.ltl_sampler, seed=seed, device=torch.device("cpu"), argmax=False, num_procs=args.procs, ignoreLTL=args.ignoreLTL, progression_mode=args.progression_mode, gnn=args.gnn, recurrence=args.recurrence, dumb_ac=False, discount=args.discount) rpe, nfpe = eval.eval(-1, episodes=args.eval_episodes, stdout=True) logs_returns_per_episode += rpe logs_num_frames_per_episode += nfpe print(sum(rpe), seed, model_path) print(logs_num_frames_per_episode) print(logs_returns_per_episode) num_frame_pe = sum(logs_num_frames_per_episode) return_per_episode = utils.synthesize(logs_returns_per_episode) num_frames_per_episode = utils.synthesize(logs_num_frames_per_episode) average_discounted_return, error = utils.average_discounted_return(logs_returns_per_episode, logs_num_frames_per_episode, args.discount, include_error=True) header = ["frames"] data = [num_frame_pe] header += ["num_frames_" + key for key in num_frames_per_episode.keys()] data += num_frames_per_episode.values() header += ["average_discounted_return", "err"] data += [average_discounted_return, error] header += ["return_" + key for key in return_per_episode.keys()] data += return_per_episode.values() for field, value in zip(header, data): print(field, value)
6,373
42.067568
189
py
T2TL
T2TL-main/src/utils/agent.py
import torch import utils from model import ACModel from recurrent_model import RecurrentACModel class Agent: """An agent. It is able: - to choose an action given an observation, - to analyze the feedback (i.e. reward and done state) of its action.""" def __init__(self, env, obs_space, action_space, model_dir, ignoreLTL, progression_mode, gnn, recurrence = 1, dumb_ac = False, device=None, argmax=False, num_envs=1): try: print(model_dir) status = utils.get_status(model_dir) except OSError: status = {"num_frames": 0, "update": 0} using_gnn = (gnn != "GRU" and gnn != "LSTM") obs_space, self.preprocess_obss = utils.get_obss_preprocessor(env, using_gnn, progression_mode) if "vocab" in status and self.preprocess_obss.vocab is not None: self.preprocess_obss.vocab.load_vocab(status["vocab"]) if recurrence > 1: self.acmodel = RecurrentACModel(env, obs_space, action_space, ignoreLTL, gnn, dumb_ac, True) self.memories = torch.zeros(num_envs, self.acmodel.memory_size, device=device) else: self.acmodel = ACModel(env, obs_space, action_space, ignoreLTL, gnn, dumb_ac, True) self.device = device self.argmax = argmax self.num_envs = num_envs self.acmodel.load_state_dict(utils.get_model_state(model_dir)) self.acmodel.to(self.device) self.acmodel.eval() def get_actions(self, obss): preprocessed_obss = self.preprocess_obss(obss, device=self.device) with torch.no_grad(): if self.acmodel.recurrent: dist, _, self.memories = self.acmodel(preprocessed_obss, self.memories) else: dist, _ = self.acmodel(preprocessed_obss) if self.argmax: actions = dist.probs.max(1, keepdim=True)[1] else: actions = dist.sample() return actions.cpu().numpy() def get_action(self, obs): return self.get_actions([obs])[0] def analyze_feedbacks(self, rewards, dones): if self.acmodel.recurrent: masks = 1 - torch.tensor(dones, dtype=torch.float).unsqueeze(1) self.memories *= masks def analyze_feedback(self, reward, done): return self.analyze_feedbacks([reward], [done])
2,374
33.926471
104
py
T2TL
T2TL-main/src/utils/__init__.py
from .agent import * from .env import * from .format import * from .other import * from .storage import * from .evaluator import * from .ast_builder import *
158
18.875
26
py
T2TL
T2TL-main/src/utils/env.py
""" This class defines the environments that we are going to use. Note that this is the place to include the right LTL-Wrapper for each environment. """ import gym import ltl_wrappers def make_env(env_key, progression_mode, ltl_sampler, seed=None, intrinsic=0, noLTL=False): env = gym.make(env_key) env.seed(seed) # Adding LTL wrappers if (noLTL): return ltl_wrappers.NoLTLWrapper(env) else: return ltl_wrappers.LTLEnv(env, progression_mode, ltl_sampler, intrinsic)
506
25.684211
90
py
T2TL
T2TL-main/src/utils/other.py
import random import numpy import torch import collections def seed(seed): random.seed(seed) numpy.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed) def synthesize(array): d = collections.OrderedDict() d["mean"] = numpy.mean(array) d["std"] = numpy.std(array) d["min"] = numpy.amin(array) d["max"] = numpy.amax(array) return d def average_reward_per_step(returns, num_frames): avgs = [] assert(len(returns) == len(num_frames)) for i in range(len(returns)): avgs.append(returns[i] / num_frames[i]) return numpy.mean(avgs) def average_discounted_return(returns, num_frames, disc): discounted_returns = [] assert(len(returns) == len(num_frames)) for i in range(len(returns)): discounted_returns.append(returns[i] * (disc ** (num_frames[i]-1))) return numpy.mean(discounted_returns)
941
21.97561
75
py
T2TL
T2TL-main/src/gnns/graph_registry.py
gnn_registry = {} def get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m = __import__( module ) for comp in parts[1:]: m = getattr(m, comp) return m def register(id="", entry_point=None, **kwargs): gnn_registry[id] = { "class": get_class(entry_point), "kwargs": kwargs } def lookup(gnn_id): return gnn_registry[gnn_id]
401
19.1
48
py
T2TL
T2TL-main/src/gnns/__init__.py
from gnns.graph_registry import * from gnns.graphs.GNN import * register(id="GCN_2x32_MEAN", entry_point="gnns.graphs.GCN.GCN", hidden_dims=[32, 32]) register(id="GCN_4x32_MEAN", entry_point="gnns.graphs.GCN.GCN", hidden_dims=[32, 32, 32, 32]) register(id="GCN_32_MEAN", entry_point="gnns.graphs.GCN.GCN", hidden_dims=[32]) register(id="GCN_32_ROOT", entry_point="gnns.graphs.GCN.GCNRoot", hidden_dims=[32]) register(id="GCN_2x32_ROOT", entry_point="gnns.graphs.GCN.GCNRoot", hidden_dims=[32, 32]) register(id="GCN_4x32_ROOT", entry_point="gnns.graphs.GCN.GCNRoot", hidden_dims=[32, 32, 32, 32]) register(id="GCN_2x32_ROOT_SHARED", entry_point="gnns.graphs.GCN.GCNRootShared", hidden_dim=32, num_layers=2) register(id="GCN_4x32_ROOT_SHARED", entry_point="gnns.graphs.GCN.GCNRootShared", hidden_dim=32, num_layers=4) register(id="RGCN_2x32_ROOT", entry_point="gnns.graphs.RGCN.RGCNRoot", hidden_dims=[32, 32]) register(id="RGCN_4x16_ROOT", entry_point="gnns.graphs.RGCN.RGCNRoot", hidden_dims=[16] * 4) register(id="RGCN_4x32_ROOT", entry_point="gnns.graphs.RGCN.RGCNRoot", hidden_dims=[32] * 4) register(id="RGCN_8x32_ROOT", entry_point="gnns.graphs.RGCN.RGCNRoot", hidden_dims=[32] * 8) register(id="RGCN_4x32_ROOT_SHARED", entry_point="gnns.graphs.RGCN.RGCNRootShared", hidden_dim=32, num_layers=4) register(id="RGCN_8x32_ROOT_SHARED", entry_point="gnns.graphs.RGCN.RGCNRootShared", hidden_dim=32, num_layers=8) register(id="RGCN_16x32_ROOT_SHARED", entry_point="gnns.graphs.RGCN.RGCNRootShared", hidden_dim=32, num_layers=16)
1,544
45.818182
114
py
T2TL
T2TL-main/src/gnns/graphs/GCN.py
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import dgl from dgl.nn.pytorch.conv import GraphConv from gnns.graphs.GNN import GNN class GCN(GNN): def __init__(self, input_dim, output_dim, **kwargs): super().__init__(input_dim, output_dim) hidden_dims = kwargs.get('hidden_dims', [32]) self.num_layers = len(hidden_dims) hidden_plus_input_dims = [hd + input_dim for hd in hidden_dims] self.convs = nn.ModuleList([GraphConv(in_dim, out_dim, activation=F.relu) for (in_dim, out_dim) in zip([input_dim] + hidden_plus_input_dims[:-1], hidden_dims)]) self.g_embed = nn.Linear(hidden_dims[-1], output_dim) # Uses the base implementation which averages hidden representations of all nodes def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = g.ndata["feat"].float() h = h_0 for i in range(self.num_layers): if i != 0: h = self.convs[i](g, torch.cat([h, h_0], dim=1)) else: h = self.convs[i](g, h) g.ndata['h'] = h # Calculate graph representation by averaging all the hidden node representations. hg = dgl.mean_nodes(g, 'h') return self.g_embed(hg).squeeze(1) # GCN, but the graph representation is only the representation of the root node. class GCNRoot(GCN): def __init__(self, input_dim, output_dim, **kwargs): super().__init__(input_dim, output_dim, **kwargs) def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = g.ndata["feat"].float() h = h_0 for i in range(self.num_layers): if i != 0: h = self.convs[i](g, torch.cat([h, h_0], dim=1)) else: h = self.convs[i](g, h) g.ndata['h'] = h hg = dgl.sum_nodes(g, 'h', weight='is_root') return self.g_embed(hg).squeeze(1) class GCNRootShared(GNN): def __init__(self, input_dim, output_dim, **kwargs): super().__init__(input_dim, output_dim) hidden_dim = kwargs.get('hidden_dim', 32) num_layers = kwargs.get('num_layers', 2) self.num_layers = num_layers self.linear_in = nn.Linear(input_dim, hidden_dim) self.conv = GraphConv(2*hidden_dim, hidden_dim, activation=F.relu) self.g_embed = nn.Linear(hidden_dim, output_dim) def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = self.linear_in(g.ndata["feat"].float()) h = h_0 # Apply convolution layers for i in range(self.num_layers): h = self.conv(g, torch.cat([h, h_0], dim=1)) g.ndata['h'] = h hg = dgl.sum_nodes(g, 'h', weight='is_root') return self.g_embed(hg).squeeze(1)
2,927
31.898876
103
py
T2TL
T2TL-main/src/gnns/graphs/RGCN.py
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import dgl from dgl.nn.pytorch.conv import RelGraphConv from gnns.graphs.GNN import GNN from utils.ast_builder import edge_types class RGCN(GNN): def __init__(self, input_dim, output_dim, **kwargs): super().__init__(input_dim, output_dim) hidden_dims = kwargs.get('hidden_dims', [32]) self.num_layers = len(hidden_dims) hidden_plus_input_dims = [hd + input_dim for hd in hidden_dims] self.convs = nn.ModuleList([RelGraphConv(in_dim, out_dim, len(edge_types), activation=F.relu) for (in_dim, out_dim) in zip([input_dim] + hidden_plus_input_dims[:-1], hidden_dims)]) self.g_embed = nn.Linear(hidden_dims[-1], output_dim) def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = g.ndata["feat"].float() h = h_0 etypes = g.edata["type"].float() for i in range(self.num_layers): if i != 0: h = self.convs[i](g, torch.cat([h, h_0], dim=1), etypes) else: h = self.convs[i](g, h, etypes) g.ndata['h'] = h # Calculate graph representation by averaging all the hidden node representations. hg = dgl.mean_nodes(g, 'h') return self.g_embed(hg).squeeze(1) class RGCNRoot(RGCN): def __init__(self, input_dim, output_dim, **kwargs): super().__init__(input_dim, output_dim, **kwargs) def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = g.ndata["feat"].float().squeeze() h = h_0 etypes = g.edata["type"] for i in range(self.num_layers): if i != 0: h = self.convs[i](g, torch.cat([h, h_0], dim=1), etypes) else: h = self.convs[i](g, h, etypes) g.ndata['h'] = h # TODO: Check if this is redundant hg = dgl.sum_nodes(g, 'h', weight='is_root') return self.g_embed(hg).squeeze(1) class RGCNRootShared(GNN): def __init__(self, input_dim, output_dim, **kwargs): # 22; 32; {'hidden_dim': 32, 'num_layers': 8} super().__init__(input_dim, output_dim) hidden_dim = kwargs.get('hidden_dim', 32) num_layers = kwargs.get('num_layers', 2) self.num_layers = num_layers self.linear_in = nn.Linear(input_dim, hidden_dim) self.conv = RelGraphConv(2*hidden_dim, hidden_dim, len(edge_types), activation=torch.tanh) self.g_embed = nn.Linear(hidden_dim, output_dim) def forward(self, g): g = np.array(g).reshape((1, -1)).tolist()[0] g = dgl.batch(g) h_0 = self.linear_in(g.ndata["feat"].float().squeeze()) h = h_0 etypes = g.edata["type"] # Apply convolution layers for i in range(self.num_layers): h = self.conv(g, torch.cat([h, h_0], dim=1), etypes) g.ndata['h'] = h g.ndata['is_root'] = g.ndata['is_root'].unsqueeze(1) hg = dgl.sum_nodes(g, 'h', weight='is_root') return self.g_embed(hg).squeeze(1)
3,153
32.913978
103
py
T2TL
T2TL-main/src/gnns/graphs/GNN.py
import torch import torch.nn as nn from gnns import * class GNN(nn.Module): def __init__(self, input_dim, output_dim): super().__init__() def forward(self, g): raise NotImplementedError def GNNMaker(gnn_type, input_dim, output_dim): # 'RGCN_8x32_ROOT_SHARED'; 22; 33 clazz = lookup(gnn_type) return clazz["class"](input_dim, output_dim, **clazz["kwargs"])
393
23.625
81
py
toulbar2
toulbar2-master/setup.py
import os import re import sys import platform import subprocess from setuptools import setup from setuptools.extension import Extension from setuptools import setup, Extension from setuptools.command.build_ext import build_ext from distutils.version import LooseVersion python_Path = sys.executable python_Root = sys.exec_prefix print("==> python_Path =", python_Path) class CMakeExtension(Extension): def __init__(self, name, sourcedir=""): Extension.__init__(self, name, sources=[]) self.sourcedir = os.path.abspath(sourcedir) class CMakeBuild(build_ext): def run(self): try: out = subprocess.check_output(["cmake", "--version"]) except OSError: raise RuntimeError("CMake must be installed to build the following extensions: " + ", ".join(e.name for e in self.extensions)) cmake_version = LooseVersion(re.search(r"version\s*([\d.]+)", out.decode()).group(1)) if cmake_version < LooseVersion("3.5.0"): raise RuntimeError("CMake >= 3.5.0 is required") for ext in self.extensions: self.build_extension(ext) def build_extension(self, ext): extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) cmake_args = [ "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir, "-DPython3_EXECUTABLE=" + python_Path, "-DPython3_ROOT_DIR=" + python_Root, "-DPYTB2=ON", ] build_type = os.environ.get("BUILD_TYPE", "Release") build_args = ["--config", build_type] # Pile all .so in one place and use $ORIGIN as RPATH cmake_args += ["-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE"] cmake_args += ["-DCMAKE_INSTALL_RPATH={}".format("$ORIGIN")] if platform.system() == "Windows": cmake_args += ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(build_type.upper(), extdir)] if sys.maxsize > 2**32: cmake_args += ["-A", "x64"] build_args += ["--", "/m"] else: cmake_args += ["-DCMAKE_BUILD_TYPE=" + build_type] build_args += ["--", "-j4"] env = os.environ.copy() env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get("CXXFLAGS", ""), self.distribution.get_version()) if not os.path.exists(self.build_temp): os.makedirs(self.build_temp) print("cmake_args:", cmake_args) subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env) subprocess.check_call(["cmake", "--build", ".", "--target", os.path.basename("pytb2")] + build_args, cwd=self.build_temp) def read_version(): return subprocess.run(["git", "describe", "--abbrev=0", "--tags", "--always"], capture_output=True).stdout.strip().decode("utf-8") setup( name="pytoulbar2", version="0.0.0.3-1", # hash c9d8388a41bdbcea4a5a1f8e860213fe5d9eda8a author="ToulBar2 team", author_email="thomas.schiex@inrae.fr", description="ToulBar2 Python package", long_description_content_type="text/markdown", license="MIT", keywords="optimization graphical-model", long_description=open("README.md").read(), ext_modules=[CMakeExtension("pytoulbar2.pytb2")], packages=["pytoulbar2"], package_dir={"pytoulbar2": "pytoulbar2"}, cmdclass=dict(build_ext=CMakeBuild), url="http://miat.inrae.fr/toulbar2", zip_safe=False, classifiers=[ "License :: OSI Approved :: MIT License", "Topic :: Scientific/Engineering :: Artificial Intelligence", "Topic :: Scientific/Engineering :: Mathematics", "Programming Language :: C++", "Programming Language :: Python :: 3", "Intended Audience :: Science/Research", ], )
3,772
36.356436
138
py
toulbar2
toulbar2-master/pytoulbar2/__init__.py
from .pytoulbar2 import *
26
12.5
25
py
toulbar2
toulbar2-master/pytoulbar2/pytoulbar2.py
"""Help on module pytoulbar2: NAME pytoulbar2 - Python3 interface of toulbar2. DESCRIPTION """ from math import isinf try : import pytoulbar2.pytb2 as tb2 except : pass class CFN: """pytoulbar2 base class used to manipulate and solve a cost function network. Constructor Args: ubinit (decimal cost or None): initial upper bound. resolution (int): decimal precision of costs. vac (int): if non zero, maximum solver depth minus one where virtual arc consistency algorithm is applied (1: VAC only in preprocessing). configuration (bool): if True then special settings for preference learning using incremental solving (see car configuration tutorial). vns (int or None): if None then solves using branch-and-bound methods else using variable neighborhood search heuristic (-1: initial solution at random, -2: minimum domain values, -3: maximum domain values, -4: first solution found by DFS, >=0: or by LDS with at most vns discrepancies). seed (int): random seed. verbose (int): verbosity control (-1: no message, 0: search statistics, 1: search tree, 2-7: propagation information). Members: CFN (WeightedCSPSolver): python interface to C++ class WeightedCSPSolver. Contradiction (exception): python exception corresponding to the same C++ class. Limit (exception|None): contains the last SolverOut exception or None if no exception occurs when solving with SolveNext. Option (TouBar2): python interface to C++ class ToulBar2. SolverOut (exception): python exception corresponding to the same C++ class. Top (decimal cost): maximum decimal cost (it can be used to represent a forbidden cost). VariableIndices (dict): associative array returning the variable name (str) associated to a given index (int). VariableNames (list): array of created variable names (str) sorted by their index number. See pytoulbar2test.py example in src repository. """ def __init__(self, ubinit = None, resolution = 0, vac = 0, configuration = False, vns = None, seed = 1, verbose = -1): tb2.init() tb2.option.decimalPoint = resolution # decimal precision of costs tb2.option.vac = vac # if no zero, maximum search depth-1 where VAC algorithm is performed (use 1 for preprocessing only) tb2.option.seed = seed # random seed number (use -1 if a pseudo-randomly generated seed is wanted) tb2.option.verbose = verbose # verbosity level of toulbar2 (-1:no message, 0:search statistics, 1:search tree, 2-7: propagation information) # default options (can be modified later by the user) tb2.option.FullEAC = False # if True, exploit VAC integrality variable orderding heuristic or just Full-EAC heuristic if VAC diseable tb2.option.VACthreshold = False # if True, reuse VAC auto-threshold value found in preprocessing during search tb2.option.useRASPS = 0 # if 1 or greater, perform iterative RASPS depth-first search (or LDS if greater than 1) in preprocessing during 1000 backtracks to find a good initial upperbound (to be used with VAC) tb2.option.weightedTightness = 0 # if 1 or 2, variable ordering heuristic exploiting cost distribution information (0: none, 1: mean cost, 2: median cost) self.configuration = configuration # if True then special settings for learning if configuration: tb2.option.elimDegree_preprocessing = 1 # maximum degree level of variable elimination in preprocessing (-1: none, 0: null degree, 1: degree one, etc.) tb2.option.solutionBasedPhaseSaving = False # if False do not reuse previous complete solutions as hints during incremental solving used by structure learning evaluation procedure! if vns is not None: tb2.option.vnsInitSol = vns # if vns different than None then perform Decomposition-Guided Variable Neighborhood Search (-1: initial solution at random, -2: minimum domain values, -3: maximum domain values, -4: first solution found by DFS, >=0: or by LDS with at most vns discrepancies) tb2.option.lds = 4 tb2.option.restart = 10000; tb2.option.searchMethod = 2; # 0:DFBB or HBFS, 1:VNS, 2:DGVNS 4:Parallel DGVNS tb2.option.vnsNeighborVarHeur = 3; # 0: random, 1:conflict, 3: decomposition self.Variables = {} self.VariableIndices = {} self.VariableNames = [] self.CFN = tb2.Solver() # initialize VAC algorithm depending on tb2.option.vac self.InternalCFNs = list() # keep alive internal CFNs created by AddWeightedCSPConstraint self.UbInit = ubinit # warning! cannot convert initial upper bound into an integer cost before knowing the rest of the problem self.Contradiction = tb2.Contradiction self.SolverOut = tb2.SolverOut self.Option = tb2.option self.Top = tb2.MAX_COST // 10**resolution # can be used to represent forbidden assignments self.Limit = None tb2.check() # checks compatibility between selected options def __del__(self): del self.Variables del self.VariableIndices del self.VariableNames del self.CFN @staticmethod def flatten(S): if S == []: return S if isinstance(S[0], list): return CFN.flatten(S[0]) + CFN.flatten(S[1:]) return S[:1] + CFN.flatten(S[1:]) def AddVariable(self, name, values): """AddVariable creates a new discrete variable. Args: name (str): variable name. values (list or iterable): list of domain values represented by numerical (int) or symbolic (str) values. Returns: Index of the created variable in the problem (int). Note: Symbolic values are implicitely associated to integer values (starting from zero) in the other functions. In case of numerical values, the initial domain size is equal to max(values)-min(values)+1 and not equal to len(values). Otherwise (symbolic case), the initial domain size is equal to len(values). """ if name in self.Variables: raise RuntimeError(name+" already defined") self.Variables[name] = values if all(isinstance(value, int) for value in values): vIdx = self.CFN.wcsp.makeEnumeratedVariable(name, min(values), max(values)) for vn in range(min(values), max(values)+1): self.CFN.wcsp.addValueName(vIdx, 'v' + str(vn)) if vn not in values: self.CFN.wcsp.remove(vIdx, vn) elif all(isinstance(value, str) for value in values): vIdx = self.CFN.wcsp.makeEnumeratedVariable(name, 0, len(values)-1) for vn in values: self.CFN.wcsp.addValueName(vIdx, vn) else: raise RuntimeError("Incorrect domain:"+str(values)) self.VariableIndices[name] = vIdx self.VariableNames.append(name) return vIdx def AddFunction(self, scope, costs, incremental = False): """AddFunction creates a cost function in extension. The scope corresponds to the input variables of the function. The costs are given by a flat array the size of which corresponds to the product of initial domain sizes (see note in AddVariable). Args: scope (list): input variables of the function. A variable can be represented by its name (str) or its index (int). costs (list): array of decimal costs for all possible assignments (iterating first over the domain values of the last variable in the scope). incremental (bool): if True then the function is backtrackable (i.e., it disappears when restoring at a lower depth, see Store/Restore). Example: AddFunction(['x','y'], [0,1,1,0]) encodes a binary cost function on Boolean variables x and y such that (x=0,y=0) has a cost of 0, (x=0,y=1) has a cost of 1, (x=1,y=0) has a cost of 1, and (x=1,y=1) has a cost of 0. """ sscope = set(scope) if len(scope) != len(sscope): raise RuntimeError("Duplicate variable in scope:"+str(scope)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) if (len(iscope) == 0): assert(isinstance(costs, (int, float))) self.CFN.wcsp.postNullaryConstraint(costs) elif (len(iscope) == 1): assert(self.CFN.wcsp.getDomainInitSize(iscope[0]) == len(costs)) self.CFN.wcsp.postUnaryConstraint(iscope[0], costs, incremental) elif (len(iscope) == 2): assert(self.CFN.wcsp.getDomainInitSize(iscope[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1]) == len(costs)) self.CFN.wcsp.postBinaryConstraint(iscope[0], iscope[1], costs, incremental) elif (len(iscope) == 3): assert(self.CFN.wcsp.getDomainInitSize(iscope[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1]) * self.CFN.wcsp.getDomainInitSize(iscope[2]) == len(costs)) self.CFN.wcsp.postTernaryConstraint(iscope[0], iscope[1], iscope[2], costs, incremental) else: if incremental: raise NameError('Sorry, incremental ' + str(len(iscope)) + '-arity cost functions not implemented yet in toulbar2.') mincost = min(costs) maxcost = max(costs) self.CFN.wcsp.postNullaryConstraint(mincost) if (mincost == maxcost): return idx = self.CFN.wcsp.postNaryConstraintBegin(iscope, 0, len(costs) - costs.count(0)) tuple = [self.CFN.wcsp.toValue(v, 0) for v in iscope] for cost in costs: if (isinf(cost)): self.CFN.wcsp.postNaryConstraintTuple(idx, tuple, tb2.MAX_COST) elif cost > mincost: self.CFN.wcsp.postNaryConstraintTuple(idx, tuple, int((cost-mincost) * 10 ** tb2.option.decimalPoint)) for r in range(len(iscope)): i = len(iscope)-1-r v = iscope[i] if tuple[i] < self.CFN.wcsp.toValue(v, self.CFN.wcsp.getDomainInitSize(v) - 1): tuple[i] += 1 for j in range(i+1,len(iscope)): tuple[j] = self.CFN.wcsp.toValue(iscope[j], 0) break self.CFN.wcsp.postNaryConstraintEnd(idx) def AddCompactFunction(self, scope, defcost, tuples, tcosts, incremental = False): """AddCompactFunction creates a cost function in extension. The scope corresponds to the input variables of the function. The costs are given by a list of assignments with the corresponding list of costs, all the other assignments taking the default cost. Args: scope (list): input variables of the function. A variable can be represented by its name (str) or its index (int). defcost (decimal cost): default cost. tuples (list): array of assignments (each assignment is a list of domain values, following the scope order). tcosts (list): array of corresponding decimal costs (tcosts and tuples have the same size). incremental (bool): if True then the function is backtrackable (i.e., it disappears when restoring at a lower depth, see Store/Restore). Example: AddCompactFunction(['x','y','z'],0,[[0,0,0],[1,1,1]],[1,-1]) encodes a ternary cost function with the null assignment having a cost of 1, the identity assignment having a cost of -1, and all the other assignments a cost of 0. """ assert(len(tuples) == len(tcosts)) sscope = set(scope) if len(scope) != len(sscope): raise RuntimeError("Duplicate variable in scope:"+str(scope)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) if (len(iscope) == 0): assert(len(tuples) == 0) self.CFN.wcsp.postNullaryConstraint(defcost) elif (len(iscope) == 1): costs = [defcost] * self.CFN.wcsp.getDomainInitSize(iscope[0]) for i, tuple in enumerate(tuples): costs[self.CFN.wcsp.toIndex(iscope[0], tuple[0])] = tcosts[i] self.CFN.wcsp.postUnaryConstraint(iscope[0], costs, incremental) elif (len(iscope) == 2): costs = [defcost] * (self.CFN.wcsp.getDomainInitSize(iscope[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1])) for i, tuple in enumerate(tuples): costs[self.CFN.wcsp.toIndex(iscope[0], tuple[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1]) + self.CFN.wcsp.toIndex(iscope[1], tuple[1])] = tcosts[i] self.CFN.wcsp.postBinaryConstraint(iscope[0], iscope[1], costs, incremental) elif (len(iscope) == 3): costs = [defcost] * (self.CFN.wcsp.getDomainInitSize(iscope[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1]) * self.CFN.wcsp.getDomainInitSize(iscope[2])) for i, tuple in enumerate(tuples): costs[self.CFN.wcsp.toIndex(iscope[0], tuple[0]) * self.CFN.wcsp.getDomainInitSize(iscope[1]) * self.CFN.wcsp.getDomainInitSize(iscope[2]) + self.CFN.wcsp.toIndex(iscope[1], tuple[1]) * self.CFN.wcsp.getDomainInitSize(iscope[2]) + self.CFN.wcsp.toIndex(iscope[2], tuple[2])] = tcosts[i] self.CFN.wcsp.postTernaryConstraint(iscope[0], iscope[1], iscope[2], costs, incremental) else: if incremental: raise NameError('Sorry, incremental ' + str(len(iscope)) + '-arity cost functions not implemented yet in toulbar2.') mincost = min(defcost, min(tcosts)) maxcost = max(defcost, max(tcosts)) self.CFN.wcsp.postNullaryConstraint(mincost) if (mincost == maxcost): return idx = self.CFN.wcsp.postNaryConstraintBegin(iscope, int((defcost - mincost) * 10 ** tb2.option.decimalPoint), len(tcosts)) for i, tuple in enumerate(tuples): self.CFN.wcsp.postNaryConstraintTuple(idx, [self.CFN.wcsp.toValue(iscope[x], self.CFN.wcsp.toIndex(iscope[x], v)) for x,v in enumerate(tuple)], int((tcosts[i] - mincost) * 10 ** tb2.option.decimalPoint)) self.CFN.wcsp.postNaryConstraintEnd(idx) def AddLinearConstraint(self, coefs, scope, operand = '==', rightcoef = 0): """AddLinearConstraint creates a linear constraint with integer coefficients. The scope corresponds to the variables involved in the left part of the constraint. All variables must belong to the left part (change their coefficient sign if they are originally in the right part). All constant terms must belong to the rigt part. Args: coefs (list or int): array of integer coefficients associated to the left-part variables (or the same integer coefficient is applied to all variables). scope (list): variables involved in the left part of the constraint. A variable can be represented by its name (str) or its index (int). operand (str): can be either '==' or '<=' or '<' or '>=' or '>'. rightcoef (int): constant term in the right part. Example: AddLinearConstraint([1,1,-2], [x,y,z], '==', -1) encodes x + y -2z = -1. """ if (isinstance(coefs, int)): coefs = [coefs for v in scope] assert(len(coefs) == len(scope)) sscope = set(scope) if len(scope) != len(sscope): raise RuntimeError("Duplicate variable in scope: "+str(scope)) if operand != '>=' and operand != '>' and operand != '<=' and operand != '<' and operand != '==': raise RuntimeError("Unknown operand in AddLinearConstraint: "+str(operand)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) if operand == '>=' or operand == '>' or operand == '==': params = str((rightcoef + 1) if (operand == '>') else rightcoef) + ' ' + ' '.join(self.flatten([[str(self.CFN.wcsp.getDomainInitSize(v)), [[str(self.CFN.wcsp.toValue(v, valindex)), str(coefs[i] * self.CFN.wcsp.toValue(v, valindex))] for valindex in range(self.CFN.wcsp.getDomainInitSize(v))]] for i,v in enumerate(iscope)])) self.CFN.wcsp.postKnapsackConstraint(iscope, params, kp = True) if operand == '<=' or operand == '<' or operand == '==': params = str((-rightcoef + 1) if (operand == '<') else -rightcoef) + ' ' + ' '.join(self.flatten([[str(self.CFN.wcsp.getDomainInitSize(v)), [[str(self.CFN.wcsp.toValue(v, valindex)), str(-coefs[i] * self.CFN.wcsp.toValue(v, valindex))] for valindex in range(self.CFN.wcsp.getDomainInitSize(v))]] for i,v in enumerate(iscope)])) self.CFN.wcsp.postKnapsackConstraint(iscope, params, kp = True) def AddSumConstraint(self, scope, operand = '==', rightcoef = 0): """AddSumConstraint creates a linear constraint with unit coefficients. The scope corresponds to the variables involved in the left part of the constraint. Args: scope (list): variables involved in the left part of the constraint. A variable can be represented by its name (str) or its index (int). operand (str): can be either '==' or '<=' or '<' or '>=' or '>'. rightcoef (int): constant term in the right part. Example: AddSumConstraint([x,y,z], '<', 3) encodes x + y + z < 3. """ self.AddLinearConstraint(1, scope, operand, rightcoef) def AddGeneralizedLinearConstraint(self, tuples, operand = '==', rightcoef = 0): """AddGeneralizedLinearConstraint creates a linear constraint with integer coefficients associated to domain values. The scope implicitely corresponds to the variables involved in the tuples. Missing domain values have an implicit zero coefficient. All constant terms must belong to the right part. Args: tuples (list): array of triplets (variable, domain value, coefficient) in the left part of the constraint. operand (str): can be either '==' or '<=' or '<' or '>=' or '>'. rightcoef (int): constant term in the right part. Example: AddGeneralizedLinearConstraint([('x',1,1),('y',1,1),('z',0,2)], '==', 1) encodes (x==1) + (y==1) + 2*(z==0) = 1 assuming 0/1 variables and (x==u) is equal to 1 if value u is assigned to x else equal to 0. """ sscope = set() scope = [] for (v, val, coef) in tuples: if v not in sscope: sscope.add(v) scope.append(v) if operand != '>=' and operand != '>' and operand != '<=' and operand != '<' and operand != '==': raise RuntimeError("Unknown operand in AddGeneralizedLinearConstraint: "+str(operand)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) if operand == '>=' or operand == '>' or operand == '==': params = str((rightcoef + 1) if (operand == '>') else rightcoef) for v in iscope: vtuples = [[str(val), str(coef)] for (var, val, coef) in tuples if (isinstance(var, str) and self.VariableIndices[var]==v) or (not isinstance(var, str) and var==v)] params += ' ' + str(len(vtuples)) params += ' ' + ' '.join(self.flatten(vtuples)) self.CFN.wcsp.postKnapsackConstraint(iscope, params, kp = True) if operand == '<=' or operand == '<' or operand == '==': params = str((-rightcoef + 1) if (operand == '<') else -rightcoef) for v in iscope: vtuples = [[str(val), str(-coef)] for (var, val, coef) in tuples if (isinstance(var, str) and self.VariableIndices[var]==v) or (not isinstance(var, str) and var==v)] params += ' ' + str(len(vtuples)) params += ' ' + ' '.join(self.flatten(vtuples)) self.CFN.wcsp.postKnapsackConstraint(iscope, params, kp = True) def AddAllDifferent(self, scope, encoding = 'binary', excepted = None, incremental = False): """Add AllDifferent hard global constraint. Args: scope (list): input variables of the function. A variable can be represented by its name (str) or its index (int). encoding (str): encoding used to represent AllDifferent (available choices are 'binary' or 'salldiff' or 'salldiffdp' or 'salldiffkp' or 'walldiff'). excepted (None or list): list of excepted domain values which can be taken by any variable without violating the constraint. incremental (bool): if True then the constraint is backtrackable (i.e., it disappears when restoring at a lower depth, see Store/Restore). """ if incremental and model != 'binary': raise RuntimeError("Implementation of AllDifferent constraint requires 'binary' encoding in incremental mode!") if excepted is not None and model != 'binary': raise RuntimeError("Excepted domain values in AllDifferent constraint requires 'binary' encoding!") sscope = set(scope) if len(scope) != len(sscope): raise RuntimeError("Duplicate variable in scope:"+str(scope)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) if (len(iscope) >= 2): if (encoding=='binary'): for i in range(len(iscope)): for j in range(i+1, len(iscope)): costs = [(0 if (self.CFN.wcsp.toValue(iscope[i], a) != self.CFN.wcsp.toValue(iscope[j], b) or (excepted and ((self.CFN.wcsp.toValue(iscope[i], a) in excepted) or (self.CFN.wcsp.toValue(iscope[j], b) in excepted)))) else self.Top) for a in range(self.CFN.wcsp.getDomainInitSize(iscope[i])) for b in range(self.CFN.wcsp.getDomainInitSize(iscope[j]))] self.CFN.wcsp.postBinaryConstraint(iscope[i], iscope[j], costs, incremental) elif (encoding=='salldiff'): self.CFN.wcsp.postWAllDiff(iscope, "var", "flow", tb2.MAX_COST); elif (encoding=='salldiffdp'): self.CFN.wcsp.postWAllDiff(iscope, "var", "DAG", tb2.MAX_COST); elif (encoding=='salldiffkp'): self.CFN.wcsp.postWAllDiff(iscope, "hard", "knapsack", tb2.MAX_COST); elif (encoding=='walldiff'): self.CFN.wcsp.postWAllDiff(iscope, "hard", "network", tb2.MAX_COST); def AddGlobalFunction(self, scope, gcname, *parameters): """AddGlobalFunction creates a soft global cost function. Args: scope (list): input variables of the function. A variable can be represented by its name (str) or its index (int). gcname (str): name of the global cost function (see toulbar2 user documentation). parameters (list): list of parameters (str or int) for this global cost function. Example: AddGlobalFunction(['x1','x2','x3','x4'], 'wamong', 'hard', 1000, 2, 1, 2, 1, 3) encodes a hard among constraint satisfied iff values {1,2} are assigned to the given variables at least once and at most 3 times, otherwise it returns a cost of 1000. """ sscope = set(scope) if len(scope) != len(sscope): raise RuntimeError("Duplicate variable in scope:"+str(scope)) iscope = [] for i, v in enumerate(scope): if isinstance(v, str): v = self.VariableIndices.get(v, -1) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) params = str(list(parameters))[1:-1].replace(',','').replace('\'','') self.CFN.wcsp.postGlobalFunction(iscope, gcname, params) def AddWeightedCSPConstraint(self, problem, lb, ub, duplicateHard = False, strongDuality = False): """AddWeightedCSPConstraint creates a hard global constraint on the cost of an input weighted constraint satisfaction problem such that its valid solutions must have a cost value in [lb,ub[. Args: problem (CFN): input problem. lb (decimal cost): any valid solution in the input problem must have a cost greater than or equal to lb. ub (decimal cost): any valid solution in the input problem must have a cost strictly less than ub. duplicateHard (bool): if true then it assumes any forbidden tuple in the original input problem is also forbidden by another constraint in the main model (you must duplicate any hard constraints in your input model into the main model). strongDuality (bool): if true then it assumes the propagation is complete when all channeling variables in the scope are assigned and the semantic of the constraint enforces that the optimum and ONLY the optimum on the remaining variables is between lb and ub. Note: If a variable in the input problem does not exist in the current problem (with the same name), it is automatically added. Example: m=tb2.CFN(); m.Read("master.cfn");s=tb2.CFN();s.Read("slave.cfn");m.AddWeightedCSPConstraint(s, lb, ub);m.Solve() """ iscope = [] for i, v in enumerate(problem.VariableNames): if isinstance(v, str): vname = v v = self.VariableIndices.get(vname, -1) if (v < 0 or v >= len(self.VariableNames)): v = self.AddVariable(vname, [(problem.CFN.wcsp.getValueName(i, value) if len(problem.CFN.wcsp.getValueName(i, value)) > 0 else value) for value in problem.Domain(vname)]) if (v < 0 or v >= len(self.VariableNames)): raise RuntimeError("Out of range variable index:"+str(v)) iscope.append(v) multicfn = MultiCFN() multicfn.PushCFN(problem, -1) negproblem = CFN(vac = self.Option.vac, seed = self.Option.seed, verbose = self.Option.verbose) negproblem.InitFromMultiCFN(multicfn) negproblem.UpdateUB(1. - problem.GetLB()) # keep alive both problem and negproblem self.InternalCFNs.append(problem) self.InternalCFNs.append(negproblem) self.CFN.wcsp.postWeightedCSPConstraint(iscope, problem.CFN.wcsp, negproblem.CFN.wcsp, problem.CFN.wcsp.DoubletoCost(lb), problem.CFN.wcsp.DoubletoCost(ub), duplicateHard, strongDuality) def Read(self, filename): """Read reads the problem from a file. Args: filename (str): problem filename. """ self.CFN.read(filename) self.VariableIndices = {} self.VariableNames = [] for i in range(self.CFN.wcsp.numberOfVariables()): name = self.CFN.wcsp.getName(i) self.VariableIndices[name] = i self.VariableNames.append(name) def Parse(self, certificate): """Parse performs a list of elementary reduction operations on domains of variables. Args: certificate (str): a string composed of a list of operations on domains, each operation in the form ',varIndex[=#<>]value' where varIndex (int) is the index of a variable as returned by AddVariable and value (int) is a domain value (comma is mandatory even for the first operation, add no space). Possible operations are: assign ('='), remove ('#'), decrease maximum value ('<'), increase minimum value ('>'). Example: Parse(',0=1,1=1,2#0'): assigns the first and second variable to value 1 and remove value 0 from the third variable. """ self.CFN.parse_solution(certificate, False if self.configuration else True) # WARNING! False: do not reuse certificate in future searches used by structure learning evaluation procedure! def Dump(self, filename): """Dump outputs the problem in a file (without doing any preprocessing). Args: filename (str): problem filename. The suffix must be '.wcsp' or '.cfn' to select in which format to save the problem. """ if self.UbInit is not None: self.CFN.wcsp.updateDUb(self.UbInit) if '.wcsp' in filename: if self.CFN.wcsp.getNegativeLb() > 0 or tb2.option.decimalPoint != 0: print('Warning! Problem optimum has been' + (' multiplied by ' + str(10 ** tb2.option.decimalPoint) if tb2.option.decimalPoint != 0 else '') + (' and' if self.CFN.wcsp.getNegativeLb() > 0 and tb2.option.decimalPoint != 0 else '') + (' shifted by ' + str(self.CFN.wcsp.getNegativeLb()) if self.CFN.wcsp.getNegativeLb() > 0 else '') + ' in wcp format') self.CFN.dump_wcsp(filename, True, 1) elif '.cfn' in filename: self.CFN.dump_wcsp(filename, True, 2) else: print('Error unknown format!') def Print(self): """Print prints the content of the CFN (variables, cost functions). """ self.CFN.wcsp.print() def GetNbVars(self): """GetNbVars returns the number of variables. Returns: Number of variables (int). """ return self.CFN.wcsp.numberOfVariables() def Domain(self, var): """Domain returns the current domain of a given variable. Args: var (int|str): variable name or its index as returned by AddVariable. Returns: List of domain values (list). """ return self.CFN.wcsp.getEnumDomain(self.VariableIndices[var] if isinstance(var, str) else var) def GetNbConstrs(self): """GetNbConstrs returns the number of non-unary cost functions. Returns: Number of non-unary cost functions (int). """ return self.CFN.wcsp.numberOfConstraints() def GetLB(self): """GetLB returns the current problem lower bound. Returns: Current lower bound (decimal cost). """ return self.CFN.wcsp.getDDualBound() def GetUB(self): """GetUB returns the initial upper bound. Returns: Current initial upper bound (decimal cost). """ return self.CFN.wcsp.getDPrimalBound() # use only for decreasing current upper bound def UpdateUB(self, cost): """UpdateUB decreases the initial upper bound to a given value. Does nothing if this value is greater than the current upper bound. Args: cost (decimal cost): new initial upper bound. Warning: This operation might generate a Contradiction if the new upper bound is lower than or equal to the problem lower bound. """ self.CFN.wcsp.updateDUb(cost) self.CFN.wcsp.enforceUb() # this might generate a Contradiction exception def GetNbNodes(self): """GetNbNodes returns the number of search nodes explored so far. Returns: Current number of search nodes (int). """ return self.CFN.getNbNodes() def GetNbBacktracks(self): """GetNbBacktracks returns the number of backtracks done so far. Returns: Current number of backtracks (int). """ return self.CFN.getNbBacktracks() def GetSolutions(self): """GetSolutions returns all the solutions found so far with their associated costs. Returns: List of pairs (decimal cost, solution) where a solution is a list of domain values. """ return self.CFN.solutions() def GetDDualBound(self): """GetDDualBound returns the global problem lower bound in minimization (resp. upper bound in maximization) found after doing an incomplete search with Solve. Returns: Global lower bound (decimal cost). """ return self.CFN.getDDualBound() def GetName(self): """GetName get the name of the CFN. Returns: Name of the CFN (string). """ return self.CFN.wcsp.getName(name) def SetName(self, name): """SetName set the name of the CFN. Args: name (str): the new name of the CFN. """ self.CFN.wcsp.setName(name) return def NoPreprocessing(self): """NoPreprocessing deactivates most preprocessing methods. """ tb2.option.elimDegree = -1 tb2.option.elimDegree_preprocessing = -1 tb2.option.preprocessTernaryRPC = 0 tb2.option.preprocessFunctional = 0 tb2.option.costfuncSeparate = False tb2.option.preprocessNary = 0 tb2.option.DEE = 0 tb2.option.MSTDAC = False tb2.option.trwsAccuracy = -1 # non-incremental solving method def Solve(self, showSolutions = 0, allSolutions = 0, diversityBound = 0, timeLimit = 0, writeSolution = ''): """Solve solves the problem (i.e., finds its optimum and proves optimality). It can also enumerate (diverse) solutions depending on the arguments. Args: showSolutions (int): prints solution(s) found (0: show nothing, 1: domain values, 2: variable names with their assigned values, 3: variable and value names). allSolutions (int): if non-zero, enumerates all the solutions with a cost strictly better than the initial upper bound until a given limit on the number of solutions is reached. diversityBound (int): if non-zero, finds a greedy sequence of diverse solutions where a solution in the list is optimal such that it also has a Hamming-distance from the previously found solutions greater than a given bound. The number of diverse solutions is bounded by the argument value of allSolutions. timeLimit (int): CPU-time limit in seconds (or 0 if no time limit) writeSolution (str): write best solution found in a file using a given file name and using the same format as showSolutions (or write all solutions if allSolutions is non-zero) Returns: The best (or last if enumeration/diversity) solution found as a list of domain values, its associated cost, always strictly lower than the initial upper bound, and the number of solutions found (returned type: tuple(list, decimal cost, int)). or None if no solution has been found (the problem has no solution better than the initial upper bound or a search limit occurs). See GetSolutions to retrieve of the solutions found so far. See GetDDualBound to retrieve of the global problem dual bound found so far. Warning: This operation cannot be called multiple times on the same CFN object (it may modify the problem or its upper bound). """ tb2.option.showSolutions = showSolutions # show solutions found (0: none, 1: value indexes, 2: value names, 3: variable and value names if available) if len(writeSolution) > 0: if showSolutions > 0: tb2.option.writeSolution(str(showSolutions)) tb2.option.writeSolution(writeSolution) tb2.option.allSolutions = allSolutions # find all solutions up to a given maximum limit (or 0 if searching for the optimum) if diversityBound != 0 and allSolutions > 0: tb2.option.divNbSol = allSolutions tb2.option.divBound = diversityBound tb2.option.divMethod = 3 self.CFN.wcsp.initDivVariables() tb2.check() # checks compatibility between selected options self.Limit = None if (timeLimit > 0): self.CFN.timer(timeLimit) if self.UbInit is not None: self.CFN.wcsp.updateDUb(self.UbInit) self.CFN.wcsp.sortConstraints() solved = self.CFN.solve() if len(writeSolution) > 0: tb2.option.closeSolution() if (len(self.CFN.solutions()) > 0): if allSolutions > 0: return self.CFN.solutions()[-1][1], self.CFN.solutions()[-1][0], len(self.CFN.solutions()) # returns the last solution found else: return self.CFN.solution(), self.CFN.wcsp.getDPrimalBound(), len(self.CFN.solutions()) # returns the best solution found else: return None # incremental solving: perform initial preprocessing before all future searches, return improved ub def SolveFirst(self): """SolveFirst performs problem preprocessing before doing incremental solving. Returns: Initial upper bound (decimal cost), possibly improved by considering a worst-case situation based on the sum of maximum finite cost per function plus one. or None if the problem has no solution (a contradiction occurs during preprocessing). Warning: This operation must be done at solver depth 0 (see Depth). Warning: This operation cannot be called multiple times on the same CFN object. """ if self.UbInit is not None: self.CFN.wcsp.updateDUb(self.UbInit) tb2.check() # checks compatibility between selected options assert(self.Depth() == 0) self.Limit = None self.CFN.wcsp.sortConstraints() ub = self.CFN.wcsp.getUb() self.CFN.beginSolve(ub) try: ub = self.CFN.preprocessing(ub) except tb2.Contradiction: self.CFN.wcsp.whenContradiction() print('Problem has no solution!') return None return self.CFN.wcsp.Cost2ADCost(ub) # incremental solving: change initial upper bound up and down before adding any problem modifications def SetUB(self, cost): """SetUB resets the initial upper bound to a given value. It should be done before modifying the problem. Args: cost (decimal cost): new initial upper bound. """ icost = self.CFN.wcsp.DoubletoCost(cost) self.Limit = None self.CFN.wcsp.setUb(icost) # must be done after problem loading self.CFN.wcsp.initSolutionCost() # important to notify previous best found solution is no more valid self.CFN.wcsp.enforceUb() # this might generate a Contradiction exception # incremental solving: find the next (optimal) solution after a problem modification (see also SetUB) def SolveNext(self, showSolutions = 0, timeLimit = 0): """SolveNext solves the problem (i.e., finds its optimum and proves optimality). It should be done after calling SolveFirst and modifying the problem if necessary. Args: showSolutions (int): prints solution(s) found (0: show nothing, 1: domain values, 2: variable names with their assigned values, 3: variable and value names). timeLimit (int): CPU-time limit in seconds (or 0 if no time limit) Returns: The best solution found as a list of domain values, its associated cost, always strictly lower than the initial upper bound, and None (returned type: tuple(list, decimal cost, None)). or None if no solution has been found (the problem has no solution better than the initial upper bound or a search limit occurs, see Limit). """ tb2.option.showSolutions = showSolutions # show solutions found (0: none, 1: value indexes, 2: value names, 3: variable and value names if available) tb2.check() # checks compatibility between selected options self.Limit = None if (timeLimit > 0): self.CFN.timer(timeLimit) initub = self.CFN.wcsp.getUb() initdepth = tb2.store.getDepth() self.CFN.beginSolve(initub) tb2.option.hbfs = 1 # reinitialize this parameter which can be modified during hybridSolve() try: try: tb2.store.store() self.CFN.wcsp.propagate() lb, ub = self.CFN.hybridSolve() except tb2.Contradiction: self.CFN.wcsp.whenContradiction() except tb2.SolverOut as e: tb2.option.limit = False self.Limit = e tb2.store.restore(initdepth) if self.CFN.wcsp.getSolutionCost() < initub: return self.CFN.solution(), self.CFN.wcsp.getDPrimalBound(), None # warning! None: does not return number of found solutions because it is two slow to retrieve all solutions in python else: return None # The following functions allow user-defined search procedures: def Depth(self): """Depth returns the current solver depth value. Returns: Current solver depth value (int). """ return tb2.store.getDepth() # make a copy (incremental) of the current problem and move to Depth+1 def Store(self): """Store makes a copy (incremental) of the current problem and increases the solver depth by one. """ tb2.store.store() # restore previous copy made at a given depth def Restore(self, depth): """Restore retrieves the copy made at a given solver depth value. Args: depth (int): solver depth value. It must be lower than the current solver depth. """ tb2.store.restore(depth) def Assign(self, var, value): """Assign assigns a variable to a domain value. Args: var (int|str): variable name or its index as returned by AddVariable. value (int): domain value. """ self.CFN.wcsp.assign(self.VariableIndices[var] if isinstance(var, str) else var, value) self.CFN.wcsp.propagate() def MultipleAssign(self, vars, values): """MultipleAssign assigns several variables at once. Args: vars (list): list of indexes or names of variables. values (list): list of domain values. """ self.CFN.wcsp.assignLS([self.VariableIndices[var] if isinstance(var, str) else var for var in vars], values, false) def Remove(self, var, value): """Remove removes a value from the domain of a variable. Args: var (int|str): variable name or its index as returned by AddVariable. value (int): domain value. """ self.CFN.wcsp.remove(self.VariableIndices[var] if isinstance(var, str) else var, value) self.CFN.wcsp.propagate() def Increase(self, var, value): """Increase removes the first values strictly lower than a given value in the domain of a variable. Args: var (int|str): variable name or its index as returned by AddVariable. value (int): domain value. """ self.CFN.wcsp.increase(self.VariableIndices[var] if isinstance(var, str) else var, value) self.CFN.wcsp.propagate() def Decrease(self, var, value): """Decrease removes the last values strictly greater than a given value in the domain of a variable. Args: var (int|str): variable name or its index as returned by AddVariable. value (int): domain value. """ self.CFN.wcsp.decrease(self.VariableIndices[var] if isinstance(var, str) else var, value) self.CFN.wcsp.propagate() def Deconnect(self, var): """Deconnect deconnects a variable from the rest of the problem and assigns it to its support value. Args: var (int|str): variable name or its index as returned by AddVariable. """ varIndexes = [] varIndexes.append(self.VariableIndices[var] if isinstance(var, str) else var) self.MultipleDeconnect(varIndexes) def MultipleDeconnect(self, vars): """MultipleDeconnect deconnects a set of variables from the rest of the problem and assigns them to their support value. Args: vars (list): list of indexes or names of variables. """ self.CFN.wcsp.deconnect([self.VariableIndices[var] if isinstance(var, str) else var for var in vars]) def ClearPropagationQueues(self): """ClearPropagationQueues resets propagation queues. It should be called when an exception Contradiction occurs. """ self.CFN.wcsp.whenContradiction() def InitFromMultiCFN(self, multicfn): """InitFromMultiCFN initializes the cfn from a multiCFN instance (linear combination of multiple CFN). Args: multicfn (MultiCFN): the instance containing the CFNs. Note: After beeing initialized, it is possible to add cost functions to the CFN but the upper bound may be inconsistent. """ multicfn.MultiCFN.makeWeightedCSP(self.CFN.wcsp) return class MultiCFN: """pytoulbar2 base class used to combine linearly multiple CFN. Members: MultiCFN: python interface to C++ class MultiCFN. """ def __init__(self): self.MultiCFN = tb2.MultiCFN() return def PushCFN(self, CFN, weight=1.0): """PushCFN add a CFN to the instance. Args: CFN (CFN): the new CFN to add. weight (float): the initial weight of the CFN in the combination. """ if CFN.UbInit is not None: CFN.SetUB(CFN.UbInit) # might throw a contradiction # this should be done in the CFN class, but the update occurs only when solving the problem # this is because DoubletoCost function depends on the negCost and LB, which may be updated when adding cost functions # if CFN.UbInit is not None: # CFN.CFN.wcsp.updateDUb(CFN.UbInit) self.MultiCFN.push_back(CFN.CFN.wcsp, weight) def SetWeight(self, cfn_index, weight): """SetWeight set a weight of a CFN. Args: cfn_index (int): index of the CFN (in addition order). weight (float): the new weight of the CFN. """ self.MultiCFN.setWeight(cfn_index, weight) def GetSolution(self): """GetSolution returns the solution of a the combined cfn after being solved. Returns: The solution of the cfn (dic). """ return self.MultiCFN.getSolution() def GetSolutionCosts(self): """GetSolutionCosts returns the costs of the combined cfn after being solved. Returns: The costs of the solution of the cfn (list). """ return self.MultiCFN.getSolutionValues() def ApproximateParetoFront(self, first_criterion, first_direction, second_criterion, second_direction): """ApproximateParetoFront returns the set of supported solutions of the problem on two criteria (on the convex hull of the non dominated solutions). Args: first_criterion (int): index of the first CFN to optimize. first_direction (str): direction of the first criterion: 'min' or 'max'. second_criterion (int): index of the second CFN to optimize. second_direction (str): direction of the second criterion: 'min' or 'max'. Returns: The non dominated solutions belonging to the convex hull of the pareto front and their costs (tuple). """ optim_dir_first = (tb2.Bicriteria.OptimDir.Min if first_direction == 'min' else tb2.Bicriteria.OptimDir.Max) optim_dir_second = (tb2.Bicriteria.OptimDir.Min if second_direction == 'min' else tb2.Bicriteria.OptimDir.Max) tb2.option.verbose = -1 tb2.Bicriteria.computeSupportedPoints(self.MultiCFN, first_criterion, second_criterion, (optim_dir_first,optim_dir_second)) # tb2.Bicriteria.computeNonSupported(self.MultiCFN, (optim_dir_first,optim_dir_second), 500) return (tb2.Bicriteria.getSolutions(), tb2.Bicriteria.getPoints()) def Print(self): """Print print the content of the multiCFN: variables, cost functions. """ self.MultiCFN.print()
49,929
48.484638
372
py
toulbar2
toulbar2-master/pytoulbar2/tests/test_pytoulbar2.py
from unittest import TestCase import pytoulbar2 class TestExtension(TestCase): def test_1(self): myCFN = pytoulbar2.CFN(2) res = myCFN.Solve() self.assertEqual(res[0],[]) self.assertEqual(res[1],0.0) self.assertEqual(res[2],1)
263
21
34
py
toulbar2
toulbar2-master/pytoulbar2/tests/__init__.py
0
0
0
py
toulbar2
toulbar2-master/src/pytoulbar2testinc.py
""" Test incremental-solving pytoulbar2 API. Generates a random binary cost function network and solves a randomly-selected modified subproblem (without taking into account the rest of the problem). """ import sys import random random.seed() import pytoulbar2 # total maximum CPU time T=3 # number of variables N=100 # domain size D=10 # maximum finite cost C=100 # number of random binary cost functions E=1000 # number of variables in each subproblem M=10 # number of extra random binary cost functions in the subproblem S=100 # number of subproblems to be solved R=10 # create a new empty cost function network with no initial upper bound Problem = pytoulbar2.CFN() # create N variables with domain size of D for i in range(N): Problem.AddVariable('x' + str(i), range(D)) # add at-most E random binary cost functions on randomly-selected pairs of variables for e in range(E): i = random.randint(0, N-1) j = random.randint(0, N-1) if (i != j): Problem.AddFunction([i, j], [random.randint(0,C) for ival in range(D) for jval in range(D)]) # add a CPU-time limit in seconds Problem.CFN.timer(T) try: Problem.SolveFirst() # preprocessing is done only once initdepth = Problem.Depth() initub = Problem.GetUB() # solves R randomly-selected subproblems for r in range(R): try: Problem.Store() # makes a copy of the problem Problem.SetUB(initub) # reinitializes the initial upper bound subproblem = set() for i in range(M): subproblem.add(random.randint(0, N-1)) # selects at random M variables to be part of the current subproblem for i in range(N): if not i in subproblem: Problem.Deconnect(i) # remove the rest of the problem (unselected variables are fixed to their support value) subproblem = list(subproblem) # add S supplementary random binary cost functions to the subproblem for e in range(S): i = random.randint(0, len(subproblem)-1) j = random.randint(0, len(subproblem)-1) if (i != j): Problem.AddFunction([subproblem[i], subproblem[j]], [random.randint(0,C) for ival in range(D) for jval in range(D)], incremental=True) res = Problem.SolveNext() # finds the optimum of the subproblem print(subproblem,res, Problem.GetNbVars(), Problem.GetNbConstrs(), Problem.GetNbNodes(), Problem.GetNbBacktracks()) except Problem.Contradiction: Problem.ClearPropagationQueues() print(subproblem,'sorry, no solution found!') Problem.Restore(initdepth) # restore the original problem (without the supplementary random binary cost functions) except Exception as e: print(e,'sorry, we have been interrupted!')
2,870
34.8875
170
py
toulbar2
toulbar2-master/src/pytoulbar2test.py
""" Test basic pytoulbar2 API. """ import sys import random random.seed() import pytoulbar2 # create a new empty cost function network with 2-digit precision and initial upper bound of 100 Problem = pytoulbar2.CFN(100., resolution=2) # add three Boolean variables and a 4-value variable x = Problem.AddVariable('x', range(2)) y = Problem.AddVariable('y', range(2)) z = Problem.AddVariable('z', range(2)) u = Problem.AddVariable('u', range(4)) # add random unary cost functions on each variable Problem.AddFunction([x], [random.uniform(-99.9,99.9), random.uniform(-99.9,99.9)]) Problem.AddFunction([y], [random.uniform(-99.9,99.9), random.uniform(-99.9,99.9)]) Problem.AddFunction([z], [random.uniform(-99.9,99.9), random.uniform(-99.9,99.9)]) # add a soft equality constraint on each pair of variables (penalizes by a cost of 33.33 if variables are assigned to different values) Problem.AddFunction([x, y], [(0 if xvalue==yvalue else 33.33) for xvalue in range(2) for yvalue in range(2)]) Problem.AddFunction([x, z], [(0 if xvalue==zvalue else 33.33) for xvalue in range(2) for zvalue in range(2)]) Problem.AddFunction([y, z], [(0 if yvalue==zvalue else 33.33) for yvalue in range(2) for zvalue in range(2)]) # add a ternary hard constraint (x+y=z) Problem.AddFunction([x, y, z], [(0 if xvalue + yvalue == zvalue else Problem.Top) for xvalue in range(2) for yvalue in range(2) for zvalue in range(2)]) #an equivalent formulation with a compact cost table expressed by a list of allowed tuples and a corresponding list of zero costs (other tuples are forbidden due to the default cost set to Problem.Top) #Problem.AddCompactFunction([x, y ,z], Problem.Top, [(0,0,0),(0,1,1),(1,0,1)], [0] * 3) #an equivalent formulation with a linear hard constraint (by default, operator is '==' and rightcoef=0) #Problem.AddLinearConstraint([1, 1, -1], [x, y ,z]) # add a linear hard constraint (x + y + 2 * z >= 2) Problem.AddLinearConstraint([1, 1, 2], [x, y ,z], '>=', 2) #an equivalent formulation with a list of (variable, value, coefficient) #Problem.AddGeneralizedLinearConstraint([(x, 1, 1), (y, 1, 1), (z, 1, 2)], '>=', 2) #an equivalent formulation with a compact cost table expressed by a list of allowed tuples and a corresponding list of zero costs (other tuples are forbidden due to the default cost set to Problem.Top) #Problem.AddCompactFunction([x, y ,z], Problem.Top, [(0,0,1),(0,1,1),(1,0,1),(1,1,0),(1,1,1)], [0] * 5) # add a linear hard constraint (x + y + z == u) Problem.AddLinearConstraint([1, 1, 1, -1], [x, y ,z, u]) #an equivalent formulation with a compact cost table expressed by a list of allowed tuples and a corresponding list of zero costs (other tuples are forbidden due to the default cost set to Problem.Top) #Problem.AddCompactFunction([x, y ,z, u], Problem.Top, [(0,0,0,0),(0,0,1,1),(0,1,0,1),(1,0,0,1),(0,1,1,2),(1,0,1,2),(1,1,0,2),(1,1,1,3)], [0] * 8) # add a hard global alldifferent constraint on variables y,z,u Problem.AddAllDifferent([y,z,u]) # add a 1-hour CPU-time limit in seconds Problem.CFN.timer(3600) try: res = Problem.Solve() # or if you want to get a greedy sequence of diverse solutions: Problem.Solve(showSolutions=2, allSolutions=10, diversityBound=2) except Exception as e: print(e) if len(Problem.CFN.solutions()) > 0: res = [Problem.CFN.solution(), Problem.CFN.wcsp.getDPrimalBound(), len(Problem.CFN.solutions())] else: res = None if res and len(res[0])==Problem.CFN.wcsp.numberOfVariables(): print('Solution found: x=' + str(res[0][0]) + ', y=' + str(res[0][1]) + ', z=' + str(res[0][2]) + ', u=' + str(res[0][3]) + ' with cost ' + str(res[1])) else: print('Sorry, no solution found!') print(Problem.CFN.solutions())
3,717
52.884058
201
py
toulbar2
toulbar2-master/web/TUTORIALS/bicriteria_latinsquare.py
import sys from random import seed, randint seed(123456789) import pytoulbar2 from matplotlib import pyplot as plt N = int(sys.argv[1]) top = N**3 +1 # printing a solution as a grid def print_solution(sol, N): grid = [0 for _ in range(N*N)] for k,v in sol.items(): grid[ int(k[5])*N+int(k[7]) ] = int(v[1:]) output = '' for var_ind in range(len(sol)): output += str(grid[var_ind]) + ' ' if var_ind % N == N-1: output += '\n' print(output, end='') # creation of the base problem: variables and hard constraints (alldiff must be decomposed into binary constraints) def create_base_cfn(cfn, N, top): # variable creation var_indexes = [] # create N^2 variables, with N values in their domains for row in range(N): for col in range(N): index = cfn.AddVariable('Cell_' + str(row) + '_' + str(col), ['v' + str(val) for val in range(N)]) var_indexes.append(index) # all permutation constraints: pairwise all different # forbidden values are enforced by infinite costs alldiff_costs = [ top if row == col else 0 for row in range(N) for col in range(N) ] for index in range(N): for var_ind1 in range(N): for var_ind2 in range(var_ind1+1, N): # permutations in the rows cfn.AddFunction([var_indexes[N*index+var_ind1], var_indexes[N*index+var_ind2]], alldiff_costs) # permutations in the columns cfn.AddFunction([var_indexes[index+var_ind1*N], var_indexes[index+var_ind2*N]], alldiff_costs) split_index = (N*N)//2 # generation of random costs cell_costs = [[randint(1,N) for _ in range(N)] for _ in range(N*N)] # multicfn is the main object for combining multiple cost function networks multicfn = pytoulbar2.MultiCFN() # first cfn: first half of the grid cfn = pytoulbar2.CFN(ubinit = top, resolution=6) cfn.SetName('first half') create_base_cfn(cfn, N, top) for variable_index in range(split_index): cfn.AddFunction([variable_index], cell_costs[variable_index]) multicfn.PushCFN(cfn) # second cfn: second half of the grid cfn = pytoulbar2.CFN(ubinit = top, resolution=6) cfn.SetName('second half') create_base_cfn(cfn, N, top) for variable_index in range(split_index+1, N*N): cfn.AddFunction([variable_index], cell_costs[variable_index]) multicfn.PushCFN(cfn) # solve with a first pair of weights weights = (1., 2.) multicfn.SetWeight(0, weights[0]) multicfn.SetWeight(1, weights[1]) cfn = pytoulbar2.CFN() cfn.InitFromMultiCFN(multicfn) # the final cfn is initialized from the combined cfn # cfn.Dump('python_latin_square_bicriteria.cfn') result = cfn.Solve() if result: print('Solution found with weights', weights, ':') sol_costs = multicfn.GetSolutionCosts() solution = multicfn.GetSolution() print_solution(solution, N) print('with costs:', sol_costs, '(sum=', result[1], ')') print('\n') # solve a second time with other weights weights = (2.5, 1.) multicfn.SetWeight(0, weights[0]) multicfn.SetWeight(1, weights[1]) cfn = pytoulbar2.CFN() cfn.InitFromMultiCFN(multicfn) # the final cfn is initialized from the combined cfn # cfn.Dump('python_latin_square_bicriteria.cfn') result = cfn.Solve() if result: print('Solution found with weights', weights, ':') sol_costs = multicfn.GetSolutionCosts() solution = multicfn.GetSolution() print_solution(solution, N) print('with costs:', sol_costs, '(sum=', result[1], ')') # approximate the pareto front (solutions, costs) = multicfn.ApproximateParetoFront(0, 'min', 1, 'min') fig, ax = plt.subplots() ax.scatter([c[0] for c in costs], [c[1] for c in costs], marker='x') for index in range(len(costs)-1): ax.plot([costs[index][0], costs[index+1][0]], [costs[index][1],costs[index+1][1]], '--', c='k') ax.plot([costs[index][0], costs[index+1][0]], [costs[index][1],costs[index][1]], '--', c='red') ax.plot([costs[index+1][0], costs[index+1][0]], [costs[index][1],costs[index+1][1]], '--', c='red') ax.set_xlabel('first half cost') ax.set_ylabel('second half cost') ax.set_title('approximation of the pareto front') ax.set_aspect('equal') plt.grid() plt.show()
4,079
28.142857
115
py
toulbar2
toulbar2-master/web/TUTORIALS/rcpsp.py
# Resource-Constrained Project Scheduling Problem # Example taken from PyCSP3 COP model RCPSP # http://pycsp.org/documentation/models/COP/RCPSP import sys import pytoulbar2 horizon = 158 capacities = [12, 13, 4, 12] job_durations = [0, 8, 4, 6, 3, 8, 5, 9, 2, 7, 9, 2, 6, 3, 9, 10, 6, 5, 3, 7, 2, 7, 2, 3, 3, 7, 8, 3, 7, 2, 2, 0] job_successors = [[1, 2, 3], [5, 10, 14], [6, 7, 12], [4, 8, 9], [19], [29], [26], [11, 18, 26], [13], [15, 24], [19, 25], [13], [16, 17], [16], [24], [20, 21], [21], [19, 21], [23, 28], [22, 24], [27], [22], [23], [29], [29], [30], [27], [30], [31], [31], [31], []] job_requirements = [[0, 0, 0, 0], [4, 0, 0, 0], [10, 0, 0, 0], [0, 0, 0, 3], [3, 0, 0, 0], [0, 0, 0, 8], [4, 0, 0, 0], [0, 1, 0, 0], [6, 0, 0, 0], [0, 0, 0, 1], [0, 5, 0, 0], [0, 7, 0, 0], [4, 0, 0, 0], [0, 8, 0, 0], [3, 0, 0, 0], [0, 0, 0, 5], [0, 0, 0, 8], [0, 0, 0, 7], [0, 1, 0, 0], [0, 10, 0, 0], [0, 0, 0, 6], [2, 0, 0, 0], [3, 0, 0, 0], [0, 9, 0, 0], [4, 0, 0, 0], [0, 0, 4, 0], [0, 0, 0, 7], [0, 8, 0, 0], [0, 7, 0, 0], [0, 7, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]] N = len(job_durations) top = 44 # give a good initial upper-bound Problem = pytoulbar2.CFN(top) for i in range(N): Problem.AddVariable('x' + str(i), range(horizon)) # first job starts at 0 Problem.AddFunction([0], [0 if a==0 else top for a in range(horizon)]) # precedence constraints for i in range(N): for j in job_successors[i]: Problem.AddFunction([i, j ], [(0 if a + job_durations[i] <= b else top) for a in range(horizon) for b in range(horizon)]) # for each ressource and each time slot, we post a linear constraint on all the jobs that require this ressource to not overcome the ressoure capacity for k, capacity in enumerate(capacities): for a in range(horizon): List = [] for i in range(N): if job_requirements[i][k] > 0: for b in range(horizon): if a >= b and a < b + job_durations[i]: List.append(('x' +str(i), a, job_requirements[i][k])) if len(List) > 0: Problem.AddGeneralizedLinearConstraint(List, operand='<=', rightcoef=capacity) print(List) # minimize makespan, i.e., the completion time of the last job Problem.AddFunction([N-1], [a for a in range(horizon)]) #Problem.Option.verbose = 0 #Problem.Option.showSolutions = 1 # returns (optimal solution, optimum value, number of solutions found) print(Problem.Solve())
2,370
38.516667
150
py
toulbar2
toulbar2-master/web/TUTORIALS/blockmodel2.py
import sys def flatten(x): result = [] for el in x: if hasattr(el, "__iter__") and not isinstance(el, str) and not isinstance(el, tuple) and not isinstance(el, dict): result.extend(flatten(el)) else: result.append(el) return result def cfn(problem, isMinimization, initPrimalBound, floatPrecision=0): globals_key_order = ["rhs", "capacity", "weights", "weightedvalues", "metric", "cost", "bounds", "vars1", "vars2", "nb_states", "starts", "ends", "transitions", "nb_symbols", "nb_values", "start", "terminals", "non_terminals", "min", "max", "values", "defaultcost", "tuples", "comparator", "to"] print('{') print('\tproblem: { "name": "%s", "mustbe": "%s%.*f" },' % (problem["name"], "<" if (isMinimization) else ">", floatPrecision, initPrimalBound)) print('\tvariables: {', end='') for i,e in enumerate(flatten(problem["variables"])): if i > 0: print(', ', end='') print('"%s":' % e[0], end='') if isinstance(e[1], int): print(' %s' % e[1], end='') else: print('[', end='') for j,a in enumerate(e[1]): if j > 0: print(', ', end='') print('"%s"' % a, end='') print(']', end='') print('},') print( '\tfunctions: {') for i,e in enumerate(flatten(problem["functions"])): if i > 0: print(',') if e.get("name") is not None: print('\t\t"%s": {scope: [' % e.get("name"), end='') else: print('\t\t{scope: [', end='') scope = {} for j,x in enumerate(e.get("scope")): if (x in scope): sys.exit(str(e) + '\nError: scope of function ' + str(i) + ' with the same variable twice is forbidden!') if j > 0: print(', ', end='') print('"%s"' % x, end='') scope[x]=j print('], ', end='') if e.get("type") is not None: print('"type:" %s, ' % e.get("type"), end='') if e.get("params") is not None: if isinstance(e.get("params"), dict): print('"params": {', end='') first = True for key in globals_key_order: if key in e.get("params"): if not first: print(', ', end='') if isinstance(e.get("params")[key], str): print('"%s": "%s"' % (str(key),str(e.get("params")[key]).replace("'", '"')), end='') else: print('"%s": %s' % (str(key),str(e.get("params")[key]).replace("'", '"')), end='') first = False print ('}', end='') else: print('"params": %s, ' % str(e.get("params")).replace("'",'"'), end='') if e.get("defaultcost") is not None: print('"defaultcost:" %s, ' % e.get("defaultcost"), end='') if e.get("costs") is not None: print('"costs": ', end='') if isinstance(e.get("costs"), str): print('"%s"' % e.get("costs"), end='') # reuse future cost function by giving its name here else: print('[', end='') for j,c in enumerate(e.get("costs")): if j > 0: print(', ', end='') if isinstance(c, str) and not c.isdigit(): print('"%s"' % c, end='') else: print('%s' % c, end='') print(']', end='') print('}', end='') print('}\n}') class Data: def __init__(self, filename, k): lines = open(filename).readlines() self.n = len(lines) self.matrix = [[int(e) for e in l.split(' ')] for l in lines] self.top = 1 + self.n*self.n def model(data, K): symmetric = all([data.matrix[i][j] == data.matrix[j][i] for i in range(data.n) for j in range(data.n) if j>i]) Var = [(chr(65 + i) if data.n < 28 else "x" + str(i)) for i in range(data.n)] # sort node variables by decreasing out degree degree = [(i, sum(data.matrix[i])) for i in range(data.n)] degree.sort(key=lambda tup: -tup[1]) indexes = [e[0] for e in degree] BlockModeling = { "name": "BlockModel_N" + str(data.n) + "_K" + str(K) + "_Sym" + str(symmetric), # order node variables before matrix M variables # order matrix M variables starting from the main diagonal and moving away progressively # if input graph is symmetric then keep only the upper triangular matrix of M "variables": [[("M_" + str(u) + "_" + str(u), 2) for u in range(K)], [("M_" + str(u) + "_" + str(v), 2) for d in range(K) for u in range(K) for v in range(K) if u != v and (not symmetric or u < v) and abs(u - v) == d], [(Var[indexes[i]], K) for i in range(data.n)]], "functions": [ # objective function # if input graph is symmetric then cost tables are also symmetric wrt node variables [{"scope": ["M_" + str(u) + "_" + str(v), Var[indexes[i]], Var[indexes[j]]], "costs": [1 if (((u == k and v == l) or (symmetric and u == l and v == k)) and data.matrix[indexes[i]][indexes[j]] != m) else 0 for m in range(2) for k in range(K) for l in range(K)]} for u in range(K) for v in range(K) for i in range(data.n) for j in range(data.n) if i != j and (not symmetric or u <= v)], # self-loops [{"scope": ["M_" + str(u) + "_" + str(u), Var[indexes[i]]], "costs": [1 if (u == k and data.matrix[indexes[i]][indexes[i]] != m) else 0 for m in range(2) for k in range(K)]} for u in range(K) for i in range(data.n)], # breaking partial symmetries by fixing first (K-1) domain variables to be assigned to cluster less than or equal to their index [{"scope": [Var[indexes[l]]], "costs": [data.top if k > l else 0 for k in range(K)]} for l in range(K-1)] ] } return BlockModeling if __name__ == '__main__': # read parameters if len(sys.argv) < 2: exit('Command line argument is problem data filename and number of blocks') K = int(sys.argv[2]) data = Data(sys.argv[1], K) # dump problem into JSON .cfn format for minimization by toulbar2 solver cfn(model(data, K), True, data.top)
6,646
47.518248
299
py
toulbar2
toulbar2-master/web/TUTORIALS/blockmodel.py
import sys import pytoulbar2 #read adjency matrix of graph G Lines = open(sys.argv[1], 'r').readlines() GMatrix = [[int(e) for e in l.split(' ')] for l in Lines] N = len(Lines) Top = N*N + 1 K = int(sys.argv[2]) #give names to node variables Var = [(chr(65 + i) if N < 28 else "x" + str(i)) for i in range(N)] # Political actor or any instance # Var = ["ron","tom","frank","boyd","tim","john","jeff","jay","sandy","jerry","darrin","ben","arnie"] # Transatlantic # Var = ["justin","harry","whit","brian","paul","ian","mike","jim","dan","ray","cliff","mason","roy"] # Sharpstone # Var = ["Sherrif","CivilDef","Coroner","Attorney","HighwayP","ParksRes","GameFish","KansasDOT","ArmyCorps","ArmyReserve","CrableAmb","FrankCoAmb","LeeRescue","Shawney","BurlPolice","LyndPolice","RedCross","TopekaFD","CarbFD","TopekaRBW"] # Kansas Problem = pytoulbar2.CFN(Top) #create a Boolean variable for each coefficient of the M GMatrix for u in range(K): for v in range(K): Problem.AddVariable("M_" + str(u) + "_" + str(v), range(2)) #create a domain variable for each node in graph G for i in range(N): Problem.AddVariable(Var[i], range(K)) #general case for each edge in G for u in range(K): for v in range(K): for i in range(N): for j in range(N): if i != j: ListCost = [] for m in range(2): for k in range(K): for l in range(K): if (u == k and v == l and GMatrix[i][j] != m): ListCost.append(1) else: ListCost.append(0) Problem.AddFunction(["M_" + str(u) + "_" + str(v), Var[i], Var[j]],ListCost) # self-loops must be treated separately as they involves only two variables for u in range(K): for i in range(N): ListCost = [] for m in range(2): for k in range(K): if (u == k and GMatrix[i][i] != m): ListCost.append(1) else: ListCost.append(0) Problem.AddFunction(["M_" + str(u) + "_" + str(u), Var[i]], ListCost) # breaking partial symmetries by fixing first (K-1) domain variables to be assigned to a cluster number less than or equal to their index for l in range(K-1): Constraint = [] for k in range(K): if k > l: Constraint.append(Top) else: Constraint.append(0) Problem.AddFunction([Var[l]], Constraint) Problem.Dump(sys.argv[1].replace('.mat','.cfn')) Problem.CFN.timer(300) res = Problem.Solve(showSolutions = 3) if res: print("M matrix:") for u in range(K): Line = [] for v in range(K): Line.append(res[0][u*K+v]) print(Line) for k in range(K): for i in range(N): if res[0][K**2+i] == k: print("Node",Var[i],"with index",str(i),"is in cluster",str(res[0][K**2+i]))
3,036
34.729412
250
py
toulbar2
toulbar2-master/web/TUTORIALS/mendel.py
import sys import pytoulbar2 class Data: def __init__(self, ped): self.id = list() self.father = {} self.mother = {} self.allelesId = {} self.ListAlle = list() self.obs = 0 stream = open(ped) for line in stream: (locus, id, father, mother, sex, allele1, allele2) = line.split()[:] self.id.append(int(id)) self.father[int(id)] = int(father) self.mother[int(id)] = int(mother) self.allelesId[int(id)] = (int(allele1), int(allele2)) if int(allele1) < int(allele2) else (int(allele2), int(allele1)) if not(int(allele1) in self.ListAlle) and int(allele1) != 0: self.ListAlle.append(int(allele1)) if int(allele2) != 0 and not(int(allele2) in self.ListAlle): self.ListAlle.append(int(allele2)) if int(allele1) != 0 or int(allele2) != 0: self.obs += 1 #collect data data = Data(sys.argv[1]) top = int(data.obs+1) Problem = pytoulbar2.CFN(top) #create a variable for each individual for i in data.id: domains = [] for a1 in data.ListAlle: for a2 in data.ListAlle: if a1 <= a2: domains.append('a'+str(a1)+'a'+str(a2)) Problem.AddVariable('g' + str(i) , domains) #create the constraints that represent the mendel's laws ListConstraintsMendelLaw = [] for p1 in data.ListAlle: for p2 in data.ListAlle: if p1 <= p2: # father alleles for m1 in data.ListAlle: for m2 in data.ListAlle: if m1 <= m2: # mother alleles for a1 in data.ListAlle: for a2 in data.ListAlle: if a1 <= a2: # child alleles if (a1 in (p1,p2) and a2 in (m1,m2)) or (a2 in (p1,p2) and a1 in (m1,m2)) : ListConstraintsMendelLaw.append(0) else : ListConstraintsMendelLaw.append(top) for i in data.id: #ternary constraints representing mendel's laws if data.father.get(i, 0) != 0 and data.mother.get(i, 0) != 0: Problem.AddFunction(['g' + str(data.father[i]),'g' + str( data.mother[i]), 'g' + str(i)], ListConstraintsMendelLaw) #unary constraints linked to the observations if data.allelesId[i][0] != 0 and data.allelesId[i][1] != 0: ListConstraintsObservation = [] for a1 in data.ListAlle: for a2 in data.ListAlle: if a1 <= a2: if (a1,a2) == data.allelesId[i]: ListConstraintsObservation.append(0) else : ListConstraintsObservation.append(1) Problem.AddFunction(['g' + str(i)], ListConstraintsObservation) #Problem.Dump('Mendel.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions=3) if res: print('There are',int(res[1]),'difference(s) between the solution and the observation.') else: print('No solution found')
2,569
30.341463
122
py
toulbar2
toulbar2-master/web/TUTORIALS/airland.py
import sys import pytoulbar2 f = open(sys.argv[1], 'r').readlines() tokens = [] for l in f: tokens += l.split() pos = 0 def token(): global pos, tokens if (pos == len(tokens)): return None s = tokens[pos] pos += 1 return int(float(s)) N = token() token() # skip freeze time LT = [] PC = [] ST = [] for i in range(N): token() # skip appearance time # Times per plane: {earliest landing time, target landing time, latest landing time} LT.append([token(), token(), token()]) # Penalty cost per unit of time per plane: # [for landing before target, after target] PC.append([token(), token()]) # Separation time required after i lands before j can land ST.append([token() for j in range(N)]) top = 99999 Problem = pytoulbar2.CFN(top) for i in range(N): Problem.AddVariable('x' + str(i), range(LT[i][0],LT[i][2]+1)) for i in range(N): ListCost = [] for a in range(LT[i][0], LT[i][2]+1): if a < LT[i][1]: ListCost.append(PC[i][0]*(LT[i][1] - a)) else: ListCost.append(PC[i][1]*(a - LT[i][1])) Problem.AddFunction([i], ListCost) for i in range(N): for j in range(i+1,N): Constraint = [] for a in range(LT[i][0], LT[i][2]+1): for b in range(LT[j][0], LT[j][2]+1): if a+ST[i][j]>b and b+ST[j][i]>a: Constraint.append(top) else: Constraint.append(0) Problem.AddFunction([i, j],Constraint) #Problem.Dump('airplane.cfn') Problem.NoPreprocessing() Problem.Solve(showSolutions = 3)
1,599
22.188406
84
py
toulbar2
toulbar2-master/web/TUTORIALS/rlfap.py
import sys import pytoulbar2 class Data: def __init__(self, var, dom, ctr, cst): self.var = list() self.dom = {} self.ctr = list() self.cost = {} self.nba = {} self.nbb = {} self.top = 1 self.Domain = {} stream = open(var) for line in stream: if len(line.split())>=4: (varnum, vardom, value, mobility) = line.split()[:4] self.Domain[int(varnum)] = int(vardom) self.var.append((int(varnum), int(vardom), int(value), int(mobility))) self.nbb["b" + str(mobility)] = self.nbb.get("b" + str(mobility), 0) + 1 else: (varnum, vardom) = line.split()[:2] self.Domain[int(varnum)] = int(vardom) self.var.append((int(varnum), int(vardom))) stream = open(dom) for line in stream: domain = line.split()[:] self.dom[int(domain[0])] = [int(f) for f in domain[2:]] stream = open(ctr) for line in stream: (var1, var2, dummy, operand, deviation, weight) = line.split()[:6] self.ctr.append((int(var1), int(var2), operand, int(deviation), int(weight))) self.nba["a" + str(weight)] = self.nba.get("a" + str(weight), 0) + 1 stream = open(cst) for line in stream: if len(line.split()) == 3: (aorbi, eq, cost) = line.split()[:3] if (eq == "="): self.cost[aorbi] = int(cost) self.top += int(cost) * self.nba.get(aorbi, self.nbb.get(aorbi, 0)) #collect data data = Data(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) top = data.top Problem = pytoulbar2.CFN(top) #create a variable for each link for e in data.var: domain = [] for f in data.dom[e[1]]: domain.append('f' + str(f)) Problem.AddVariable('link' + str(e[0]), domain) #binary hard and soft constraints for (var1, var2, operand, deviation, weight) in data.ctr: ListConstraints = [] for a in data.dom[data.Domain[var1]]: for b in data.dom[data.Domain[var2]]: if ((operand==">" and abs(a - b) > deviation) or (operand=="=" and abs(a - b) == deviation)): ListConstraints.append(0) else: ListConstraints.append(data.cost.get('a' + str(weight),top)) Problem.AddFunction(['link' + str(var1), 'link' + str(var2)], ListConstraints) #unary hard and soft constraints for e in data.var: if len(e) >= 3: ListConstraints = [] for a in data.dom[e[1]]: if a == e[2]: ListConstraints.append(0) else: ListConstraints.append(data.cost.get('b' + str(e[3]),top)) Problem.AddFunction(['link' + str(e[0])], ListConstraints) #Problem.Dump('Rflap.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions=3) if res: print("Best solution found with cost:",int(res[1]),"in", Problem.GetNbNodes(), "search nodes.") else: print('Sorry, no solution found!')
2,662
28.921348
96
py
toulbar2
toulbar2-master/web/TUTORIALS/weightedqueens.py
import sys from random import seed, randint seed(123456789) import pytoulbar2 N = int(sys.argv[1]) top = N**2 +1 Problem = pytoulbar2.CFN(top) for i in range(N): Problem.AddVariable('Q' + str(i+1), ['row' + str(a+1) for a in range(N)]) for i in range(N): for j in range(i+1,N): #Two queens cannot be on the same row constraints ListConstraintsRow = [] for a in range(N): for b in range(N): if a != b : ListConstraintsRow.append(0) else: ListConstraintsRow.append(top) Problem.AddFunction([i, j], ListConstraintsRow) #Two queens cannot be on the same upper diagonal constraints ListConstraintsUpperD = [] for a in range(N): for b in range(N): if a + i != b + j : ListConstraintsUpperD.append(0) else: ListConstraintsUpperD.append(top) Problem.AddFunction([i, j], ListConstraintsUpperD) #Two queens cannot be on the same lower diagonal constraints ListConstraintsLowerD = [] for a in range(N): for b in range(N): if a - i != b - j : ListConstraintsLowerD.append(0) else: ListConstraintsLowerD.append(top) Problem.AddFunction([i, j], ListConstraintsLowerD) #Random unary costs for i in range(N): ListConstraintsUnaryC = [] for j in range(N): ListConstraintsUnaryC.append(randint(1,N)) Problem.AddFunction([i], ListConstraintsUnaryC) #Problem.Dump('WeightQueen.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions = 3) if res: for i in range(N): row = ['X' if res[0][j]==i else ' ' for j in range(N)] print(row) # and its cost print("Cost:", int(res[1]))
1,594
23.166667
77
py
toulbar2
toulbar2-master/web/TUTORIALS/warehouse.py
import sys import pytoulbar2 f = open(sys.argv[1], 'r').readlines() precision = int(sys.argv[2]) # in [0,9], used to convert cost values from float to integer (by 10**precision) tokens = [] for l in f: tokens += l.split() pos = 0 def token(): global pos, tokens if pos == len(tokens): return None s = tokens[pos] pos += 1 return s N = int(token()) # number of warehouses M = int(token()) # number of stores top = 1 # sum of all costs plus one CostW = [] # maintenance cost of warehouses Capacity = [] # capacity limit of warehouses (not used) for i in range(N): Capacity.append(token()) CostW.append(int(float(token()) * 10.**precision)) top += sum(CostW) Demand = [] # demand for each store CostS = [[] for i in range(M)] # supply cost matrix for j in range(M): Demand.append(int(token())) for i in range(N): CostS[j].append(int(float(token()) * 10.**precision)) top += sum(CostS[j]) # create a new empty cost function network Problem = pytoulbar2.CFN(top) # add warehouse variables for i in range(N): Problem.AddVariable('w' + str(i), range(2)) # add store variables for j in range(M): Problem.AddVariable('s' + str(j), range(N)) # add maintenance costs for i in range(N): Problem.AddFunction([i], [0, CostW[i]]) # add supply costs for each store for j in range(M): Problem.AddFunction([N+j], CostS[j]) # add channeling constraints between warehouses and stores for i in range(N): for j in range(M): Constraint = [] for a in range(2): for b in range(N): if a == 0 and b == i: Constraint.append(top) else: Constraint.append(0) Problem.AddFunction([i, N+j], Constraint) #Problem.Dump('warehouse.cfn') Problem.Solve(showSolutions=3)
1,844
23.6
110
py
toulbar2
toulbar2-master/web/TUTORIALS/golomb.py
import sys import pytoulbar2 N = int(sys.argv[1]) top = N**2 + 1 Problem = pytoulbar2.CFN(top) #create a variable for each mark for i in range(N): Problem.AddVariable('X' + str(i), range(N**2)) #ternary constraints to link new variables of difference with the original variables for i in range(N): for j in range(i+1, N): Problem.AddVariable('X' + str(j) + '-X' + str(i), range(N**2)) Constraint = [] for k in range(N**2): for l in range(N**2): for m in range(N**2): if l-k == m: Constraint.append(0) else: Constraint.append(top) Problem.AddFunction(['X' + str(i), 'X' + str(j), 'X' + str(j) + '-X' + str(i)], Constraint) Problem.AddAllDifferent(['X' + str(j) + '-X' + str(i) for i in range(N) for j in range(i+1,N)]) Problem.AddFunction(['X' + str(N-1)], range(N**2)) #fix the first mark to be zero Problem.AddFunction(['X0'], [0] + [top] * (N**2 - 1)) #Problem.Dump('golomb.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions=3) if res: ruler = '0' for i in range(1,N): ruler += ' '*(res[0][i]-res[0][i-1]-1) + str(res[0][i]) print('Golomb ruler of size:',int(res[1])) print(ruler)
1,285
27.577778
99
py
toulbar2
toulbar2-master/web/TUTORIALS/square.py
import sys from random import randint, seed seed(123456789) import pytoulbar2 try: N = int(sys.argv[1]) S = int(sys.argv[2]) assert N <= S except: print('Two integers need to be given as arguments: N and S') exit() #pure constraint satisfaction problem Problem = pytoulbar2.CFN(1) #create a variable for each square for i in range(N): Problem.AddVariable('sq' + str(i+1), ['(' + str(l) + ',' + str(j) + ')' for l in range(S-i) for j in range(S-i)]) #binary hard constraints for overlapping squares for i in range(N): for j in range(i+1,N): ListConstraintsOverlaps = [] for a in [S*k+l for k in range(S-i) for l in range(S-i)]: for b in [S*m+n for m in range(S-j) for n in range(S-j)]: #calculating the coordinates of the squares X_i = a%S X_j = b%S Y_i = a//S Y_j = b//S #calculating if squares are overlapping if X_i >= X_j : if X_i - X_j < j+1: if Y_i >= Y_j: if Y_i - Y_j < j+1: ListConstraintsOverlaps.append(1) else: ListConstraintsOverlaps.append(0) else: if Y_j - Y_i < i+1: ListConstraintsOverlaps.append(1) else: ListConstraintsOverlaps.append(0) else: ListConstraintsOverlaps.append(0) else : if X_j - X_i < i+1: if Y_i >= Y_j: if Y_i - Y_j < j+1: ListConstraintsOverlaps.append(1) else: ListConstraintsOverlaps.append(0) else: if Y_j - Y_i < i+1: ListConstraintsOverlaps.append(1) else: ListConstraintsOverlaps.append(0) else: ListConstraintsOverlaps.append(0) Problem.AddFunction(['sq' + str(i+1), 'sq' + str(j+1)], ListConstraintsOverlaps) #Problem.Dump('Square.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions=3) if res: for i in range(S): row = '' for j in range(S): row += ' ' for k in range(N-1, -1, -1): if (res[0][k]%(S-k) <= j and j - res[0][k]%(S-k) <= k) and (res[0][k]//(S-k) <= i and i - res[0][k]//(S-k) <= k): row = row[:-1] + chr(65 + k) print(row) else: print('No solution found!')
2,078
26
117
py
toulbar2
toulbar2-master/web/TUTORIALS/boardcoloration.py
import sys from random import randint, seed seed(123456789) import pytoulbar2 try: n = int(sys.argv[1]) m = int(sys.argv[2]) except: print('Two integer need to be in arguments: number of rows n, number of columns m') exit() top = n*m + 1 Problem = pytoulbar2.CFN(top) #create a variable for each cell for i in range(n): for j in range(m): Problem.AddVariable('sq(' + str(i) + ',' + str(j) + ')', range(n*m)) #create a variable for the maximum of colors Problem.AddVariable('max', range(n*m)) #quaterny hard constraints for rectangle with same color angles #for each cell on the chessboard for i1 in range(n): for i2 in range(m): #for every cell on the chessboard that could form a rectangle with the first cell as up left corner and this cell as down right corner for j1 in range(i1+1, n): for j2 in range(i2+1, m): Constraint = [] for k in range(n*m): for l in range(n*m): for o in range(n*m): for p in range(n*m): if k ==l and l == o and o == p: #if they are all the same color Constraint.append(top) else: Constraint.append(0) Problem.AddFunction(['sq(' + str(i1) + ',' + str(i2) + ')', 'sq(' + str(i1) + ',' + str(j2) + ')', 'sq(' + str(j1) + ',' + str(i2) + ')', 'sq(' + str(j1) + ',' + str(j2) + ')'], Constraint) #binary hard constraints to fix the variable max as an upper bound for i in range(n): for j in range(m): Constraint = [] for k in range(n*m): for l in range(n*m): if k>l: #if the color of the square is more than the number of the max Constraint.append(top) else: Constraint.append(0) Problem.AddFunction(['sq(' + str(i) + ',' + str(j) + ')', 'max'], Constraint) #minimize the number of colors Problem.AddFunction(['max'], range(n*m)) #Problem.Dump('boardcoloration.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions =3) if res: for i in range(n): row = [] for j in range(m): row.append(res[0][m*i+j]) print(row) else: print('No solutions found') 70,2-9 Bas
2,496
33.680556
205
py
toulbar2
toulbar2-master/web/TUTORIALS/fapp.py
import sys import pytoulbar2 class Data: def __init__(self, filename, k): self.var = {} self.dom = {} self.ctr = list() self.softeq = list() self.softne = list() self.nbsoft = 0 stream = open(filename) for line in stream: if len(line.split())==3 and line.split()[0]=="DM": (DM, dom, freq) = line.split()[:3] if self.dom.get(int(dom)) is None: self.dom[int(dom)] = [int(freq)] else: self.dom[int(dom)].append(int(freq)) if len(line.split()) == 4 and line.split()[0]=="TR": (TR, route, dom, polarisation) = line.split()[:4] if int(polarisation) == 0: self.var[int(route)] = [(f,-1) for f in self.dom[int(dom)]] + [(f,1) for f in self.dom[int(dom)]] if int(polarisation) == -1: self.var[int(route)] = [(f,-1) for f in self.dom[int(dom)]] if int(polarisation) == 1: self.var[int(route)] = [(f,1) for f in self.dom[int(dom)]] if len(line.split())==6 and line.split()[0]=="CI": (CI, route1, route2, vartype, operator, deviation) = line.split()[:6] self.ctr.append((int(route1), int(route2), vartype, operator, int(deviation))) if len(line.split())==14 and line.split()[0]=="CE": (CE, route1, route2, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) = line.split()[:14] self.softeq.append((int(route1), int(route2), [int(s) for s in [s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10]])) self.nbsoft += 1 if len(line.split())==14 and line.split()[0]=="CD": (CD, route1, route2, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) = line.split()[:14] self.softne.append((int(route1), int(route2), [int(s) for s in [s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10]])) self.top = 10*(k+1)*self.nbsoft**2 + 1 if len(sys.argv) < 2: exit('Command line argument is composed of the problem data filename and the relaxation level') k = int(sys.argv[2]) #collect data data = Data(sys.argv[1], k) Problem = pytoulbar2.CFN(data.top) #create a variable for each link for e in list(data.var.keys()): domain = [] for i in data.var[e]: domain.append(str(i)) Problem.AddVariable("X" + str(e), domain) #hard binary constraints for (route1, route2, vartype, operand, deviation) in data.ctr: Constraint = [] for (f1,p1) in data.var[route1]: for (f2,p2) in data.var[route2]: if vartype == 'F': if operand == 'E': if abs(f2 - f1) == deviation: Constraint.append(0) else: Constraint.append(data.top) else: if abs(f2 - f1) != deviation: Constraint.append(0) else: Constraint.append(data.top) else: if operand == 'E': if p2 == p1: Constraint.append(0) else: Constraint.append(data.top) else: if p2 != p1: Constraint.append(0) else: Constraint.append(data.top) Problem.AddFunction(["X" + str(route1), "X" + str(route2)], Constraint) #soft binary constraints for equal polarization for (route1, route2, deviations) in data.softeq: for i in range(11): ListConstraints = [] for (f1,p1) in data.var[route1]: for (f2,p2) in data.var[route2]: if p1!=p2 or abs(f1 - f2) >= deviations[i]: ListConstraints.append(0) elif i >= k: ListConstraints.append(data.top) elif i == k-1: ListConstraints.append(10*data.nbsoft) else: ListConstraints.append(1) Problem.AddFunction(["X" + str(route1), "X" + str(route2)], ListConstraints) #soft binary constraints for not equal polarization for (route1, route2, deviations) in data.softne: for i in range(11): ListConstraints = [] for (f1,p1) in data.var[route1]: for (f2,p2) in data.var[route2]: if p1==p2 or abs(f1 - f2) >= deviations[i]: ListConstraints.append(0) elif i >= k: ListConstraints.append(data.top) elif i == k-1: ListConstraints.append(10*data.nbsoft) else: ListConstraints.append(1) Problem.AddFunction(["X" + str(route1), "X" + str(route2)], ListConstraints) #zero-arity cost function representing a constant cost corresponding to the relaxation at level k Problem.AddFunction([], 10*k*data.nbsoft**2) #Problem.Dump('Fapp.cfn') Problem.CFN.timer(900) Problem.Solve(showSolutions=3)
4,169
30.590909
115
py
toulbar2
toulbar2-master/web/TUTORIALS/magicsquare.py
import sys import pytoulbar2 N = int(sys.argv[1]) magic = N * (N * N + 1) // 2 top = 1 Problem = pytoulbar2.CFN(top) for i in range(N): for j in range(N): #Create a variable for each square Problem.AddVariable('Cell(' + str(i) + ',' + str(j) + ')', range(1,N*N+1)) Problem.AddAllDifferent(['Cell(' + str(i) + ',' + str(j) + ')' for i in range(N) for j in range(N)], encoding = 'salldiffkp') for i in range(N): #Create a sum constraint with variables on the same row Problem.AddLinearConstraint([1 for j in range(N)], ['Cell(' + str(i) + ',' + str(j) + ')' for j in range(N)],'==',magic) #Create a sum constraint with variables on the same column Problem.AddLinearConstraint([1 for j in range(N)], ['Cell(' + str(j) + ',' + str(i) + ')' for j in range(N)],'==',magic) #Create a sum constraint with variables on the same diagonal Problem.AddLinearConstraint([1 for j in range(N)], ['Cell(' + str(i) + ',' + str(i) + ')' for i in range(N)],'==',magic) Problem.AddLinearConstraint([1 for j in range(N)], ['Cell(' + str(N-i-1) + ',' + str(i) + ')' for i in range(N)],'==',magic) #Problem.Dump('MagicSquare.cfn') Problem.CFN.timer(900) res = Problem.Solve(showSolutions = 3) if res and len(res[0]) == N*N: # pretty print solution for i in range(N): print([res[0][i * N + j] for j in range(N)]) # and its magic number print("Magic:", int(magic))
1,373
33.35
125
py
toulbar2
toulbar2-master/web/TUTORIALS/squaresoft.py
import sys from random import randint, seed seed(123456789) import pytoulbar2 try: N = int(sys.argv[1]) S = int(sys.argv[2]) assert N <= S except: print('Two integers need to be given as arguments: N and S') exit() Problem = pytoulbar2.CFN(N**4 + 1) #create a variable for each square for i in range(N): Problem.AddVariable('sq' + str(i+1), ['(' + str(l) + ',' + str(j) + ')' for l in range(S-i) for j in range(S-i)]) #binary soft constraints for overlapping squares for i in range(N): for j in range(i+1,N): ListConstraintsOverlaps = [] for a in [S*k+l for k in range(S-i) for l in range(S-i)]: for b in [S*m+n for m in range(S-j) for n in range(S-j)]: #calculating the coordinates of the squares X_i = a%S X_j = b%S Y_i = a//S Y_j = b//S #calculating if squares are overlapping if X_i >= X_j : if X_i - X_j < j+1: if Y_i >= Y_j: if Y_i - Y_j < j+1: ListConstraintsOverlaps.append(min(j+1-(X_i - X_j),i+1)*min(j+1-(Y_i - Y_j),i+1)) else: ListConstraintsOverlaps.append(0) else: if Y_j - Y_i < i+1: ListConstraintsOverlaps.append(min(j+1-(X_i - X_j),i+1)*min(i+1-(Y_j - Y_i),j+1)) else: ListConstraintsOverlaps.append(0) else: ListConstraintsOverlaps.append(0) else : if X_j - X_i < i+1: if Y_i >= Y_j: if Y_i - Y_j < j+1: ListConstraintsOverlaps.append(min(i+1-(X_j - X_i),j+1)*min(j+1-(Y_i - Y_j),i+1)) else: ListConstraintsOverlaps.append(0) else: if Y_j - Y_i < i+1: ListConstraintsOverlaps.append(min(i+1-(X_j - X_i),j+1)*min(i+1-(Y_j - Y_i),j+1)) else: ListConstraintsOverlaps.append(0) else: ListConstraintsOverlaps.append(0) Problem.AddFunction(['sq' + str(i+1), 'sq' + str(j+1)], ListConstraintsOverlaps) #Problem.Dump('SquareSoft.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions=3) if res: for i in range(S): row = '' for j in range(S): row += ' ' for k in range(N-1, -1, -1): if (res[0][k]%(S-k) <= j and j - res[0][k]%(S-k) <= k) and (res[0][k]//(S-k) <= i and i - res[0][k]//(S-k) <= k): row = row[:-1] + chr(65 + k) print(row) else: print('No solution found!')
2,244
28.155844
117
py
toulbar2
toulbar2-master/web/TUTORIALS/latinsquare.py
import sys from random import seed, randint seed(123456789) import pytoulbar2 N = int(sys.argv[1]) top = N**3 +1 Problem = pytoulbar2.CFN(top) for i in range(N): for j in range(N): #Create a variable for each square Problem.AddVariable('Cell(' + str(i) + ',' + str(j) + ')', range(N)) for i in range(N): #Create a constraint all different with variables on the same row Problem.AddAllDifferent(['Cell(' + str(i) + ',' + str(j) + ')' for j in range(N)], encoding = 'salldiffkp') #Create a constraint all different with variables on the same column Problem.AddAllDifferent(['Cell(' + str(j) + ',' + str(i) + ')'for j in range(N)], encoding = 'salldiffkp') #Random unary costs for i in range(N): for j in range(N): ListConstraintsUnaryC = [] for l in range(N): ListConstraintsUnaryC.append(randint(1,N)) Problem.AddFunction(['Cell(' + str(i) + ',' + str(j) + ')'], ListConstraintsUnaryC) #Problem.Dump('WeightLatinSquare.cfn') Problem.CFN.timer(300) res = Problem.Solve(showSolutions = 3) if res and len(res[0]) == N*N: # pretty print solution for i in range(N): print([res[0][i * N + j] for j in range(N)]) # and its cost print("Cost:", int(res[1]))
1,257
28.952381
111
py
toulbar2
toulbar2-master/web/TUTORIALS/sudoku/MNIST_train.py
from __future__ import print_function import argparse import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pickle import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision import datasets, transforms from torch.optim.lr_scheduler import StepLR class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 32, 3, 1) self.conv2 = nn.Conv2d(32, 64, 3, 1) self.dropout1 = nn.Dropout2d(0.25) self.dropout2 = nn.Dropout2d(0.5) self.fc1 = nn.Linear(9216, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = self.conv1(x) x = F.relu(x) x = self.conv2(x) x = F.max_pool2d(x, 2) x = self.dropout1(x) x = torch.flatten(x, 1) x = self.fc1(x) x = F.relu(x) x = self.dropout2(x) x = self.fc2(x) output = F.log_softmax(x, dim=1) return output def train(args, model, device, train_loader, optimizer, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % args.log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) def test(args, model, device, test_loader): model.eval() test_loss = 0 correct = 0 with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset))) def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--batch-size', type=int, default=64, metavar='N', help='input batch size for training (default: 64)') parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=14, metavar='N', help='number of epochs to train (default: 14)') parser.add_argument('--lr', type=float, default=1.0, metavar='LR', help='learning rate (default: 1.0)') parser.add_argument('--gamma', type=float, default=0.7, metavar='M', help='Learning rate step gamma (default: 0.7)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--save-model', action='store_true', default=False, help='For Saving the current Model') parser.add_argument('--save-test-marginals', action='store_true', default=False, help='For Saving the marginal scores of the Model on the test set') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) device = torch.device("cuda" if use_cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} train_loader = torch.utils.data.DataLoader( datasets.MNIST('./data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_set = datasets.MNIST('./data', train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])) test_loader = torch.utils.data.DataLoader(test_set, batch_size=args.test_batch_size, shuffle=True, **kwargs) model = Net().to(device) optimizer = optim.Adadelta(model.parameters(), lr=args.lr) scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma) for epoch in range(1, args.epochs + 1): train(args, model, device, train_loader, optimizer, epoch) test(args, model, device, test_loader) scheduler.step() if args.save_model: torch.save(model.state_dict(), "mnist_cnn.pt") if args.save_test_marginals: img_logits = [[],[],[],[],[],[],[],[],[],[]] img_indces = [[],[],[],[],[],[],[],[],[],[]] test_loader = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False) for idx, (data, target) in enumerate(test_loader): with torch.no_grad(): label = target.data.numpy()[0] logits = model.forward(data) img_logits[label].append(-logits.data.numpy().squeeze()) img_indces[label].append(idx) with open("MNIST_test_marginal","wb") as f: pickle.dump( img_logits , f) # restore with img_logits = pickle.load(open("MNIST_test_marginal", "rb" )) with open("MNIST_test_indices","wb") as f: pickle.dump( img_indces , f) # restore with img_indces = pickle.load(open("MNIST_test_indices", "rb" )) data = iter(torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False)) images = list(map(lambda x: x[0].reshape(28,28), data)) white = np.zeros((28,28)) mpl.rcParams['toolbar'] = 'None' plt.style.use('dark_background') fig, axs = plt.subplots(9, 9,figsize=(5,5)) for i in range(9): for j in range(9): axs[i][j].set_axis_off() if (i==j): axs[i][j].imshow(white,cmap=plt.get_cmap('Greys')) else: axs[i][j].imshow(images[i*9+j],cmap=plt.get_cmap('Greys')) fig.tight_layout(pad=0.2,h_pad=0.2,w_pad=0.2) plt.show() if __name__ == '__main__': main()
6,939
40.065089
97
py
toulbar2
toulbar2-master/web/TUTORIALS/sudoku/sudoku.py
import pytoulbar2 import numpy as np import itertools import pandas as pd # Adds a clique of differences with violation "cost" on "varList" def addCliqueAllDiff(theCFN, varList, cost): different = (cost*np.identity(size, dtype=np.int64)).flatten() for vp in itertools.combinations(varList,2): theCFN.AddFunction(vp,different) # Sets the value of variable with index "vIdx" to "value" using a unary function def setHint(theCFN,vIdx,value): costs = theCFN.GetUB()*np.ones(size, dtype = np.int64) costs[value-1] = 0 theCFN.AddFunction([vIdx], costs) def printGrid(l): for i,v in enumerate(l): print(v,end=(' ' if (i+1)%size else '\n')) myCFN = pytoulbar2.CFN(1) # Sudoku size parameter (typical 3 gives 3*3 x 3*3 grid) par = 3 size = par * par # Prefilled grids/solutions from the validation set of the RRN paper (0 meaning unknown) valid = pd.read_csv("valid.csv.xz",sep=",", header=None).values hints = valid[:][:,0] sols = valid[:][:,1] grid = [int(h) for h in hints[0]] # list of row, column and cells variable indices rows = [ [] for _ in range(size) ] columns = [ [] for _ in range(size) ] cells = [ [] for _ in range(size) ] # create variables and keep indices in row, columns and cells for i in range(size): for j in range(size): vIdx = myCFN.AddVariable("X"+str(i+1)+"."+str(j+1),range(1,size+1)) columns[j].append(vIdx) rows[i].append(vIdx) cells[(i//par)*par+(j//par)].append(vIdx) # add the clique constraints on rows, columns and cells for scope in rows+columns+cells: addCliqueAllDiff(myCFN,scope, myCFN.GetUB()) # fill-in hints: a string of values, 0 denote empty cells for v,h in enumerate(grid): if h: setHint(myCFN,v,h) sol = myCFN.Solve() printGrid(sol[0])
1,783
28.733333
88
py
toulbar2
toulbar2-master/web/TUTORIALS/sudoku/MNIST_sudoku.py
import pytoulbar2 import math, numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pickle import torch from torchvision import datasets, transforms import itertools import pandas as pd import hashlib ########################################################################## # Image output routines ########################################################################## def fillImage(fig,axsf,g,ph,cs): for v,h in enumerate(g): axsf[v].set_axis_off() if h: if (ph[v]): if h != int(cs[v]): h = int(cs[v]) mycmap = plt.get_cmap('Purples_r') elif ph[v] != int(cs[v]): mycmap = plt.get_cmap('Greens_r') else: mycmap = plt.get_cmap('Greys_r') else: if h != int(cs[v]): mycmap = plt.get_cmap('Purples') else: mycmap = plt.get_cmap('Greys') axsf[v].imshow(MNIST_image(cs,v,h),cmap=mycmap) else: axsf[v].imshow(np.zeros((28,28))) fig.tight_layout(pad=0.2,h_pad=0.2,w_pad=0.2) # Prepare figure with flat axis for easier access mpl.rcParams['toolbar'] = 'None' plt.style.use('dark_background') figs, axss = plt.subplots(9, 9,figsize=(5,5)) axssf = axss.flatten() ########################################################################## # Loads MNIST images and outputs on every test set image ########################################################################## test_set = datasets.MNIST('./data', download = True, train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])) data = iter(torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False)) images = list(map(lambda x: x[0].reshape(28,28), data)) # Load MNIST outputs and image indices for every MNIST test digit logits = pickle.load(open("MNIST_test_marginal", "rb")) logits_len = list(map(lambda x: len(x), logits)) img_indces = pickle.load(open("MNIST_test_indices", "rb")) def myhash(str): return int(hashlib.sha512(str.encode('utf-8')).hexdigest(), 16) def MNIST_output(cg,p,val): h = myhash(cg+str(p)) return logits[val][h % logits_len[val]] def MNIST_image(cg,p,val): h = myhash(cg+str(p)) return images[img_indces[val][h % logits_len[val]]] ########################################################################## # Sudoku grids loading ########################################################################## # Load grid/solution pairs from the validation set of the RRN paper valid = pd.read_csv("valid.csv.xz",sep=",", header=None).values hints = valid[:][:,0] sols = valid[:][:,1] size = math.isqrt(len(sols[0])) par = math.isqrt(size) def MNIST_fails(lg): lf = [] for i,cg in enumerate(lg): mygrid = [int(h) for h in cg] ok = True for v,h in enumerate(mygrid): if h and (np.argmin(MNIST_output(cg,v,h)) != h): ok = False if (not ok): lf.append(i) return lf ########################################################################## # Auxiliary CFN functions for Sudoku ########################################################################## # Adds a clique of differences with violation "cost" on "varList" def addCliqueAllDiff(theCFN, varList, cost): different = (cost*np.identity(size, dtype=np.int64)).flatten() for vp in itertools.combinations(varList,2): theCFN.AddFunction(vp,different) # Sets the value of variable with index "vIdx" to "value" using a unary function def setHint(theCFN,vIdx,value): costs = theCFN.GetUB()*np.ones(size, dtype = np.int64) costs[value-1] = 0 theCFN.AddFunction([vIdx], costs) # Add a MNIST minus log prob cost on vIdx. Uncalibrated yet decent def setProbHint(theCFN,vIdx,mlp): theCFN.AddFunction([vIdx], mlp[1:]) ########################################################################## # Auxiliary CFN functions for Sudoku : 6 13 ########################################################################## CP_mode = False grid_number = 13 cgrid = hints[grid_number] csol = sols[grid_number] grid = [int(h) for h in cgrid] # list of row, column and cells variable indices rows = [ [] for _ in range(size) ] columns = [ [] for _ in range(size) ] cells = [ [] for _ in range(size) ] myCFN = pytoulbar2.CFN(1) if CP_mode else pytoulbar2.CFN(1000000,6) # create variables and keep indices in row, columns and cells for i in range(size): for j in range(size): vIdx = myCFN.AddVariable("X"+str(i+1)+"."+str(j+1),range(1,size+1)) columns[j].append(vIdx) rows[i].append(vIdx) cells[(i//par)*par+(j//par)].append(vIdx) # add the clique constraints on rows, columns and cells for scope in rows+columns+cells: addCliqueAllDiff(myCFN,scope, myCFN.GetUB()) # assign/bias variables pgrid = [] for v,h in enumerate(grid): if h: prediction = np.argmin(MNIST_output(csol,v,h)) pgrid.append(prediction) if (prediction != h): row = v//size col = v % size print("Erreur MNIST on cell",row+1,col+1,", a", h,"has been predicted as", prediction) if (CP_mode): setHint(myCFN,v,prediction) else: setProbHint(myCFN,v,MNIST_output(csol,v,h)) else: pgrid.append(0) sol = myCFN.Solve() if (sol): fillImage(figs,axssf,sol[0],pgrid,csol) else: fillImage(figs,axssf,pgrid,pgrid,csol) print("No solution found") plt.show()
5,697
33.325301
98
py
toulbar2
toulbar2-master/docker/toulbar2/using/problem.py
from pytoulbar2 import CFN import numpy myCFN = CFN(1) myCFN.Solve() print("problem end OK")
98
8
26
py
toulbar2
toulbar2-master/docker/pytoulbar2/using/problem.py
from pytoulbar2 import CFN import numpy myCFN = CFN(1) myCFN.Solve() print("problem end OK")
98
8
26
py
toulbar2
toulbar2-master/docs/source/conf.py
# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # # This file does only contain a selection of the most common options. For a # full list see the documentation: # http://www.sphinx-doc.org/en/master/config # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. import os import sys # sys.path.insert(0, os.path.abspath('.')) docs_path = os.path.normpath(os.path.abspath('.')) # python source code path pytoulbar2_code_path = os.path.normpath(os.path.join(docs_path, "..", "..", "pytoulbar2")) sys.path.insert(0, pytoulbar2_code_path) # -- Project information ----------------------------------------------------- project = 'toulbar2' copyright = '2022, INRAE' author = 'INRAE' # The short X.Y version version = '1.0' # The full version, including alpha/beta/rc tags release = '1.0.0' # -- General configuration --------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx_rtd_theme', 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode', #'sphinx.ext.imgmath', #'sphinx.ext.mathjax', 'sphinx.ext.githubpages', # => .nojekyll file 'sphinx.ext.graphviz', 'breathe', 'myst_parser', 'sphinx.ext.autosectionlabel', ] # Breathe breathe_default_project = "toulbar2cpp" breathe_projects = { "toulbar2cpp" : os.path.normpath(os.path.join(docs_path, "..", "..", "build", "xml")), } #breathe_implementation_filename_extensions = ['.c', '.cc', '.cpp'] # Prefix document path to section labels, to use: # `path/to/file:heading` instead of just `heading` autosectionlabel_prefix_document = True # Add any paths that contain templates here, relative to this directory. templates_path = [os.path.join(docs_path,'_templates')] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: source_suffix = ['.rst', '.md'] # The master toctree document. master_doc = 'index' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = 'en' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = ['Thumbs.db', '.DS_Store', #'_build', _pyvenv', 'README.md', ] # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # # html_theme_options = {} html_theme_options = { #'logo_only': False, #'display_version': True, #'prev_next_buttons_location': 'bottom', #'style_external_links': False, #'vcs_pageview_mode': '', #'style_nav_header_background': 'FireBrick', ## Toc options #'collapse_navigation': True, #'sticky_navigation': True, 'navigation_depth': 4, #'includehidden': True, # False, #'titles_only': False } # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = [os.path.join(docs_path,'_static')] html_style = 'css/toulbar2.css' # Custom sidebar templates, must be a dictionary that maps document names # to template names. # # The default sidebars (for documents that don't match any pattern) are # defined by theme itself. Builtin themes are using these templates by # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', # 'searchbox.html']``. # # html_sidebars = {} #html_logo = # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'toulbar2doc' # -- Options for LaTeX output ------------------------------------------------ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', # remove blank pages (between the title page and the TOC, etc.) 'classoptions': ',openany,oneside', 'babel' : '\\usepackage[english]{babel}', # Additional stuff for the LaTeX preamble. # # 'preamble': '', # Latex figure (float) alignment # # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ # extracts ('examples/tutorials_pdf', 'tutorials.tex', '', # to keep .rst title 'INRAE', 'manual'), ('examples/usecases_pdf', 'usecases.tex', '', # to keep .rst title 'INRAE', 'manual'), ('refman', 'refman.tex', 'toulbar2 Reference Manual', 'INRAE', 'manual'), ('userdoc', 'userdoc.tex', 'toulbar2 User Guide', 'INRAE', 'manual'), ('formats/wcspformat', 'WCSP_format.tex', '', # to keep .rst title 'INRAE', 'manual'), ('formats/cfnformat', 'CFN_format.tex', '', # to keep .rst title 'INRAE', 'manual'), # api ref ('ref/ref_cpp', 'cpp_library.tex', 'C++ Library of toulbar2', 'INRAE', 'manual'), ('ref/ref_python', 'python_library.tex', 'Python Library of toulbar2', 'INRAE', 'manual'), # main ('index_pdf', 'toulbar2.tex', 'toulbar2 Documentation', 'INRAE', 'manual'), ] # -- Options for manual page output ------------------------------------------ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'toulbar2', 'toulbar2 Documentation', [author], 1) ] # -- Options for Texinfo output ---------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'toulbar2', 'toulbar2 Documentation', author, 'toulbar2', 'One line description of project.', 'Miscellaneous'), ] # -- Options for Epub output ------------------------------------------------- # Bibliographic Dublin Core info. epub_title = project # The unique identifier of the text. This can be a ISBN number # or the project homepage. # # epub_identifier = '' # A unique identification for the text. # # epub_uid = '' # A list of files that should not be packed into the epub file. epub_exclude_files = ['search.html'] # -- Extension configuration -------------------------------------------------
7,750
28.471483
79
py