def load_stn(file_path, stp_solver): with open(file_path) as json_file: stn_dict = json.load(json_file) stn_json = json.dumps(stn_dict) stp = STP(stp_solver) stn = stp.get_stn(stn_json=stn_json) return stp, stn
def setUp(self): # Load the stn as a dictionary with open(STN) as json_file: stn_dict = json.load(json_file) # Convert the dict to a json string stn_json = json.dumps(stn_dict) self.stp = STP('fpc') self.stn = self.stp.get_stn(stn_json=stn_json)
def get_stp_solver(self): allocation_method = self._components.get('allocation_method') solver_name = self._allocation_methods.get(allocation_method) if not solver_name: self.logger.error("The given allocation method is not available") raise ValueError(allocation_method) return STP(solver_name)
def __init__(self, ccu_store, api, stp_solver, allocation_method, round_time=5, **kwargs): self.logger = logging.getLogger("mrs.auctioneer") self.robot_ids = list() self.timetables = dict() self.api = api self.stp = STP(stp_solver) self.allocation_method = allocation_method self.round_time = timedelta(seconds=round_time) self.alternative_timeslots = kwargs.get('alternative_timeslots', False) self.logger.debug("Auctioneer started") self.tasks_to_allocate = dict() self.allocations = list() self.waiting_for_user_confirmation = list() self.round = Round() # TODO: Update zero_timepoint today_midnight = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0) self.zero_timepoint = TimeStamp() self.zero_timepoint.timestamp = today_midnight
def __init__(self, robot_id, api, robot_store, stp_solver, task_type): self.id = robot_id self.api = api self.stp = STP(stp_solver) self.timetable = Timetable(robot_id, self.stp) today_midnight = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0) self.timetable.zero_timepoint = TimeStamp() self.timetable.zero_timepoint.timestamp = today_midnight
def __init__(self, ccu_store, api, stp_solver, freeze_window, **kwargs): self.logger = logging.getLogger('mrs.dispatcher') self.api = api self.stp = STP(stp_solver) self.freeze_window = timedelta(minutes=freeze_window) self.re_allocate = kwargs.get('re_allocate', False) self.robot_ids = list() self.scheduler = Scheduler(self.stp) self.logger.debug("Dispatcher started")
def from_dict(timetable_dict): robot_id = timetable_dict['robot_id'] stp_solver = STP(timetable_dict['solver_name']) timetable = Timetable(robot_id, stp_solver) stn_cls = timetable.stp_solver.get_stn() ztp = timetable_dict.get('ztp') timetable.ztp = TimeStamp.from_str(ztp) timetable.stn = stn_cls.from_dict(timetable_dict['stn']) timetable.dispatchable_graph = stn_cls.from_dict(timetable_dict['dispatchable_graph']) timetable.stn_tasks = timetable_dict['stn_tasks'] return timetable
class TestFPC(unittest.TestCase): """ Tests the solver FullPathConsistency """ logger = logging.getLogger('stn.test') def setUp(self): # Load the stn as a dictionary with open(STN) as json_file: stn_dict = json.load(json_file) # Convert the dict to a json string stn_json = json.dumps(stn_dict) self.stp = STP('fpc') self.stn = self.stp.get_stn(stn_json=stn_json) def test_build_stn(self): self.logger.info("STN: \n %s", self.stn) metric, minimal_network = self.stp.solve(self.stn) self.logger.info("Minimal STN: \n %s", minimal_network) completion_time = minimal_network.get_completion_time() makespan = minimal_network.get_makespan() self.logger.info("Completion time: %s ", completion_time) self.logger.info("Makespan: %s ", makespan) self.assertEqual(completion_time, 65) self.assertEqual(makespan, 100) constraints = minimal_network.get_constraints() for (i, j) in constraints: if i == 0 and j == 1: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 35) self.assertEqual(upper_bound, 41) if i == 0 and j == 2: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 41) self.assertEqual(upper_bound, 47) if i == 0 and j == 3: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 45) self.assertEqual(upper_bound, 51) if i == 0 and j == 4: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 90) self.assertEqual(upper_bound, 96) if i == 0 and j == 5: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 96) self.assertEqual(upper_bound, 102) if i == 0 and j == 6: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 100) self.assertEqual(upper_bound, 106) if i == 1 and j == 2: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 6) self.assertEqual(upper_bound, 12) if i == 2 and j == 3: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 4) self.assertEqual(upper_bound, 10) if i == 3 and j == 4: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 39) self.assertEqual(upper_bound, 51) if i == 4 and j == 5: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 6) self.assertEqual(upper_bound, 12) if i == 5 and j == 6: lower_bound = -minimal_network[j][i]['weight'] upper_bound = minimal_network[i][j]['weight'] self.assertEqual(lower_bound, 4) self.assertEqual(upper_bound, 10)
class TestDSC(unittest.TestCase): """ Tests the solver Degree of Strong Controllability """ logger = logging.getLogger('stn.test') def setUp(self): # Load the stn as a dictionary with open(STNU) as json_file: stnu_dict = json.load(json_file) # Convert the dict to a json string stnu_json = json.dumps(stnu_dict) self.stp = STP('dsc') self.stn = self.stp.get_stn(stn_json=stnu_json) def test_build_stn(self): self.logger.info("STNU: \n %s", self.stn) self.logger.info("Getting Schedule...") schedule = self.stp.solve(self.stn) self.logger.info("DSC: %s ", schedule.risk_metric) self.logger.info("schedule: %s ", schedule) completion_time = schedule.get_completion_time() makespan = schedule.get_makespan() self.logger.info("Completion time: %s ", completion_time) self.logger.info("Makespan: %s ", makespan) self.assertEqual(completion_time, 157) self.assertEqual(makespan, 98) expected_risk_metric = 0.0 self.assertEqual(schedule.risk_metric, expected_risk_metric) constraints = schedule.get_constraints() for (i, j) in constraints: if i == 0 and j == 1: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 37) self.assertEqual(upper_bound, 37) if i == 0 and j == 2: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 41) self.assertEqual(upper_bound, 45) if i == 0 and j == 3: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 43) self.assertEqual(upper_bound, 51) if i == 0 and j == 4: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 92) self.assertEqual(upper_bound, 92) if i == 0 and j == 5: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 96) self.assertEqual(upper_bound, 100) if i == 0 and j == 6: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 98) self.assertEqual(upper_bound, 106) if i == 1 and j == 2: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 4) self.assertEqual(upper_bound, 8) if i == 2 and j == 3: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 2) self.assertEqual(upper_bound, 6) if i == 3 and j == 4: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, MAX_FLOAT) if i == 4 and j == 5: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 4) self.assertEqual(upper_bound, 8) if i == 5 and j == 6: lower_bound = -schedule[j][i]['weight'] upper_bound = schedule[i][j]['weight'] self.assertEqual(lower_bound, 2) self.assertEqual(upper_bound, 6)
class TestSREA(unittest.TestCase): """ Tests the solver Static Robust Execution """ logger = logging.getLogger('stn.test') def setUp(self): # Load the stn as a dictionary with open(STN) as json_file: pstn_dict = json.load(json_file) # Convert the dict to a json string pstn_json = json.dumps(pstn_dict) self.stp = STP('srea') self.stn = self.stp.get_stn(stn_json=pstn_json) def test_build_stn(self): self.logger.info("PSTN: \n %s", self.stn) self.logger.info("Getting GUIDE...") alpha, guide_stn = self.stp.solve(self.stn) self.logger.info("GUIDE") self.logger.info(guide_stn) self.logger.info("Alpha: %s ", alpha) completion_time = guide_stn.get_completion_time() makespan = guide_stn.get_makespan() self.logger.info("Completion time: %s ", completion_time) self.logger.info("Makespan: %s ", makespan) self.assertEqual(completion_time, 60) self.assertEqual(makespan, 97) expected_alpha = 0.0 self.assertEqual(alpha, expected_alpha) constraints = guide_stn.get_constraints() for (i, j) in constraints: if i == 0 and j == 1: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 37) self.assertEqual(upper_bound, 38) if i == 0 and j == 2: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 41) self.assertEqual(upper_bound, 47) if i == 0 and j == 3: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 42) self.assertEqual(upper_bound, 54) if i == 0 and j == 4: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 92) self.assertEqual(upper_bound, 94) if i == 0 and j == 5: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 96) self.assertEqual(upper_bound, 102) if i == 0 and j == 6: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 97) self.assertEqual(upper_bound, 109) if i == 1 and j == 2: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, 47) if i == 2 and j == 3: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, 61) if i == 3 and j == 4: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, 61) if i == 4 and j == 5: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, 61) if i == 5 and j == 6: lower_bound = -guide_stn[j][i]['weight'] upper_bound = guide_stn[i][j]['weight'] self.assertEqual(lower_bound, 0) self.assertEqual(upper_bound, MAX_FLOAT)
import json from stn.stp import STP STNU = "data/stnu_two_tasks.json" if __name__ == '__main__': with open(STNU) as json_file: stnu_dict = json.load(json_file) # Convert the dict to a json string stnu_json = json.dumps(stnu_dict) stp = STP('dsc_lp') stn = stp.get_stn(stn_json=stnu_json) print(stn) stn_dict = stn.to_dict() print(stn_dict) print(type(stn_dict['nodes'][0]['data']))