def test_updateState_Not_First_Shift(self): param = Parameters() param.num_processors = 2 self.coreObj = Core(param) for mock_processor in self.coreObj.processors: mock_processor.isIdle = MagicMock(return_value=False) mock_event = Event() mock_event.executeEvent = MagicMock() mock_event.eventTime = Constants.SIMULATION_INITIAL_TIME + 1 * 3600 + 1 self.coreObj.updateState(mock_event) mock_event = Event() mock_event.executeEvent = MagicMock() mock_event.eventTime = Constants.SIMULATION_INITIAL_TIME + 5 * 3600 + 1 self.coreObj.updateState(mock_event) self.assertEqual( self.coreObj.currentTime, Constants.SIMULATION_INITIAL_TIME + 5 * 3600 + 1, "The current time should be updated to SIMULATION_INITIAL_TIME + 5*3600 + 1" ) self.assertEqual( self.coreObj.serviceProcessors, (5 * 3600 + 1) * 2, "The service processors time should be (5*3600 + 1) * 2 (2 service processors)" ) self.assertEqual(self.coreObj.idleProcessors, 0, "The idle time should be 0")
def setUpClass(cls): cls.setWithShifts(True) param = Parameters() param.num_processors = Constants.DEFAULT_PROCESSORS shift_duration = [5, 5, 2, 2] shift_type = [ Constants.ENTREGA, Constants.ENTREGA, Constants.RECOGIDA, Constants.DUAL ] shift_factor = 3600 # hours param.setParameters(shift_duration, shift_type, shift_factor) cls.coreObj = Core(param) cls.coreObj.output_file = open('./TEST_System.txt', "w+") cls.coreObj.parameters.output_file = Constants.OUTPUT_PATH + "TEST_System" cls.coreObj.run() cls.coreObj.output_file.close() with open(Constants.OUTPUT_PATH + 'TEST_System.csv', "r") as read: header = read.readline().split(',') header = [tmp.split('\n')[0] for tmp in header] for i in range(len(header)): cls.cols[header[i]] = i line = read.readline() # [current_time, event_name, event_scheduled, event_time, idle_proc, service_proc, num_idle_proc, buff_len, queue_len, entities_system, shift] # Current_Time,Event_Name,Event_Scheduled,Event-Time,Idle_Processors,Service_Processors,Number_Idle_Processors,Buffer_Length,Queue_Length,Entities_System,Shift while line: row = TestSystemWithShifts.process_line(line) cls.static_big_matrix.append(row) line = read.readline()
def test_run(self): param = Parameters() param.num_processors = 2 self.coreObj = Core(param) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.parameters.output_file = "TEST" self.coreObj.run() self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 0, "The length should be 0 because the simulation ended") obj = self.coreObj.eventsList self.assertIsNotNone("The object is not none", obj) with open('./TEST.stats.csv', "r") as read: self.assertNotEquals(len(read.read()), 0, "The file should not be empty")
def sourceIncrement(self, operationType): param = Parameters() if param.WITH_SHIFTS: if operationType == Constants.ENTREGA: return ceil(random.exponential(1 / self.LAMBDA_Entrega)) elif operationType == Constants.RECOGIDA: return ceil(random.exponential(1 / self.LAMBDA_Recogida)) else: # DUAL return ceil(random.exponential(1 / self.LAMBDA_Dual)) else: return ceil(random.exponential(1 / self.LAMBDA))
def __init__(self, parameters=None): if parameters is None: self.parameters = Parameters() else: self.parameters = parameters num_sources = Constants.DEFAULT_SOURCES num_processors = self.parameters.num_processors # Attributes initialization self.processors = [] self.sources = [] self.eventsList = PriorityQueue(0) # maxsize = 0 (infinite) self.previousTime = Constants.SIMULATION_INITIAL_TIME self.currentTime = Constants.SIMULATION_INITIAL_TIME self.idleProcessors = 0 self.serviceProcessors = 0 self.entitiesSystem = 0 self.service_per_shift = [] self.service_per_total = [] self.shift_durations = self.parameters.getParameters()[1] self.shift_next_time = self.shift_durations[0] self.shift_next_index = 1 # Instance creation self.queue = Queue(Constants.SLOTS_BUFFER) self.parking = Queue(Constants.SLOTS_QUEUE) self.random = Random() for _ in range(0, num_processors): self.processors.append(Processor(self)) for _ in range(0, num_sources): self.sources.append(Source(self)) # Dependency injection for source in self.sources: source.addOutput(self.queue) # source -> queue self.queue.addOutput(self.parking) # queue -> parking self.parking.addInput(self.queue) # parking <- queue for processor in self.processors: self.parking.addOutput(processor) # parking -> processor processor.addInput(self.parking) # processor <- parking self.output_file = None self.numberOfIdleProcessors = num_processors
def test_updateState_With_2Idle_Processors(self): param = Parameters() param.num_processors = 2 self.coreObj = Core(param) for mock_processor in self.coreObj.processors: mock_processor.isIdle = MagicMock(return_value=True) mock_event = Event() mock_event.executeEvent = MagicMock() mock_event.eventTime = Constants.SIMULATION_INITIAL_TIME + 123 self.coreObj.updateState(mock_event) self.assertEqual( self.coreObj.currentTime, Constants.SIMULATION_INITIAL_TIME + 123, "The current time should be updated to SIMULATION_INITIAL_TIME + 123" ) self.assertEqual( self.coreObj.idleProcessors, 123 * 2, "The idle time should be 123 * 2 (2 idle processors)") self.assertEqual(self.coreObj.serviceProcessors, 0, "The service processors time should be 0")
def processorIncrement(self, shift): param = Parameters() if param.WITH_SHIFTS: if shift == Constants.ENTREGA: return ceil( random.uniform(Constants.MINIMUM_TIME_ENTREGA, Constants.MAXIMUM_TIME_ENTREGA)) elif shift == Constants.RECOGIDA: return ceil( random.uniform(Constants.MINIMUM_TIME_RECOGIDA, Constants.MAXIMUM_TIME_RECOGIDA)) else: # DUAL return ceil( random.uniform(Constants.MINIMUM_TIME_DUAL, Constants.MAXIMUM_TIME_DUAL)) else: return ceil( random.uniform(Constants.MINIMUM_TIME, Constants.MAXIMUM_TIME))
def initialization(self): random.seed(0) # se hace getNumTrucks para las tres self.numTrucksEntrega = self.getNumTrucks(Constants.ENTREGA) self.numTrucksRecogida = self.getNumTrucks(Constants.RECOGIDA) self.numTrucksDual = self.getNumTrucks(Constants.DUAL) self.numTrucks = self.getNumTrucks(None) p = Parameters() self.LAMBDA_Entrega = p.getTotalTime( Constants.ENTREGA) / self.numTrucksEntrega self.LAMBDA_Recogida = p.getTotalTime( Constants.RECOGIDA) / self.numTrucksRecogida self.LAMBDA_Dual = p.getTotalTime(Constants.DUAL) / self.numTrucksDual self.LAMBDA = p.getTotalTime(None) / self.numTrucks
def getNumTrucks(self, operationType): param = Parameters() if param.WITH_SHIFTS: if operationType == Constants.ENTREGA: return ceil( random.triangular(Constants.MINIMUM_TRUCKS_ENTREGA, Constants.MEDIAN_TRUCKS_ENTREGA, Constants.MAXIMUM_TRUCKS_ENTREGA)) elif operationType == Constants.RECOGIDA: return ceil( random.triangular(Constants.MINIMUM_TRUCKS_RECOGIDA, Constants.MEDIAN_TRUCKS_RECOGIDA, Constants.MAXIMUM_TRUCKS_RECOGIDA)) else: # DUAL return ceil( random.triangular(Constants.MINIMUM_TRUCKS_DUAL, Constants.MEDIAN_TRUCKS_DUAL, Constants.MAXIMUM_TRUCKS_DUAL)) else: return ceil( random.triangular(Constants.MINIMUM_TRUCKS, Constants.MEDIAN_TRUCKS, Constants.MAXIMUM_TRUCKS))
def __init__(self, list_chrom, p_mut): Parameters.__init__(self) self.list_chrom = list_chrom self.p_mut = p_mut
def setWithShifts(self, value): param = Parameters() param.WITH_SHIFTS = value self.randObj = Random() self.randObj.initialization()
class TestParameters(TestCase): def setUp(self): self.parametersObj = Parameters() self.parametersObj.WITH_SHIFTS = True rand = Random() rand.initialization() def tearDown(self): self.parametersObj = None def test_getTotalTime(self): shift_duration = [5, 9, 2, 1] shift_type = [ Constants.ENTREGA, Constants.ENTREGA, Constants.RECOGIDA, Constants.DUAL ] shift_factor = 3600 # hours self.parametersObj.setParameters(shift_duration, shift_type, shift_factor) self.assertEqual(self.parametersObj.getTotalTime(Constants.ENTREGA), 14, "The delivery time should be 5 + 9") self.assertEqual(self.parametersObj.getTotalTime(Constants.RECOGIDA), 2, "The collection time should be 2") self.assertEqual(self.parametersObj.getTotalTime(Constants.DUAL), 1, "The dual time should be 1") def test_getCurrentShift_Not_Edges(self): shift_duration = [5, 9, 2, 1] # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # E E E E E E E E E E E E E E R R D shift_type = [ Constants.ENTREGA, Constants.ENTREGA, Constants.RECOGIDA, Constants.DUAL ] shift_factor = 3600 # hours self.parametersObj.setParameters(shift_duration, shift_type, shift_factor) self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 2 * 3600), Constants.ENTREGA, "The shift should be ENTREGA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 7 * 3600), Constants.ENTREGA, "The shift should be ENTREGA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 15 * 3600), Constants.RECOGIDA, "The shift should be RECOGIDA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 17 * 3600), Constants.DUAL, "The shift should be DUAL") def test_getCurrentShift_Edges(self): shift_duration = [5, 9, 2, 1] # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # E E E E E E E E E E E E E E R R D shift_type = [ Constants.ENTREGA, Constants.ENTREGA, Constants.RECOGIDA, Constants.DUAL ] shift_factor = 3600 # hours self.parametersObj.setParameters(shift_duration, shift_type, shift_factor) self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 0 * 3600), Constants.ENTREGA, "The shift should be ENTREGA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 13 * 3600), Constants.ENTREGA, "The shift should be ENTREGA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 14 * 3600), Constants.RECOGIDA, "The shift should be RECOGIDA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 15 * 3600), Constants.RECOGIDA, "The shift should be RECOGIDA") self.assertEqual( self.parametersObj.getCurrentShift( Constants.SIMULATION_INITIAL_TIME + 16 * 3600), Constants.DUAL, "The shift should be DUAL")
def many_simulations(t: int): """Run many simulations for different values of densities and display the results Args: t (int): Number of density1 and density2 to test """ densities1 = np.linspace(0.0, 0.6, t) densities2 = np.linspace(0.0, 0.4, t) # Run simulations results = [ [ one_simulation( Parameters( density1=densities1[i1], density2=densities2[i2], nbInitialFires=3, wind=Wind.West, firebreak=False, resistanceTree1=1, resistanceTree2=1.3, transmissibilityFire3=0.3, transmissibilityFire4=0.5, ) ) for i1 in tqdm(range(len(densities1))) ] for i2 in tqdm(range(len(densities2))) ] # Display results print(results) plt.figure(1) for i in range(t): plt.plot( densities1, results[i], label=f"density2 = {densities2[i]}", ) plt.title(f"Final density of trees for different initial values of densities") plt.xlabel("density1") plt.ylabel("final density of all trees") plt.legend() plt.figure(2) difference = [ [ densities1[i1] + densities2[i2] - resultats[i1][i2] for i1 in range(len(densities1)) ] for i2 in range(len(densities2)) ] for i in range(t): plt.plot( densities1, difference[i], label=f"density2 = {densities2[i]}", ) plt.title(f"Difference of density between initial and final values of densities") plt.xlabel("density1") plt.ylabel("loss of density") plt.legend() plt.show()
minutes = int((time % 3600) / 60) hourstr = str(hour) minutesstr = str(minutes) if hour < 10: hourstr = '0' + hourstr if minutes < 10: minutesstr = '0' + minutesstr return hourstr + ':' + minutesstr # Main program # load traces df = pd.read_csv(Constants.OUTPUT_PATH + "/trace.csv") ds = pd.read_csv(Constants.OUTPUT_PATH + "/trace.stats.csv") parameters = Parameters() charts = Charts() if (len(df.index) == 0) or (len(ds.index) == 0): raise Exception("The trace file is empty") shifts = True event = df.iloc[0] if event['Shift'] == '-': shifts = False # timeline chart total_time_cargas = 0 total_time_descargas = 0 total_time_duo = 0
class Core: # CLASS FUNCTIONS def __init__(self, parameters=None): if parameters is None: self.parameters = Parameters() else: self.parameters = parameters num_sources = Constants.DEFAULT_SOURCES num_processors = self.parameters.num_processors # Attributes initialization self.processors = [] self.sources = [] self.eventsList = PriorityQueue(0) # maxsize = 0 (infinite) self.previousTime = Constants.SIMULATION_INITIAL_TIME self.currentTime = Constants.SIMULATION_INITIAL_TIME self.idleProcessors = 0 self.serviceProcessors = 0 self.entitiesSystem = 0 self.service_per_shift = [] self.service_per_total = [] self.shift_durations = self.parameters.getParameters()[1] self.shift_next_time = self.shift_durations[0] self.shift_next_index = 1 # Instance creation self.queue = Queue(Constants.SLOTS_BUFFER) self.parking = Queue(Constants.SLOTS_QUEUE) self.random = Random() for _ in range(0, num_processors): self.processors.append(Processor(self)) for _ in range(0, num_sources): self.sources.append(Source(self)) # Dependency injection for source in self.sources: source.addOutput(self.queue) # source -> queue self.queue.addOutput(self.parking) # queue -> parking self.parking.addInput(self.queue) # parking <- queue for processor in self.processors: self.parking.addOutput(processor) # parking -> processor processor.addInput(self.parking) # processor <- parking self.output_file = None self.numberOfIdleProcessors = num_processors def increaseEntitiesSystem(self): self.entitiesSystem += 1 def decreaseEntitiesSystem(self): self.entitiesSystem -= 1 def startSimulation(self): """Implemented by all modules""" startEvent = Event(self, Constants.START_SIMULATION, self.currentTime, self.currentTime) self.logEvent(startEvent) for source in self.sources: source.startSimulation() def endSimulation(self): """Implemented by all modules""" endEvent = Event( self, # eventCreator Constants.END_SIMULATION, # eventName self.currentTime, # eventScheduled self.currentTime # eventTime ) self.logEvent(endEvent) self.updateState(endEvent) def executeEvent(self, currentEvent): """Implemented by all event creator modules""" if currentEvent.eventName == Constants.START_SIMULATION: self.startSimulation() def run(self): self.logHeaders() # creates output file with flag w+ with open(self.parameters.output_file + '.csv', "a+") as self.output_file: self.startSimulation() while not self.eventsList.empty(): currentEvent = self.eventsList.get() self.updateState(currentEvent) self.logEvent(currentEvent) currentEvent.executeEvent() self.endSimulation() self.stats() def addEvent(self, addedEvent): self.eventsList.put(addedEvent, addedEvent.eventTime) def getCurrentTime(self): return self.currentTime def updateState(self, event): self.previousTime = self.currentTime self.currentTime = event.eventTime timeStep = self.currentTime - self.previousTime self.numberOfIdleProcessors = 0 for processor in self.processors: if processor.isIdle(): self.idleProcessors += timeStep self.numberOfIdleProcessors += 1 else: self.serviceProcessors += timeStep if self.currentTime > self.shift_next_time * 3600: if self.shift_next_index < len(self.shift_durations): self.shift_next_time += self.shift_durations[ self.shift_next_index] self.shift_next_index += 1 if not self.service_per_shift: # first shift - empty list; if == 0 self.service_per_shift.append(self.serviceProcessors) else: self.service_per_shift.append(self.serviceProcessors - self.service_per_total[-1]) self.service_per_total.append(self.serviceProcessors) if event.eventName == Constants.END_SIMULATION and len( self.service_per_shift) < len(self.shift_durations): # print('END_SIMULATION') self.service_per_shift.append(self.serviceProcessors - self.service_per_total[-1]) def getCurrentShift(self): return self.parameters.getCurrentShift(self.currentTime) def logHeaders(self): s = 'Current_Time,' s += 'Event_Name,' s += 'Event_Scheduled,' s += 'Event-Time,' s += 'Idle_Processors,' s += 'Service_Processors,' s += 'Number_Idle_Processors,' s += 'Buffer_Length,' s += 'Queue_Length,' s += 'Entities_System,' s += 'Shift' with open(self.parameters.output_file + '.csv', "w+") as output_file: output_file.write(s + '\n') def logEvent(self, currentEvent): s = str(self.currentTime) + ',' s += currentEvent.eventName + ',' s += str(currentEvent.eventScheduled) + ',' s += str(currentEvent.eventTime) + ',' s += str(self.idleProcessors) + ',' s += str(self.serviceProcessors) + ',' s += str(self.numberOfIdleProcessors) + ',' s += str(self.queue.getQueueLength()) + ',' s += str(self.parking.getQueueLength()) + ',' s += str(self.entitiesSystem) + ',' s += str(self.parameters.getCurrentShift(currentEvent.eventScheduled)) self.output_file.write(s + '\n') def stats(self): s = 'Max_Queue_Length' r = str(self.parking.getMaxQueueLength()) s += ',Processors_Capacity_Used' r += ',' + str( round( 100 * self.serviceProcessors / (self.parameters.num_processors * Constants.SIMULATION_DURATION), 2)) # in % shift_type, shift_duration = self.parameters.getParameters() for idx in range(len(shift_type)): s += ',Shift_Type' r += ',' + shift_type[idx] s += ',Shift_Duration' r += ',' + str(shift_duration[idx]) s += ',Shift_Capacity_Usage' r += ',' + str( round( 100 * self.service_per_shift[idx] / (self.parameters.num_processors * self.shift_durations[idx] * 3600), 2)) with open(self.parameters.output_file + '.stats.csv', "w+") as output_file: output_file.write(s + '\n') output_file.write(r + '\n')
print('Options:') print('-h, --help\t\tShows the program usage help.') print('-p, --processors=...\tSets the number of processors.') # MAIN FUNCTION if __name__ == "__main__": # Default arguments processors = Constants.DEFAULT_PROCESSORS flag_experimenter = False shift_duration = [] shift_type = [] shift_factor = 0 parameters = Parameters() # Get arguments try: opts, args = getopt.getopt(sys.argv[1:], 'hp:e', ['help', 'processors=', 'experimenter']) for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit() if opt in ('-p', '--processors'): num_processors = int(arg) parameters.setNumProcessors(num_processors) if opt in ('-e', '--experimenter'): flag_experimenter = True except getopt.GetoptError:
else: # shift == '11' new_shift_type.append(Constants.DUAL) new_shift_duration.append(1) return new_shift_type, new_shift_duration # MAIN NUM_GENERATIONS = 50 NUM_INDIVIDUALS = 50 NUM_KEEP_BEST = int(2 * NUM_INDIVIDUALS / 5) # 2/5 NUM_OFFSPRING = int(2 * NUM_INDIVIDUALS / 5) # 2/5 CHANCE_KEEP_BAD = 0.05 CHANCE_MUTATION = 0.05 seed(664) # seed tested: 6, 1, 21, 25, 200, 212, 60, 78, 1024, 789, 3379, 567, 4655, 878, 123, 46, 1221, 111, 777, 664 parameters = Parameters() population = [] # Check consistency while NUM_KEEP_BEST < 0 or NUM_OFFSPRING < 0 or NUM_KEEP_BEST + NUM_OFFSPRING > NUM_INDIVIDUALS: print('NUM_KEEP_BEST + NUM_OFFSPRING cannot be higher than', NUM_INDIVIDUALS) NUM_KEEP_BEST = int(input('Enter new value for NUM_KEEP_BEST:')) NUM_OFFSPRING = int(input('Enter new value for NUM_OFFSPRING:')) # GEN 0 while len(population) < NUM_INDIVIDUALS: gen_individual = get_new_individual() population.append(gen_individual) # Begin selection
from src.Scene import Scene from src.Wind import Wind CELL_SIZE = 10 SCREEN_SIZE = (600, 600) # (900,900) (1280,1280) GRID_DIM = tuple(map(lambda x: int(x / CELL_SIZE), SCREEN_SIZE)) REFRESH_TIME = 10000 COLORS = [(255, 255, 255), (26, 174, 70), (7, 70, 22), (194, 46, 28), (107, 30, 30)] parameters = Parameters( density1=0.6, # The sum of densities must be included between 0 and 1 density2=0.3, nbInitialFires=3, wind=Wind.West, # South, West, North, East firebreak=True, resistanceTree1=1, resistanceTree2=1.3, transmissibilityFire3=0.3, transmissibilityFire4=0.5, ) def one_simulation(parameters: Parameters): """Make one entire simulation for one configuration Args: parameters (Parameters): parameters for the simulation Returns: float: final density of all trees
try: inputFile = sys.argv[1] except: usage() sys.exit() # Start core with open(Constants.INPUT_PATH + inputFile, 'r') as ifs: population = [] fitness = {} content = ifs.readlines() for line in content: if line: args = line.split() parameters = Parameters() shift_duration = [] shift_type = [] shift_factor = 3600 duration_total = 0 idx = 2 while duration_total < Constants.SIMULATION_DURATION / 3600: in_shift_type = str(args[idx]) in_shift_duration = int(args[idx + 1]) idx += 2 if duration_total + in_shift_duration <= Constants.SIMULATION_DURATION / 3600 and \ in_shift_type in (Constants.ENTREGA, Constants.RECOGIDA, Constants.DUAL): duration_total += in_shift_duration shift_duration.append(in_shift_duration) shift_type.append(in_shift_type)
def __init__(self, steps_g, number_average, p_mutation): Parameters.__init__(self) self.g = 2 # number of generation self.steps_g = steps_g self.number_average = number_average self.p_mutation = p_mutation
def setWithShifts(cls, value): param = Parameters() param.WITH_SHIFTS = value cls.randObj = Random() cls.randObj.initialization()
def setUp(self): self.parametersObj = Parameters() self.parametersObj.WITH_SHIFTS = True rand = Random() rand.initialization()
def setUp(self): param = Parameters() param.WITH_SHIFTS = True self.randObj = Random() self.randObj.initialization() self.coreObj = Core()