def report_state(self, data): state = State.from_json(data) if self.state_keeper.challenge_state(state): print "Energy of current state:", self.state_evaluator.evaluate(state) return 'Accepted' else: return 'Discarded'
def report_state(self, data): state = State.from_json(data) if self.state_keeper.challenge_state(state): print "Energy of current state:", self.state_evaluator.evaluate(state) return "Accepted" else: return "Discarded"
def run(self): evaluator = TablePositionAgnosticClosnessEvaluator() searcher = SingleThreadedSearcher( ClosenessStepper(evaluator), SquareStateEvaluator(evaluator), PrintLogger() ) while True: response = requests.get('http://%s:%s/get_best_state' % (self.addr, self.port)) state = State.from_json(response.content) state, _ = searcher.search(state, n=1000) requests.post('http://%s:%s/report_state' % (self.addr, self.port), data=state.to_json())
def run(self): evaluator = TablePositionAgnosticClosnessEvaluator() searcher = SingleThreadedSearcher(ClosenessStepper(evaluator), SquareStateEvaluator(evaluator), PrintLogger()) while True: response = requests.get('http://%s:%s/get_best_state' % (self.addr, self.port)) state = State.from_json(response.content) state, _ = searcher.search(state, n=1000) requests.post('http://%s:%s/report_state' % (self.addr, self.port), data=state.to_json())
def _to_state(excel_data): """ @type excel_data: ExcelData """ group_names = [] group_weights = [] group_indexes = [] # Check that placements still respect dimensions constraint reset_placement = False if excel_data.dimensions is not None: for (dimensions_name, sizes), (placement_name, positions) in zip( excel_data.dimensions, excel_data.placements): if dimensions_name != placement_name: reset_placement = True break for size, position in zip(sizes, positions): if size != len(position): reset_placement = True if not reset_placement: # Allocate room given placement cnt = 0 for placement_name, positions in excel_data.placements: group_names.append(placement_name) group_indexes.append([cnt, cnt + len(positions)]) group_weights.append(1) cnt += len(positions) else: # Allocate room given dimensions cnt = 0 for dimension_name, sizes in excel_data.dimensions: group_names.append(dimension_name) group_indexes.append([cnt, cnt + len(sizes)]) group_weights.append(1) cnt += len(sizes) # Allocate room for groups placement_names = set(group_names) for group_name, group in excel_data.groups: group_name, weight_str = re.match(r"\s*([^(]*)\s*(?:\((\d+)\))?", group_name).groups() if weight_str: weight = int(weight_str) else: weight = 1 if group_name in placement_names: continue group_names.append(group_name.strip()) group_indexes.append([cnt, cnt + 1]) group_weights.append(weight) cnt += 1 names = excel_data.names matrix = numpy.zeros((len(names), cnt), dtype=int) fixed = numpy.zeros((len(names), cnt), dtype=bool) person_index_by_name = {name: i for i, name in enumerate(names)} if not reset_placement: # Fill in positions from placement matrix_col = 0 for _, positions in excel_data.placements: for position in positions: for person, is_fixed in position: matrix_row = person_index_by_name[person] matrix[matrix_row, matrix_col] = 1 fixed[matrix_row, matrix_col] = is_fixed matrix_col += 1 else: # Fill in positions in name order groups_by_group_name = { group_name: group for group_name, group in excel_data.groups } for (i, j), (dimension_name, sizes) in zip(group_indexes, excel_data.dimensions): # Traverse names in group with same name (or all persons if there is no such group) persons = groups_by_group_name.get(dimension_name, names) person_iter = iter(persons) try: for matrix_col, size in zip(range(i, j), sizes): for _ in range(size): person = next(person_iter) matrix_row = person_index_by_name[person] matrix[matrix_row, matrix_col] = 1 fixed[matrix_row, matrix_col] = False next(person_iter) msg = "Not enough places for all %d persons in '%s' to fit in the given tables." % ( len(persons), dimension_name) raise Exception(msg) except StopIteration: pass matrix_col = j for group_name, group in excel_data.groups: if group_name in placement_names: continue for name in group: matrix_row = person_index_by_name[name] matrix[matrix_row, matrix_col] = 1 matrix_col += 1 geometry = matrix.copy().transpose() return State(names=names, group_names=group_names, group_indexes=group_indexes, group_weights=group_weights, seating=matrix, fixed=fixed, geometry=geometry)
def test_json(initial): json = initial.to_json() actual = State.from_json(json) _assert_same_state(initial, actual)
def read_text(content): """ @type content: str """ group_names = [] group_weights = [] groups = [] group = [] position = [] for line in BytesIO(content): if line.startswith('#'): name, weight_str = re.match(r"#\s*([^(]*)\s*(?:\((\d+)\))?", line.strip()).groups() group_names.append(name.strip()) weight = int(weight_str) if weight_str else 1 group_weights.append(weight) if position: group.append(position) position = [] if group: groups.append(group) group = [] elif line.strip() == '': if position: group.append(position) position = [] else: fixed = line.startswith('*') if fixed: line = line[1:] position.append([line.strip(), fixed]) if position: group.append(position) if group: groups.append(group) matrix_col = 0 group_indexes = [] for group in groups: group_indexes.append([matrix_col, matrix_col + len(group)]) matrix_col += len(group) names = list({ name for group in groups for position in group for (name, _) in position }) names.sort() matrix = numpy.zeros((len(names), group_indexes[-1][1]), dtype=int) fixed = numpy.zeros((len(names), group_indexes[-1][1]), dtype=bool) persons_by_name = {name: idx for idx, name in enumerate(names)} matrix_col = 0 for group in groups: for position in group: for name, is_fixed in position: matrix_rox = persons_by_name[name] matrix[matrix_rox, matrix_col] = 1 fixed[matrix_rox, matrix_col] = is_fixed matrix_col += 1 geometry = matrix.copy().transpose() return State(names=names, group_names=group_names, group_indexes=group_indexes, group_weights=group_weights, seating=matrix, fixed=fixed, geometry=geometry)