Skip to content

Commit

Permalink
Fixes to make simulator runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
yura-hb committed Feb 8, 2024
1 parent aec4f14 commit 920ed95
Show file tree
Hide file tree
Showing 54 changed files with 551 additions and 339 deletions.
4 changes: 2 additions & 2 deletions diploma_thesis/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

from .machine import *
from .workcenter import *
from .utils.phase import *
from .workcenter import WorkCenter, WorkCenterInput, from_cli as work_center_from_cli
from .machine import Machine, MachineInput, from_cli as machine_from_cli
40 changes: 14 additions & 26 deletions diploma_thesis/agents/base/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,26 @@
import torch

from agents.utils import Phase, EvaluationPhase
from .encoder import Encoder as STEncoder
from .model import Model as Brain
from .encoder import Encoder as StateEncoder, Input, State
from .model import Model, Action, Result

StateEncoder = TypeVar('StateEncoder', bound=STEncoder)
Model = TypeVar('Model', bound=Brain)


class Agent(Generic[StateEncoder, Model], metaclass=ABCMeta):
class Agent(metaclass=ABCMeta):

def __init__(self,
state_encoder: StateEncoder.State,
model: Model,
model: Model[Input, State, Action, Result],
state_encoder: StateEncoder[Input, State],
memory):
self.state_encoder = state_encoder
self.model = model
self.memory = memory
self.phase = EvaluationPhase()

# TODO: Check if it works!!!

assert isinstance(self.state_encoder.Input, self.model.Input), \
f"State encoder input type {self.state_encoder.Input} does not match model input type {self.model.Input}"
assert isinstance(self.state_encoder.State, self.model.State), \
f"State encoder state type {self.state_encoder.State} does not match model state type {self.model.State}"

def update(self, phase: Phase):
self.phase = phase

@abstractmethod
@property
@abstractmethod
def is_trainable(self):
pass

Expand All @@ -42,18 +32,16 @@ def train_step(self):
pass

def store(self,
state: StateEncoder.State,
action: Model.Action,
next_state: StateEncoder.State,
state: State,
action: Action,
next_state: State,
reward: torch.FloatTensor):
pass

def schedule(self, state: StateEncoder.Input) -> Model.Record:
state = self.encode_state(state)

return self.model(state, state)

def encode_state(self, state: StateEncoder.Input) -> StateEncoder.State:
return self.state_encoder.encode(state)
def schedule(self, parameters: Input) -> Model.Record:
state = self.encode_state(parameters)

return self.model(state, parameters)

def encode_state(self, parameters: Input) -> State:
return self.state_encoder.encode(parameters)
Empty file removed diploma_thesis/agents/cli.py
Empty file.
14 changes: 13 additions & 1 deletion diploma_thesis/agents/machine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@

from .utils import Input as MachineInput
from .machine import Machine
from .machine import Machine
from .static import StaticMachine


key_to_class = {
"static": StaticMachine
}


def from_cli(parameters) -> Machine:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters['parameters'])
8 changes: 1 addition & 7 deletions diploma_thesis/agents/machine/machine.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@

from abc import ABCMeta
from typing import TypeVar

from agents.base.agent import Agent
from .model import MachineModel
from .state import StateEncoder

MachineStateEncoder = TypeVar('MachineStateEncoder', bound=StateEncoder)
Model = TypeVar('Model', bound=MachineModel)


class Machine(Agent[MachineStateEncoder, Model], metaclass=ABCMeta):
class Machine(Agent, metaclass=ABCMeta):

pass
10 changes: 10 additions & 0 deletions diploma_thesis/agents/machine/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@
from .model import MachineModel
from .static import StaticModel as StaticMachineModel
from .rule import SchedulingRule

key_to_class = {
"static": StaticMachineModel
}


def from_cli(parameters) -> MachineModel:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters['parameters'])
9 changes: 8 additions & 1 deletion diploma_thesis/agents/machine/model/static.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from .model import MachineModel
from agents.machine.state import PlainEncoder
from .rule import SchedulingRule
from .rule import SchedulingRule, ALL_SCHEDULING_RULES
from typing import Dict


class StaticModel(MachineModel[PlainEncoder.State, None]):
Expand All @@ -18,3 +19,9 @@ def __call__(self, state: State, parameters: MachineModel.Input) -> MachineModel
action=None
)

@staticmethod
def from_cli(parameters: Dict):
rule = parameters['rule']
rule = ALL_SCHEDULING_RULES[rule]

return StaticModel(rule())
11 changes: 11 additions & 0 deletions diploma_thesis/agents/machine/state/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@

from .encoder import StateEncoder
from .plain import PlainEncoder


key_to_class = {
"plain": PlainEncoder
}


def from_cli(parameters) -> StateEncoder:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters.get('parameters', {}))
5 changes: 5 additions & 0 deletions diploma_thesis/agents/machine/state/plain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from .encoder import StateEncoder
from dataclasses import dataclass
from typing import Dict


class PlainEncoder(StateEncoder):
Expand All @@ -11,3 +12,7 @@ class State:

def encode(self, parameters: StateEncoder.Input) -> State:
return self.State()

@staticmethod
def from_cli(parameters: Dict):
return PlainEncoder()
18 changes: 13 additions & 5 deletions diploma_thesis/agents/machine/static.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@


from .machine import Machine
from .model import StaticMachineModel, SchedulingRule
from .state import PlainEncoder
from .model import MachineModel, from_cli as model_from_cli
from .state import StateEncoder, from_cli as state_encoder_from_cli
from typing import Dict


class StaticMachine(Machine[StaticMachineModel, PlainEncoder]):
class StaticMachine(Machine):

def __init__(self, rule: SchedulingRule):
super().__init__(model=StaticMachineModel(rule), state_encoder=PlainEncoder(), memory=None)
def __init__(self, model: MachineModel, state_encoder: StateEncoder):
super().__init__(model=model, state_encoder=state_encoder, memory=None)

@property
def is_trainable(self):
return False

def train_step(self):
pass

@staticmethod
def from_cli(parameters: Dict):
model = model_from_cli(parameters['model'])
encoder = state_encoder_from_cli(parameters['encoder'])

return StaticMachine(model=model, state_encoder=encoder)
14 changes: 13 additions & 1 deletion diploma_thesis/agents/workcenter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@

from .utils import Input as WorkCenterInput
from .work_center import WorkCenter
from .work_center import WorkCenter
from .static import StaticWorkCenter


key_to_class = {
"static": StaticWorkCenter
}


def from_cli(parameters) -> WorkCenter:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters['parameters'])
10 changes: 10 additions & 0 deletions diploma_thesis/agents/workcenter/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
from .model import WorkCenterModel
from .static import StaticModel as StaticWorkCenterModel
from .rule import RoutingRule

key_to_class = {
"static": StaticWorkCenterModel
}


def from_cli(parameters) -> WorkCenterModel:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters['parameters'])
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/ct.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from routing_rule import *
from .routing_rule import *


class CTRoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/ea.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class EARoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/et.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class ETRoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/random.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class RandomRoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/sq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class SQRoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/tt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class TTRoutingRule(RoutingRule):
Expand Down
2 changes: 1 addition & 1 deletion diploma_thesis/agents/workcenter/model/rule/ut.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from routing_rule import *
from .routing_rule import *


class UTRoutingRule(RoutingRule):
Expand Down
9 changes: 8 additions & 1 deletion diploma_thesis/agents/workcenter/model/static.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from .model import WorkCenterModel
from agents.machine.state import PlainEncoder
from .rule import RoutingRule
from .rule import RoutingRule, ALL_ROUTING_RULES
from typing import Dict


class StaticModel(WorkCenterModel[PlainEncoder.State, None]):
Expand All @@ -18,3 +19,9 @@ def __call__(self, state: State, parameters: WorkCenterModel.Input) -> WorkCente
action=None
)

@staticmethod
def from_cli(parameters: Dict):
rule = parameters['rule']
rule = ALL_ROUTING_RULES[rule]

return StaticModel(rule())
13 changes: 12 additions & 1 deletion diploma_thesis/agents/workcenter/state/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
from .plain import PlainEncoder
from .encoder import StateEncoder
from .plain import PlainEncoder


key_to_class = {
"plain": PlainEncoder
}


def from_cli(parameters) -> StateEncoder:
cls = key_to_class[parameters['kind']]

return cls.from_cli(parameters.get('parameters', {}))
5 changes: 5 additions & 0 deletions diploma_thesis/agents/workcenter/state/plain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from .encoder import StateEncoder
from dataclasses import dataclass
from typing import Dict


class PlainEncoder(StateEncoder):
Expand All @@ -11,3 +12,7 @@ class State:

def encode(self, parameters: StateEncoder.Input) -> State:
return self.State()

@staticmethod
def from_cli(parameters: Dict):
return PlainEncoder()
19 changes: 14 additions & 5 deletions diploma_thesis/agents/workcenter/static.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@


from .work_center import WorkCenter
from .model import StaticWorkCenterModel, RoutingRule
from .state import PlainEncoder
from .model import StaticWorkCenterModel, from_cli as model_from_cli
from .state import StateEncoder, from_cli as state_encoder_from_cli
from typing import Dict


class StaticMachine(WorkCenter[StaticWorkCenterModel, PlainEncoder]):
class StaticWorkCenter(WorkCenter):

def __init__(self, rule: RoutingRule):
super().__init__(model=StaticWorkCenterModel(rule), state_encoder=PlainEncoder(), memory=None)
def __init__(self, model: StaticWorkCenterModel, state_encoder: StateEncoder):
super().__init__(model=model, state_encoder=state_encoder, memory=None)

@property
def is_trainable(self):
return False

def train_step(self):
pass

@staticmethod
def from_cli(parameters: Dict):
model = model_from_cli(parameters['model'])
encoder = state_encoder_from_cli(parameters['encoder'])

return StaticWorkCenter(model=model, state_encoder=encoder)

9 changes: 1 addition & 8 deletions diploma_thesis/agents/workcenter/work_center.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@

from abc import ABCMeta
from typing import TypeVar

from agents.base.agent import Agent
from .model import WorkCenterModel
from .state import StateEncoder

WorkCenterStateEncoder = TypeVar('WorkCenterStateEncoder', bound=StateEncoder)
Model = TypeVar('Model', bound=WorkCenterModel)


class WorkCenter(Agent[WorkCenterStateEncoder, Model], metaclass=ABCMeta):

class WorkCenter(Agent, metaclass=ABCMeta):
pass
Loading

0 comments on commit 920ed95

Please sign in to comment.