def img_to_text(file_path: str, pool: ThreadPool, debug: bool) -> Instance: img = normalize_image(file_path) w, h = img.size question_text, question_size = question_to_text(deepcopy(img), w, h, debug) answers_text = answers_to_text(deepcopy(img), w, h, question_size, pool, debug) return Instance.create_instance(question_text, answers_text[0], answers_text[1], answers_text[2])
def CheckSolution(sol): """ sol name or """ if isinstance(sol, str): if sol[-4:] != '.json': sol += '.sol.json' fp = open(f'solutions/{sol}') sol = json.load(fp) name = sol['instance'] I = Instance.from_file(name) return check_solution(I, sol)
def MinimizeMakespan(I, save=False): """ I fname | Instance """ if isinstance(I, str): I = Instance.from_file(I) sv = Solver1() ans = sv.solve(I, pad=20, th=.01) # th circulation density sol = {'instance': I.name, 'steps': ans} assert (check_solution(I, sol)) if save: fp = open(f'solutions/{I.name}.sol.json', 'w') json.dump(sol, fp) return sol
'points': point[instance.third_answer] }, ], }) d = json.dumps(questions) with open('dump.txt', 'w') as the_file: the_file.write(d) elif args.test_dump: with open('dump.txt') as json_file: data = json.load(json_file, strict=False) switch = Switch(pool) for question in data: instance = Instance.create_instance( question['question'], question['answers'][0]['first_answer'], question['answers'][1]['second_answer'], question['answers'][2]['third_answer']) instance.print_question() switch.run(instance) key = input() elif args.test_dump_id: with open('dump_patty.txt') as json_file: data = json.load(json_file, strict=False) switch = Switch(pool) for question in data: if question['index'] == args.test_dump_id: instance = Instance.create_instance( question['question'], question['answers'][0]['first_answer'], question['answers'][1]['second_answer'],
def test_init(self): """ Tests the initialization process """ n_p_units = 100 n_days = 7 n_tasks = 5 inst = Instance(n_p_units, n_days, n_tasks) # # Test if the solution attribute has the correct shape. # self.assertEqual(len(inst.solution), n_days*n_p_units*n_tasks) # Test if the instance accepts the correct shaped variables. Qjk = 2 * randint(0, high=2, size=(n_days, n_tasks)) QCjk = Qjk QGjk = Qjk Ci = randint(0, high=2, size=n_p_units) Gi = randint(0, high=2, size=n_p_units) Fijk = set([(randint(0, n_p_units), randint(0, n_days), randint(0, n_tasks)) for i in range(15)]) Rijk = set([(randint(0, n_p_units), randint(0, n_days), randint(0, n_tasks)) for i in range(15)]) Rijk = list(Rijk.difference(Fijk)) Fijk = list(Fijk) inst.n_parents = Ci inst.n_women = Gi inst.force = Fijk inst.reject = Rijk inst.people_per_task = Qjk inst.parents_per_task = np.copy(QCjk) inst.women_per_task = np.copy(QGjk) # Check the pre-decision consistency. inst.check_consistency() # Check parent data inconsistency (QCjk > Qjk for some jk) with self.assertRaises(ConsistencyError): QCjk = inst.parents_per_task QCjk[0, 0] = QCjk[0, 0] + 1 inst.parents_per_task = QCjk inst.check_consistency() # Get back to consistent state inst.parents_per_task = np.copy(inst.people_per_task) # Check gender data inconsistency (QGjk > Qjk for some jk) with self.assertRaises(ConsistencyError): QGjk = inst.women_per_task QGjk[0, 0] = QGjk[0, 0] + 1 inst.women_per_task = QGjk inst.check_consistency() # Get back to consistent state inst.parents_per_task = np.copy(inst.people_per_task) # Test with the wrongly shaped variables (Should raise ShapeError) Qjk = 2 * randint(0, high=2, size=(n_days + 1, n_tasks + 1)) QCjk = np.copy(Qjk) QGjk = np.copy(Qjk) Ci = randint(0, high=2, size=n_p_units + 1) Gi = randint(0, high=2, size=n_p_units + 1) Fijk = set([(randint(0, n_p_units), randint(0, n_days)) for i in range(15)]) Rijk = set([(randint(0, n_p_units), randint(0, n_days)) for i in range(15)]) Rijk = list(Rijk.difference(Fijk)) Fijk = list(Fijk) with self.assertRaises(ShapeError): inst.n_parents = Ci with self.assertRaises(ShapeError): inst.n_women = Gi with self.assertRaises(ShapeError): inst.force = Fijk with self.assertRaises(ShapeError): inst.reject = Rijk with self.assertRaises(ShapeError): inst.people_per_task = Qjk with self.assertRaises(ShapeError): inst.parents_per_task = QCjk with self.assertRaises(ShapeError): inst.women_per_task = QGjk
def main(): # Open the configuration file with open('config.yml', 'r') as ymlfile: cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader) ficha_path = cfg['ficha_servo_path'] demanda_path = cfg['demanda_path'] indisp_path = cfg['indisp_path'] ficha_info = FichaServoInfo( id_column=cfg['ficha_servo_info']['id_column'], exp_column=cfg['ficha_servo_info']['exp_column'], gend_column=cfg['ficha_servo_info']['gend_column'], task_columns=cfg['ficha_servo_info']['task_columns'], name_column=cfg['ficha_servo_info']['name_column']) demanda_info = DemandaInfo( people_column=cfg['demanda_info']['people_column'], women_column=cfg['demanda_info']['women_column'], parents_column=cfg['demanda_info']['parents_column']) indisp_info = IndispInfo(id_column=cfg['indisp_info']['id_column'], name_column=cfg['indisp_info']['name_column'], day_columns=cfg['indisp_info']['day_columns']) # Get the problem info from the excel files prb_info = get_problem_info(ficha_path, demanda_path, indisp_path, exp_threshold=cfg['exp_threshold'], ficha_servo_info=ficha_info, demanda_info=demanda_info, indip_info=indisp_info) # Add the couples/groups to the problem info couples = [literal_eval(couple) for couple in cfg['couples']] for couple in couples: prb_info = make_group(couple, prb_info) # Make the problem instance from the problem info instance = Instance(prb_info.n_p_units, prb_info.n_days, prb_info.n_tasks, max_sols=cfg['maximum_number_of_solutions']) instance.n_people = prb_info.n_people instance.n_women = prb_info.n_women instance.n_parents = prb_info.n_parents instance.people_per_task = prb_info.people_per_task instance.women_per_task = prb_info.women_per_task instance.parents_per_task = prb_info.parents_per_task instance.force = prb_info.force instance.reject = prb_info.reject # Solve the instance instance.solve(one_alloc_period=cfg['one_allocation_every_how_many_weeks']) # Write the solutions to excel file(s) solutions_path = osp.join(osp.dirname(osp.abspath(__file__)), 'solutions') single_file = cfg['solutions_in_single_file'] if single_file: wb = Workbook() dest_filename = osp.join(solutions_path, '{}.xlsx'.format(cfg['solution_name'])) remove_sheet = wb.active remove_sheet.title = 'remove' else: solution_dir = dest_filename = osp.join(solutions_path, cfg['solution_name']) mkdir(solution_dir) for i, solution in enumerate(instance.solution_list): solution_array, days_worked = sol_to_array(solution, prb_info) if single_file: sheet = wb.create_sheet('Solution{}'.format(i)) else: wb = Workbook() dest_filename = osp.join(solution_dir, 'solution_{}.xlsx'.format(i)) sheet = wb.active sheet.title = "Solution{}".format(i) for row in range(len(solution_array)): for column in range(len(solution_array[0])): sheet.cell(column=column + 1, row=row + 1, value=solution_array[row][column]) p_counter_column = len(solution_array[0]) + 2 sheet.cell(column=p_counter_column, row=1, value='Name') sheet.cell(column=p_counter_column + 1, row=1, value='Number of days worked') for j, name in enumerate(prb_info.names): sheet.cell(column=p_counter_column, row=j + 2, value=name) sheet.cell(column=p_counter_column + 1, row=j + 2, value=days_worked[j]) if not single_file: wb.save(filename=dest_filename) if single_file: wb.remove(wb.get_sheet_by_name('remove')) wb.save(filename=dest_filename)
model = Models() model.execute(http, command_to_execute) for key, value in command_to_execute["data_source"].items(): if value is not None: #logger.debug("key exists "+str(key)) logger.debug("Executing the command input") data_source = Data_source() data_source.execute(http, command_to_execute) for key, value in command_to_execute["data_output"].items(): if value is not None: #logger.debug("key exists "+str(key)) logger.debug("Executing the command output") data_output = Data_output() data_output.execute(http, command_to_execute) for key, value in command_to_execute["command"].items(): if value is not None: #logger.debug("key exists "+str(key)) logger.debug("Executing the command") command = Command() command.execute(http, command_to_execute) for key, value in command_to_execute["instance"].items(): if value is not None: #logger.debug("key exists "+str(key)) logger.debug("Creating an instance") instance = Instance() instance.execute(http, command_to_execute)
def test_sat(self): """ Tests """ names = [ 'Pedro e Kim', 'Tania', 'Clarissa', 'Guilherme', 'Rafael', 'Andressa', 'Carlinhos', 'Debora', 'Carol e Rafaela', 'Bruna', 'Manu', 'Erika Mesq.', 'Alane', 'Carol e Leo', 'Larissa', 'Lucas Mendes', 'Marcus', 'Pericles', 'Sayro', 'Luciana', 'Aline Palm.', 'Belinha', 'Eliane', 'Aline Wiez.', 'Ranuzia', 'Grazi', 'Socorro' ] inst = Instance(27, 6, 2) # Define the p units. inst.n_people = np.array([ 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]) inst.n_women = np.array([ 1, 1, 1, 0, 0, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 ]) inst.n_parents = np.array([ 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 ]) # One baby task (4 people - 2 parents - 2 women) and one kids task # (2 people - 0 parents - 1 women) inst.people_per_task = np.array([[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]) inst.women_per_task = np.array([[2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1]]) inst.parents_per_task = np.array([[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]) inst.force = [(7, 1, 0)] inst.reject = [(2, 0, 1), (5, 1, 1)] inst.solve() solution = inst.solution_list[0] # [Constraint 1: Demand of people per task] for j in range(inst.n_days): for k in range(inst.n_tasks): self.assertTrue( np.dot(solution[:, j, k], inst.n_people) == inst.people_per_task[j, k]) # [Constraint 2: Min number of parents] for j in range(inst.n_days): for k in range(inst.n_tasks): self.assertTrue( np.dot(solution[:, j, k], inst.n_parents) >= inst.parents_per_task[j, k]) # [Constraint 3: Min number of women] for j in range(inst.n_days): for k in range(inst.n_tasks): self.assertTrue( np.dot(solution[:, j, k], inst.n_women) >= inst.women_per_task[j, k]) # [Constraint 4: One task per day] for i in range(inst.n_p_units): for j in range(inst.n_days): self.assertTrue(np.sum(solution[i, j, :]) <= 1) # [Constraint 5: Force Variables] for triple in inst.force: self.assertTrue(solution[triple] == 1) # [Constraint 6: Reject Variables] for triple in inst.reject: self.assertTrue(solution[triple] == 0) # [Constraint 7: Max one allocation per 4 weeks] for i in range(inst.n_p_units): for j in range(inst.n_days - 4): self.assertTrue(np.sum(solution[i, j:(j + 4), :]) <= 1)