Esempio n. 1
0
def main():
    problem_file = sys.argv[1]
    minutes = int(sys.argv[2])

    problem = Problem(problem_file)

    parser = ProblemParser(problem)
    parser.parse()

    solutions_count = 0
    time_end = time.time() + 60 * minutes
    while time.time() < time_end:
        problem.make_objects()
        if problem.solve():
            solutions_count += 1

    print("Solutions count", solutions_count)
    if problem.best_tracks is not None:
        print("Best: ", problem.best_gg1, problem.best_gg2)
    else:
        print("Optimal: ", problem.optimal_gg1, problem.optimal_gg2)
    print("Invocation count", problem.grader.invocation_count)
    output_file_name = get_output_file_name(minutes, problem_file)
    writer = SolutionWriter(problem)
    writer.write(output_file_name)
Esempio n. 2
0
    def main(self):
        filename = "sudoku.txt"
        problem = Problem()
        problem.readFromFile(filename)

        contr = Controller(problem)

        cons = Console(contr)
        cons.mainMenu()
Esempio n. 3
0
    def solve_smart(problem: Problem):
        tasks = problem.tasks
        # Sort decreasing considering (L-E)/T - cost per time unit
        tasks = sorted(
            tasks,
            key=lambda item: (item.too_late_penalty - item.too_early_penalty) / item.processing_time,
            reverse=True
        )

        # Split into two parts L and R considering CDD as split point
        time_sum = 0
        left_part = []
        right_part = []
        for task in tasks:
            time_sum += task.processing_time
            if time_sum < problem.common_due_date:
                left_part.append(task)
            else:
                right_part.append(task)

        # Sort left side by E/T - increasing cost per time unit for too early delay
        left_part = sorted(left_part, key=lambda item: item.too_early_penalty / item.processing_time)
        # Sort right side by L/T - decreasing cost per time unit for too late delay
        right_part = sorted(right_part, key=lambda item: item.too_late_penalty / item.processing_time, reverse=True)
        # Connect L and R
        problem.tasks = left_part + right_part

        # Move starting point from 0 to CDD searching for minimum cost
        costs = []
        for index in range(problem.common_due_date):
            problem.starting_point = index
            costs.append(CostCounter.count(problem))
        min_cost_index = costs.index(min(costs))
        problem.starting_point = min_cost_index

        return Solution(problem, costs[min_cost_index])
Esempio n. 4
0
class StateMachine:
    def __init__(self):
        self.State_Machine_id = 0
        self.Problem = Problem()
        self.State = State()

    def set_problem(self, problem=Problem()):
        if type(problem) == Problem:
            self.Problem = problem
        else:
            print("warning !!! StateMachine. set_problem")

    def get_problem(self):
        return self.Problem

    def set_state(self, state):
        self.State = state

    def change_state(self, character_list=CharacterList()):
        old_state = copy.deepcopy(self.State)
        situation_list = self.Problem.get_situation_list()
        situation = situation_list.find_situation(character_list)
        if situation is not None:
            if situation.get_step() > self.State.get_step(
            ) and situation.is_right():
                self.State.set_step(situation.get_step())
        else:
            situation = DefaultSituation()

        return self.make_result(old_state, self.State, situation)

    def make_result(self,
                    old_state=State(),
                    new_state=State(),
                    situation=Situation()):
        out_result = Result()
        if old_state.get_step() < new_state.get_step() and situation.is_right(
        ):
            out_result.set_right()
        else:
            out_result.set_wrong()
        out_result.set_step(new_state.get_step())
        out_result.set_feedback(situation.get_feedback())
        out_result.set_end(situation.is_end)
        return out_result
Esempio n. 5
0
    def read(file_path: str, boundary: int):
        problems = []
        with open(file_path, 'r') as file:
            data = file.read()
        lines = data.split('\n')
        counter = 0

        number_of_problems = int(lines[counter])
        counter += 1

        index = 0
        while counter < len(lines) and lines[counter] != '':
            number_of_tasks = int(lines[counter])
            counter += 1
            problems.append(Problem(lines[counter:counter + number_of_tasks], boundary, index))
            index += 1
            counter += number_of_tasks

        assert len(problems) == number_of_problems
        return problems
Esempio n. 6
0
 def order_tasks(problem: Problem, order: list):
     result = []
     for index in order:
         result.append(problem.tasks[index])
     problem.tasks = result
     return problem
Esempio n. 7
0
 def solve_random(problem: Problem):
     shuffle(problem.tasks)
     problem.starting_point = random.randrange(0, 10)
     cost = CostCounter.count(problem)
     return Solution(problem, cost)
Esempio n. 8
0
 def set_problem(self, problem=Problem()):
     if type(problem) == Problem:
         self.Problem = problem
     else:
         print("warning !!! StateMachine. set_problem")
Esempio n. 9
0
 def __init__(self):
     self.State_Machine_id = 0
     self.Problem = Problem()
     self.State = State()
Esempio n. 10
0
def build_problem(processed_data=ProcessedData()):
    out_problem = Problem(processed_data.get_processed_problem_data().get_name(), build_situation_list(processed_data.get_processed_step_data_list()) )
    return out_problem
Esempio n. 11
0
 def __init__(self, *args, **kwargs):
     self.problem = Problem(args[0], args[1], args[2])
     print(' ')
     print(self.breadthFirstSearch(self.problem))
"""
Tests problem
"""
import json
from problem.Problem import Problem

s = Problem()
s.html = s.getProblem()

print(json.dumps(s.html))