Commit 0a0536c3 authored by Jonas Boysen's avatar Jonas Boysen
Browse files

improve sa

parent bf075a4c
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
%matplotlib ipympl
from field_optimization import Simulation, FieldVisualizer, IPyUI

def random_search_successor(current_state: list, neighbor_states: list[list], neighbor_costs: list) -> [list, bool]:
    terminate = True
    return current_state, terminate

def stochastic_hc_successor(current_state: list, neighbor_states: list[list], neighbor_costs: list) -> [list, bool]:
    terminate = True
    return current_state, terminate

def t_schedule(t: int) -> float:
    t_max = 100
    return t_max - t

MAX_ITERATIONS = 50

def t_schedule(t: int) -> float:
    return (MAX_ITERATIONS - t) / MAX_ITERATIONS



optimize_sim = Simulation(field_save_dir="field", t_schedule=t_schedule, stochastic_hc=stochastic_hc_successor, random_search=random_search_successor)
optimize_sim.prepare_complete_state_representation()
field_vis = FieldVisualizer(optimize_sim, canvas_size=600)
ui = IPyUI(field_vis=field_vis, optimize_sim=optimize_sim, max_iterations=MAX_ITERATIONS)
ui.optimization_ui()
```
+6 −2
Original line number Diff line number Diff line
@@ -314,7 +314,7 @@ class Simulation:
        # get t
        t = len(self.cost_history)
        # calculate temperature with function of user
        temperature = self.t_schedule(t)
        temperature = self.t_schedule(t) * 100
        if temperature <= 0:
            return self.state, True
        # choose random successor
@@ -327,10 +327,14 @@ class Simulation:
        cost = self.evaluate_state(state=neighbor_candidate_state)

        delta_e = cost - self.cost_history[-1]

        if delta_e < 0:
            return neighbor_candidate_state, False
        else:
            propability = np.exp(delta_e / temperature)
            propability = np.exp(-delta_e / temperature)
            print(
                f"Keine Verbesserung mit delta_e {delta_e:.2f}, temperature {temperature / 100:.2f} und Annahmewahrscheinlichkeit {propability * 100:.2f}%"
            )
            if random.random() <= propability:
                return neighbor_candidate_state, False
            else:
+2 −3
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
%matplotlib ipympl
from graph_optimization import Simulation, Visualizer, IPyUI

###### Dieser Teil gehört zur optionalen Programmieraufgabe ######
def random_search_successor(current_state: list, neighbor_states: list[list], neighbor_costs: list) -> [list, bool]:
    terminate = True
    return current_state, terminate

def stochastic_hc_successor(current_state: list, neighbor_states: list[list], neighbor_costs: list) -> [list, bool]:
    terminate = True
    return current_state, terminate
###### Teil-Ende ######


###### In diesem Teil könnt ihr Änderungen vornehmen ######
MAX_ITERATIONS = 50
def t_schedule(t: int) -> float:
    t_max = 100
    return t_max - t
    return (MAX_ITERATIONS - t) / MAX_ITERATIONS

MAX_ITERATIONS = 50
MANUAL_STATE_TEST = ["Hof", 0, 1, 2, 3, "Hof", 4, 5, 6, 7]
###### Teil-Ende ######

###### Der restliche Teil startet das Programm ######
optimize_sim = Simulation(graph_load_dir="graph", t_schedule=t_schedule, stochastic_hc=stochastic_hc_successor, random_search=random_search_successor, dummy=True)
print(f"Die manuell eingegebne complete state formulation wird mit {optimize_sim.evaluate_state(MANUAL_STATE_TEST)} bewertet.")
del optimize_sim
optimize_sim = Simulation(graph_load_dir="graph", t_schedule=t_schedule, stochastic_hc=stochastic_hc_successor, random_search=random_search_successor)
field_vis = Visualizer(optimize_sim, canvas_size=550)
ui = IPyUI(field_vis=field_vis, optimize_sim=optimize_sim, max_iterations=MAX_ITERATIONS)
ui.optimization_ui()
```
+5 −2
Original line number Diff line number Diff line
@@ -229,7 +229,7 @@ class Simulation:
        # get t
        t = len(self.cost_history)
        # calculate temperature with function of user
        temperature = self.t_schedule(t)
        temperature = self.t_schedule(t) * 100
        if temperature <= 0:
            return self.state, True
        # choose random successor
@@ -245,7 +245,10 @@ class Simulation:
        if delta_e < 0:
            return neighbor_candidate_state, False
        else:
            propability = np.exp(delta_e / temperature)
            propability = np.exp(-delta_e / temperature)
            print(
                f"Keine Verbesserung mit delta_e {delta_e:.2f}, temperature {temperature / 100:.2f} und Annahmewahrscheinlichkeit {propability * 100:.2f}%"
            )
            if random.random() <= propability:
                return neighbor_candidate_state, False
            else: