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 ) Terminal.print_line( message = f"\nNeighbors of {solution.representation} are:") for neighbor in neighbors: print(f" {neighbor.representation} ")
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")
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}" )
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._evaluate()
from lab4.algorithm.hill_climbing import HillClimbing from lab4.problem.mastermind_problem import MastermindProblem, mastermind_get_neighbors, mastermind_encoding2 from lab4.util.observer import LocalSearcObserver from lab4.util.terminal import Terminal, FontColor master_mind_problem = MastermindProblem(encoding_rule=mastermind_encoding2) secret = master_mind_problem.build_solution() # Hill Climbing 1 - Classical Stop Condition hc1 = HillClimbing(problem_instance=master_mind_problem, neighbohood_function=mastermind_get_neighbors, params={ "Maximum-Iterations": 10, "Stop-Conditions": "Alternative-01", "Target-Fitness": 40 }, feedback=secret) observer1 = LocalSearcObserver(hc1) hc1.register_observer(observer1) hc1.search() Terminal.print_box(messages=[f"Secret: {secret.representation}"], font_color=FontColor.Cyan)
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 Taget Achieved!"], font_color=FontColor.Green)
from lab4.problem.mastermind_problem import MastermindProblem, mastermind_encoding1, mastermind_encoding2, mastermind_get_neighbors from lab4.problem.solution import LinearSolution from lab4.problem.problem_template import ProblemTemplate from lab4.util.terminal import Terminal, FontColor # The problem instance must define the mastermind_problem_instance1 = MastermindProblem() mastermind_problem_instance2 = MastermindProblem( encoding_rule=mastermind_encoding2) Terminal.print_box(messages=["MASTERMIND PROBLEM"]) print(f" Objective = {mastermind_problem_instance1.objective}") print(f" Multi-objective = {mastermind_problem_instance1.is_multi_objective}") # ----------------------------------------------------------------------------------------------- # Build Solution Function: build_solution() TESTS # ----------------------------------------------------------------------------------------------- # Can-Repeat = False - Objetive Function: evaluate_solution() TESTS # ----------------------------------------------------------------------------------------------- Terminal.print_box(messages=["CanRepeat = False | Build Solution"]) for _ in range(0, 8): solution = mastermind_problem_instance1.build_solution() print(f"solution = {solution.representation}") # Can-Repeat = False - Objetive Function: evaluate_solution() TESTS # ----------------------------------------------------------------------------------------------- Terminal.print_box(messages=["CanRepeat = False | Build Solution"]) for _ in range(0, 8): solution = mastermind_problem_instance2.build_solution() print(f"solution = {solution.representation}")