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()
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")
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) else: raise Exception('Wrong format: time limit exceeded.') parameters.setParameters(shift_duration, shift_type, shift_factor) output_file = str(args[0]) parameters.output_file = Constants.OUTPUT_PATH + output_file # RUN CORE path_list = output_file.split('/') filename = str(path_list[len(path_list) - 1:][0]) if filename not in fitness: population.append(filename) s = ' Testing ' + filename + '...' print(s) core = Core() core.run() fitness[filename] = Auxiliary.get_fitness( parameters.output_file)
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 fitness = {} for generation in range(NUM_GENERATIONS): print('Generation', generation) for individual in population: if individual not in fitness: shift_type, shift_duration = individual_to_parameters(individual) parameters.setParameters(shift_duration, shift_type, 3600) print(' Testing ' + individual + '...') core = Core() core.run() fitness[individual] = Auxiliary.get_fitness(parameters.output_file) population = sorted(population, key=lambda idv: fitness[idv], reverse=True) if generation < NUM_GENERATIONS - 1: fittest = population[:NUM_KEEP_BEST] unfit = population[NUM_KEEP_BEST:] population = fittest while len(population) < NUM_KEEP_BEST + NUM_OFFSPRING: individual1, individual2 = sample(fittest, 2) population.append(operator_crossover(individual1, individual2)) while len(population) < NUM_INDIVIDUALS: if random() < CHANCE_KEEP_BAD: population.append(unfit[NUM_INDIVIDUALS - len(population) - 1])