Exemplo n.º 1
0
    def update(self):

        state = self._algorithm.get_state()

        # self._state = {
        #    "iteration" : self._generation,
        #    "message"   : message,
        #    "content"   : content,
        #    "fittest"   : self._fittest
        #}

        # Parse
        iteration = 0
        if "iteration" in state: iteration = state["iteration"]
        message = ""
        if "message" in state: message = state["message"]
        content = ""
        if "content" in state: content = state["content"]
        fittest = None
        if "fittest" in state: fittest = state["fittest"]

        if message == "Genetic Algorithm":
            Terminal.print_box(messages=[message], font_color=FontColor.Green)
        elif message == "Configuration":
            print(message)
            print(content)
        elif message == "Fittest Solution":
            if fittest:
                Terminal.print_box(
                    messages=[message, f"fitness = {fittest.fitness}"],
                    font_color=FontColor.Green)
        else:
            if fittest:
                print(
                    f"Generation {iteration:7d} - fitness: {fittest.fitness}")
Exemplo n.º 2
0
 def _build_solution(self):
     Terminal.print_box(messages=[
         f"Build Solution - ENCODING: Can repeat = {self._problem_instance.encoding.can_repeat_elements} - Is Ordered = {self._problem_instance.encoding.is_ordered }"
     ])
     solution = None
     for _ in range(0, 8):
         try:
             solution = self._problem_instance.build_solution()
             print(f"solution = {solution.representation}")
         except:
             self._errors.append("Build Solution Failed!")
             if solution == None:
                 print(
                     " - Error: Check if build solution is OK or if it was implemented"
                 )
Exemplo n.º 3
0
    def run(self):
        Terminal.print_box(messages=[
            f"Neighborhood Function - ENCODING: Can repeat = {self._problem_instance.encoding.can_repeat_elements} - Is Ordered = {self._problem_instance.encoding.is_ordered }"
        ])
        for _ in range(0, 2):
            solution = self._problem_instance.build_solution()

            neighbors = self._neighborhood_function(
                solution=solution,
                problem=self._problem_instance,
                neighborhood_size=self._neighborhood_size)

            Terminal.print_line(
                message=f"\nNeighbors of {solution.representation} are:")

            for neighbor in neighbors:
                print(f"             {neighbor.representation} ")
Exemplo n.º 4
0
    def _evaluate(self):
        Terminal.print_box(messages=["Objective Function"])

        feedback = None
        if self._require_feedback:
            feedback = self._problem_instance.build_solution()
            for _ in range(0, 8):
                solution = self._problem_instance.build_solution()
                self._problem_instance.evaluate_solution(solution=solution,
                                                         feedback=feedback)
                print(
                    f" solution = {solution.representation} | feedback = { feedback.representation } | fitness = {solution.fitness}"
                )
        else:
            for _ in range(0, 8):
                solution = self._problem_instance.build_solution()
                self._problem_instance.evaluate_solution(solution=solution)
                print(
                    f" solution = {solution.representation} | fitness = {solution.fitness}"
                )
Exemplo n.º 5
0
 def _is_admissible(self):
     Terminal.print_box(messages=[
         f"Is Admissible Solution - {self._problem_instance.constraints}"
     ])
     Terminal.print_line(" Constraints DV ")
     print(self._constraint_dv)
     solution = None
     for _ in range(0, 8):
         try:
             solution = self._problem_instance.build_solution()
             is_admissible = self._problem_instance.is_admissible(solution)
             print(
                 f"solution = {solution.representation}  - Is admissible? {is_admissible}"
             )
         except:
             self._errors.append("Is admissible solution Failed!")
             if solution == None:
                 print(
                     " - Error: Check if build solution is OK or if it was implemented"
                 )
Exemplo n.º 6
0
    def run(self):
        self._errors = []

        Terminal.clear()
        Terminal.print_box(messages=["Name: " + self._problem_instance._name],
                           font_color=FontColor.Yellow)
        try:
            print(f" Objective       = {self._problem_instance.objective}")
        except:
            print("ERROR: " + self._problem_instance.objective)
            self._errors.append()

        print(
            f" Multi-objective = {self._problem_instance.is_multi_objective}")

        print(f" Encoding Rule = {self._problem_instance.encoding_rule }")

        self._build_solution()

        self._is_admissible()

        self._evaluate()
Exemplo n.º 7
0
    def update(self):
        state = self._algorithm.get_state()

        # {
        #    "problem"   : self._problem_instance,
        #    "iteration" : self._iteration,
        #    "message"   : message,
        #    "solution"  : self._solution,
        #    "neighbor"  : self._neighbor
        # }

        message = ""
        if "message" in state: message = state["message"]

        # started
        if message == LocalSearchMessage.Started:
            Terminal.clear()
            Terminal.print_box(
                messages=[self._algorithm.name, self._algorithm.description],
                font_color=FontColor.Green)
            Terminal.print_box(
                messages=["Problem: " + self._algorithm.problem.name],
                font_color=FontColor.Yellow)

        # initialized
        elif message == LocalSearchMessage.Initialized:

            Terminal.print_box(messages=["Initialized"])
            Terminal.print_line(message="Initial Solution:")
            print(
                f"   Solution: {self._algorithm.solution.representation} - fitness: {self._algorithm.solution.fitness}"
            )
            Terminal.print_box(messages=["Iterations"])

        elif message == LocalSearchMessage.ReplacementAccepted:
            iteration = -1
            if "iteration" in state:
                iteration = state["iteration"]

            Terminal.print_line(
                message=
                f"Iteration {iteration:10d} | Solution: {self._algorithm.solution.representation } | Fitness: {self._algorithm.solution.fitness}"
            )

        elif message == LocalSearchMessage.ReplacementRejected:
            iteration = -1
            if "iteration" in state:
                iteration = state["iteration"]

            Terminal.print_line(
                message=
                f"Iteration {iteration:10d} | Solution: {self._algorithm.solution.representation } | Fitness: {self._algorithm.solution.fitness} *** (no change)"
            )

        elif message == LocalSearchMessage.StoppedPrematurely:
            Terminal.print_box(messages=["Stopped Prematurely!"],
                               font_color=FontColor.Yellow)

        elif message == LocalSearchMessage.Stopped:
            Terminal.print_box(messages=["Stopped Max Iterations!"])

        elif message == LocalSearchMessage.StoppedTargetAchieved:
            Terminal.print_box(messages=["Stopped Target Achieved!"],
                               font_color=FontColor.Green)