Commit ee399898 authored by Gaetan D's avatar Gaetan D
Browse files

Merge branch 'master' of gitlab.crans.org:alopez/projet-optimisation

parents 06424c41 2c01edde
......@@ -21,46 +21,46 @@ class Explicit:
class Euc_2D:
def __init__(self, x = None, y = None, number = None, number = None):
def __init__(self, x = None, y = None, number = None):
self.x = x
self.y = y
self.number = number
def dist(self, other):
assert (type(other)==Euc_2D)
xd = self.x - other.x
yd = self.y - other.y
return sqrt(xd**2 + yd**2)
def distance(self, other):
assert (type(other)==Euc_2D)
xd = self.x - other.x
yd = self.y - other.y
return sqrt(xd**2 + yd**2)
class Euc_3D:
def __init__(self, x = None, y = None, z = None, number = None):
self.x = x
self.y = y
self.z = z
self.number = number
def dist(self, other):
assert (type(other)==Euc_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return sqrt(xd**2 + yd**2 + zd**2)
def distance(self, other):
assert (type(other)==Euc_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return sqrt(xd**2 + yd**2 + zd**2)
class Max_2D:
def __init__(self, x = None, y = None, number = None):
self.x = x
self.y = y
self.number = number
def dist(self, other):
assert (type(other)==Max_2D)
xd = self.x - other.x
yd = self.y - other.y
return max(round(abs(xd)), round(abs(yd)))
def distance(self, other):
assert (type(other)==Max_2D)
xd = self.x - other.x
yd = self.y - other.y
return max(round(abs(xd)), round(abs(yd)))
class Max_3D:
def __init__(self, x = None, y = None, z = None, number = None):
self.x = x
......@@ -68,26 +68,26 @@ class Max_3D:
self.z = z
self.number = number
def dist(self, other):
assert (type(other)==Max_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return max(round(abs(xd)), max(round(abs(yd))), round(abs(zd))))
def distance(self, other):
assert (type(other)==Max_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return max(round(abs(xd)), round(abs(yd)), round(abs(zd)))
class Man_2D:
def __init__(self, x = None, y = None, number = None):
self.x = x
self.y = y
self.number = number
def dist(self, other):
assert (type(other)==Man_2D)
xd = self.x - other.x
yd = self.y - other.y
return round(abs(xd) + abs(yd))
def distance(self, other):
assert (type(other)==Man_2D)
xd = self.x - other.x
yd = self.y - other.y
return round(abs(xd) + abs(yd))
class Man_3D:
def __init__(self, x = None, y = None, z = None, number = None):
......@@ -96,17 +96,17 @@ class Man_3D:
self.z = z
self.number = number
def dist(self, other):
assert (type(other)==Man_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return round(abs(xd) + abs(yd) + abs(zd))
def distance(self, other):
assert (type(other)==Man_3D)
xd = self.x - other.x
yd = self.y - other.y
zd = self.z - other.z
return round(abs(xd) + abs(yd) + abs(zd))
class GeoCoord:
def __init__(self, degrees = None, minutes = None, number = None):
self.number = number
self.number = number
if degrees < 0:
(self.degrees, self.minutes) = (degrees, -1 * minutes)
else:
......@@ -114,7 +114,7 @@ class GeoCoord:
def toRadians(self):
return (self.degrees + self.minutes / 60) * pi / 180.0
class GeoCity:
def __init__(self, lat = None, lon = None, number = None):
......@@ -122,24 +122,19 @@ class GeoCity:
self.lon = lon.toRadians()
self.number = number
def dist(self, other):
assert(type(other)==GeoCity)
lat1, lon1 = self.lat, self.lon
lat2, lon2 = other.lat, other.lon
if lat1 == lat2 and lon1 == lon2:
return 0
else:
q1 = cos( lon1 - lon2 )
q2 = cos( lat1 - lat2 )
q3 = cos( lat1 + lat2 )
radius = 6378.388
# distance formula from the documentation
distance = radius * acos (1/2 * ((1 + q1) * q2 - (1 - q1) * q3)) + 1
return(int(distance)) # truncate (toward zero)
def distance(town1,town2):
return town1.dist(town2)
def distance(self, other):
assert(type(other)==GeoCity)
lat1, lon1 = self.lat, self.lon
lat2, lon2 = other.lat, other.lon
if lat1 == lat2 and lon1 == lon2:
return 0
else:
q1 = cos( lon1 - lon2 )
q2 = cos( lat1 - lat2 )
q3 = cos( lat1 + lat2 )
radius = 6378.388
# distance formula from the documentation
distance = radius * acos (1/2 * ((1 + q1) * q2 - (1 - q1) * q3)) + 1
return(int(distance)) # truncate (toward zero)
......@@ -17,7 +17,7 @@ class Graph:
en fonction des paramètres
de création de l'objet
"""
if self.weights = None:
if self.weights == None:
return city1.distance (city2)
else:
return weights[city1.number][city2.number]
......
......@@ -3,22 +3,20 @@
# Lopez Aliaume
from parser import read_tsp_file
from city import distance
import city
import graph
from itertools import permutations
probleme = read_tsp_file ("problemes/a280.tsp")
def chemins (prob):
l = prob["CITIES"]
def chemins (graph):
l = graph.cities
return permutations (l)
def cout_chemin (chemin):
return sum (map (lambda x: distance(x[0], x[1]), zip (chemin, chemin[1:])))
def algorithme_naif (prob):
return min (chemins (prob), key = cout_chemin)
def algorithme_naif (graph):
return min (chemins (graph), key = graph.cout_chemin)
print ("SOL : {}".format (algorithme_naif (probleme)))
print ("SOL : {}".format (algorithme_naif (probleme["GRAPH"])))
from collections import deque
from city import GeoCoord, GeoCity, Euc_2D
from graph import Graph
def minimal_tsp():
return { "COMMENT" : ""
, "DIMENSION" : None
, "TYPE" : None
, "EDGE_WEIGHT_TYPE" : None
, "CITIES" : []
, "MATRIX" : None }
, "GRAPH" : None
}
def scan_keywords(tsp,tspfile):
for line in tspfile:
......@@ -58,21 +59,46 @@ def read_numbered_euc_2d_city_line(desired_number, words):
else:
print("Missing or mislabeld city: expected {0}".format(desired_number))
def read_matrix (tsp, tspfile):
pass
def read_cities(tsp,tspfile):
cities = []
for n in range(1, tsp["DIMENSION"] + 1):
line = tspfile.readline()
words = deque(line.split()) # list of the words from the line
line = tspfile.readline()
words = deque(line.split()) # list of the words from the line
if tsp["EDGE_WEIGHT_TYPE"] == "EUC_2D":
tsp["CITIES"].append(read_numbered_euc_2d_city_line(n, words))
cities.append(read_numbered_euc_2d_city_line(n, words))
elif tsp["EDGE_WEIGHT_TYPE"] == "GEO":
tsp["CITIES"].append(read_numbered_geo_city_line(n, words))
cities.append(read_numbered_geo_city_line(n, words))
else:
print("Unsupported coordinate type: " + tsp["EDGE_WEIGHT_TYPE"])
return cities
def read_tsp_file(path):
tspfile = open(path,'r')
tsp = minimal_tsp()
scan_keywords(tsp,tspfile)
read_cities(tsp,tspfile)
if tsp["EDGE_WEIGHT_TYPE"] == "EXPLICIT":
matrix = read_matrix (tsp,tspfile)
else:
matrix = None
cities = read_cities(tsp,tspfile)
g = Graph (cities, matrix)
tsp["GRAPH"] = g
tspfile.close()
return tsp
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment