Skip to content
Snippets Groups Projects
Commit 873f84bb authored by Lars Wiebach's avatar Lars Wiebach
Browse files

mode

parent cd92c47a
No related branches found
No related tags found
No related merge requests found
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
......@@ -31,15 +31,24 @@ def update_Position_centroid_based(p, c1, c2, c3, global_attractor):
p.position = add_pos_vel(add_pos_vel(d_glob, v_tmp), v_rand)
def cost_function(nodes, permutation):
def cost_function(nodes, permutation, mode = "GEO"):
cost = 0
for i in range(0, len(permutation) - 1):
lat1 = nodes['Breitengrad_DEC'][nodes.index[permutation[i]]]
lon1 = nodes['Laengengrad_DEC'][nodes.index[permutation[i]]]
lat2 = nodes['Breitengrad_DEC'][nodes.index[permutation[i + 1]]]
lon2 = nodes['Laengengrad_DEC'][nodes.index[permutation[i + 1]]]
cost += math.ceil(geo_distance(lat1, lon1, lat2, lon2))
return cost
if(mode=="GEO"):
for i in range(0, len(permutation) - 1):
lat1 = nodes['Breitengrad_DEC'][nodes.index[permutation[i]]]
lon1 = nodes['Laengengrad_DEC'][nodes.index[permutation[i]]]
lat2 = nodes['Breitengrad_DEC'][nodes.index[permutation[i + 1]]]
lon2 = nodes['Laengengrad_DEC'][nodes.index[permutation[i + 1]]]
cost += math.ceil(geo_distance(lat1, lon1, lat2, lon2))
return cost
elif(mode=="EUC_2D"):
for i in range(0, len(permutation) - 1):
lat1 = nodes['Breitengrad_DEC'][nodes.index[permutation[i]]]
lon1 = nodes['Laengengrad_DEC'][nodes.index[permutation[i]]]
lat2 = nodes['Breitengrad_DEC'][nodes.index[permutation[i + 1]]]
lon2 = nodes['Laengengrad_DEC'][nodes.index[permutation[i + 1]]]
cost += math.ceil(euc_distance(lat1, lon1, lat2, lon2))
return cost
def geo_distance(lat1, lon1, lat2, lon2):
......@@ -65,7 +74,7 @@ def geo_distance(lat1, lon1, lat2, lon2):
return (int)(RRR * math.acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0)
def eucl_distance(x1, y1, x2, y2):
def euc_distance(x1, y1, x2, y2):
xd = x1 - x2;
yd = y1 - y2;
dij = nint(math.sqrt(xd * xd + yd * yd))
......@@ -74,15 +83,20 @@ def eucl_distance(x1, y1, x2, y2):
def pre_processing_tsplib(json_file=""):
### Takes a filename of a .json file in tsplib and returns a dataframe
mode = ""
nodes = pd.read_json("json/tsplib/" + json_file + ".json")
solution_tsplib = pd.read_json("json/tsplib/" + json_file + "_tour.json")
if(nodes["edge_weight_type"][0]=="GEO"):
mode = "GEO"
elif(nodes["edge_weight_type"][0]=="EUC_2D"):
mode = "EUC_2D"
known_best_cost = solution_tsplib['tourlength'][0]
best_route = solution_tsplib['tour']
nodes.drop(nodes.columns.difference(['node_coordinates']), 1, inplace=True)
nodes_cleaned = pd.DataFrame(nodes['node_coordinates'].to_list(), columns=['Breitengrad_DEC',
'Laengengrad_DEC'])
return nodes_cleaned, known_best_cost, best_route
return nodes_cleaned, known_best_cost, best_route, mode
def two_opt(df, route):
......
......@@ -5,10 +5,11 @@ import random
class Particle():
def __init__(self, nodes):
def __init__(self, nodes, mode):
n = len(nodes) # Länge der Rundreise
self.mode = mode
self.position = get_random_Position(n) # Initiale Position eines Partikels
self.cost = pl.cost_function(nodes, self.position) # Initiale Kostenberechnung
self.cost = pl.cost_function(nodes, self.position, self.mode) # Initiale Kostenberechnung
self.local_attractor = self.position # Initialer lokaler Attraktor == initiale Position
self.local_attractor_cost = self.cost # Initialer lokaler Attraktor Kosten == initiale Position Kosten
self.velocity = list() # leere Transpositionsliste
......@@ -17,7 +18,7 @@ class Particle():
self.position = add_pos_vel(self.position, self.velocity)
def update_cost(self, nodes): # Kostenfunktion neu berechnen
self.cost = pl.cost_function(nodes, self.position)
self.cost = pl.cost_function(nodes, self.position, self.mode)
def update_velocity(self, c1, c2, c3, globalAttractor): # Update Methode für velocity
self.velocity = add_vel(
......
......@@ -2,9 +2,9 @@ import Particle
class Population():
def __init__(self, num_pop, nodes):
def __init__(self, num_pop, nodes, mode):
self.n = len(nodes)
self.population = list(Particle.Particle(nodes) for i in range(num_pop)) # Erstelle Population der Größe num_pop
self.population = list(Particle.Particle(nodes, mode) for i in range(num_pop)) # Erstelle Population der Größe num_pop
self.best_solution = self.population[0].position # initiale Beste Lösung...
self.best_cost = self.population[0].cost # ...und deren Kosten
self.check_for_best_solution() # berechnet die tatsächlich beste Lösung der Startpopulation
......
......@@ -5,8 +5,8 @@ import numpy as np
import random
def pso_two_opt(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50):
population = pop.Population(num_pop, nodes) # Population erstellen
def pso_two_opt(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50, mode='GEO'):
population = pop.Population(num_pop, nodes, mode) # Population erstellen
not_changed_count = 0
while (not_changed_count < abort):
for p in population.population:
......@@ -23,8 +23,8 @@ def pso_two_opt(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50):
return(twoOpt, twoOptCost)
def pso(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50):
population = pop.Population(num_pop, nodes)
def pso(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50, mode='GEO'):
population = pop.Population(num_pop, nodes, mode)
not_changed_count = 0
while (not_changed_count < abort):
#print(not_changed_count)
......@@ -40,8 +40,8 @@ def pso(num_pop, nodes, c1=1/3, c2=1/3, c3=6/10, abort=50):
#print(population.best_cost)
return population.best_solution, population.best_cost
def var_pso(num_pop, nodes, c1=1 / 3, c2=1 / 3, c3=1 / 3, abort=20):
population = pop.Population(num_pop, nodes)
def var_pso(num_pop, nodes, c1=1 / 3, c2=1 / 3, c3=1 / 3, abort=20, mode='GEO'):
population = pop.Population(num_pop, nodes,mode)
not_changed_count = 0
while (c1 > 0.05):
......
erste_Analyse.PNG

86.6 KiB

......@@ -10,7 +10,7 @@ import PSO_Lib as pslib
import numpy as np
import time
df, opt, opt_route = pslib.pre_processing_tsplib("burma14")
df, opt, opt_route, mode = pslib.pre_processing_tsplib("burma14")
population = 20
abbruch = 15
......@@ -24,7 +24,7 @@ laufzeit = np.array([])
for i in range(0, 1):
start = time.time()
route,kosten = PSO.pso(num_pop=population, nodes=df, c1=9 / 15, c2=3 / 15, c3=3 / 15, abort=abbruch)
route,kosten = PSO.pso(num_pop=population, nodes=df, c1=9 / 15, c2=3 / 15, c3=3 / 15, abort=abbruch, mode=mode)
# perm = pslib.get_random_Position(len(df))
# res = pslib.cost_function(df, pslib.two_opt_with_dist_matrix(distmatrix,perm))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment