reformatted
parent
b2ebfddd8b
commit
b2cfa7297c
|
|
@ -1,8 +1,10 @@
|
||||||
from cyclic_utils import rem_routes
|
from cyclic_utils import rem_routes
|
||||||
|
|
||||||
|
|
||||||
class Route(object):
|
class Route(object):
|
||||||
"""docstring for Route."""
|
"""docstring for Route."""
|
||||||
def __init__(self, route_id, contract_id, load,src,dst):
|
|
||||||
|
def __init__(self, route_id, contract_id, load, src, dst):
|
||||||
super(Route, self).__init__()
|
super(Route, self).__init__()
|
||||||
self.route_id = route_id
|
self.route_id = route_id
|
||||||
self.contract_id = contract_id
|
self.contract_id = contract_id
|
||||||
|
|
@ -10,46 +12,50 @@ class Route(object):
|
||||||
self.src = src
|
self.src = src
|
||||||
self.dst = dst
|
self.dst = dst
|
||||||
|
|
||||||
def __eq__(self,rt):
|
def __eq__(self, rt):
|
||||||
eq = self.route_id == rt.route_id and self.contract_id == rt.contract_id
|
eq = self.route_id == rt.route_id and self.contract_id == rt.contract_id
|
||||||
return eq
|
return eq
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self.contract_id)+"-"+str(self.route_id)+":"+self.src+" - "+self.dst+" "+self.load
|
return str(self.contract_id) + "-" + str(self.route_id) + ":" + self.src + " - " + self.dst + " " + self.load
|
||||||
|
|
||||||
|
|
||||||
class Contract(object):
|
class Contract(object):
|
||||||
"""docstring for Contract."""
|
"""docstring for Contract."""
|
||||||
|
|
||||||
def __init__(self, contract_id, routes):
|
def __init__(self, contract_id, routes):
|
||||||
super(Contract, self).__init__()
|
super(Contract, self).__init__()
|
||||||
self.contract_id = contract_id
|
self.contract_id = contract_id
|
||||||
self.routes = routes
|
self.routes = routes
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "\tContract : "+str(self.contract_id)+"\n\tRoutes\n\t\t"+"\n\t\t".join(map(repr,self.routes))
|
return "\tContract : " + str(self.contract_id) + "\n\tRoutes\n\t\t" + "\n\t\t".join(map(repr, self.routes))
|
||||||
|
|
||||||
|
|
||||||
class Transporter(object):
|
class Transporter(object):
|
||||||
"""docstring for Transporter."""
|
"""docstring for Transporter."""
|
||||||
|
|
||||||
def __init__(self, contracts):
|
def __init__(self, contracts):
|
||||||
super(Transporter, self).__init__()
|
super(Transporter, self).__init__()
|
||||||
self.contracts = contracts
|
self.contracts = contracts
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls,c_file):
|
def from_file(cls, c_file):
|
||||||
return cls(cls.read_contracts(c_file))
|
return cls(cls.read_contracts(c_file))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read_contracts(c_file):
|
def read_contracts(c_file):
|
||||||
c_lines = open(c_file,'r').readlines()
|
c_lines = open(c_file, 'r').readlines()
|
||||||
c_dict = {}
|
c_dict = {}
|
||||||
for l in c_lines:
|
for l in c_lines:
|
||||||
(c_id,src,dst,load) = l.split(' ')
|
(c_id, src, dst, load) = l.split(' ')
|
||||||
if c_dict.has_key(c_id):
|
if c_dict.has_key(c_id):
|
||||||
route_id = len(c_dict[c_id])
|
route_id = len(c_dict[c_id])
|
||||||
c_dict[c_id].append(Route(route_id,int(c_id),load,src,dst))
|
c_dict[c_id].append(Route(route_id, int(c_id), load, src, dst))
|
||||||
else:
|
else:
|
||||||
c_dict[c_id] = [Route(0,int(c_id),load,src,dst)]
|
c_dict[c_id] = [Route(0, int(c_id), load, src, dst)]
|
||||||
contracts = [Contract(int(i),c_dict[i]) for i in sorted(c_dict.keys())]
|
contracts = [Contract(int(i), c_dict[i])
|
||||||
|
for i in sorted(c_dict.keys())]
|
||||||
|
|
||||||
return contracts
|
return contracts
|
||||||
|
|
||||||
|
|
@ -63,9 +69,9 @@ class Transporter(object):
|
||||||
def contracts_required(self):
|
def contracts_required(self):
|
||||||
routes = self.contract_routes()
|
routes = self.contract_routes()
|
||||||
unfulfilled = rem_routes(routes)
|
unfulfilled = rem_routes(routes)
|
||||||
print "No of contracts required : ",len(unfulfilled)
|
print "No of contracts required : ", len(unfulfilled)
|
||||||
if len(unfulfilled) > 0:
|
if len(unfulfilled) > 0:
|
||||||
print "Contracts from ",repr(unfulfilled)
|
print "Contracts from ", repr(unfulfilled)
|
||||||
|
|
||||||
def contract_corridors(self):
|
def contract_corridors(self):
|
||||||
return rem_routes(self.contract_routes())
|
return rem_routes(self.contract_routes())
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,57 @@
|
||||||
|
|
||||||
CYCLE_LIMIT = 3
|
CYCLE_LIMIT = 3
|
||||||
|
|
||||||
|
|
||||||
def rem_routes(routes):
|
def rem_routes(routes):
|
||||||
loop_routes = routes
|
loop_routes = routes
|
||||||
while len(loop_routes)>0:
|
while len(loop_routes) > 0:
|
||||||
circ,paths,loop_routes = largest_loop(loop_routes)
|
circ, paths, loop_routes = largest_loop(loop_routes)
|
||||||
if not circ:
|
if not circ:
|
||||||
break
|
break
|
||||||
partial_paths = []
|
partial_paths = []
|
||||||
partial_routes = loop_routes
|
partial_routes = loop_routes
|
||||||
while len(partial_routes)>0:
|
while len(partial_routes) > 0:
|
||||||
lp = longest_path(partial_routes)
|
lp = longest_path(partial_routes)
|
||||||
partial_paths.append((lp[-1].dst,lp[0].src))
|
partial_paths.append((lp[-1].dst, lp[0].src))
|
||||||
partial_routes = filter(lambda x:x not in lp,partial_routes)
|
partial_routes = filter(lambda x: x not in lp, partial_routes)
|
||||||
return partial_paths
|
return partial_paths
|
||||||
|
|
||||||
|
|
||||||
def route_deps(routes):
|
def route_deps(routes):
|
||||||
routes_map = {}
|
routes_map = {}
|
||||||
for r in routes:
|
for r in routes:
|
||||||
if routes_map.has_key(r.src):
|
if routes_map.has_key(r.src):
|
||||||
routes_map[r.src].append(r)
|
routes_map[r.src].append(r)
|
||||||
else:
|
else:
|
||||||
routes_map[r.src]= [r]
|
routes_map[r.src] = [r]
|
||||||
if not routes_map.has_key(r.dst):
|
if not routes_map.has_key(r.dst):
|
||||||
routes_map[r.dst]= []
|
routes_map[r.dst] = []
|
||||||
return routes_map
|
return routes_map
|
||||||
|
|
||||||
|
|
||||||
def largest_loop(routes):
|
def largest_loop(routes):
|
||||||
deps = route_deps(routes)
|
deps = route_deps(routes)
|
||||||
circ_paths = [find_loop(r.src,r.src,deps,set(),[]) for r in routes]
|
circ_paths = [find_loop(r.src, r.src, deps, set(), []) for r in routes]
|
||||||
circ,paths = max(circ_paths,key=lambda p:len(p[1]))
|
circ, paths = max(circ_paths, key=lambda p: len(p[1]))
|
||||||
# print filter(lambda p:not p[0],circ_paths)
|
# print filter(lambda p:not p[0],circ_paths)
|
||||||
rem_paths = filter(lambda x:x not in paths,routes)
|
rem_paths = filter(lambda x: x not in paths, routes)
|
||||||
return (circ,paths,rem_paths)
|
return (circ, paths, rem_paths)
|
||||||
|
|
||||||
|
|
||||||
def longest_path(routes):
|
def longest_path(routes):
|
||||||
def route_path(r,deps):
|
def route_path(r, deps):
|
||||||
if not deps.has_key(r.dst) or len(deps[r.dst])==0:
|
if not deps.has_key(r.dst) or len(deps[r.dst]) == 0:
|
||||||
return [r]
|
return [r]
|
||||||
all_dep_paths = [([r]+route_path(rt,deps)) for rt in deps[r.dst]]
|
all_dep_paths = [([r] + route_path(rt, deps)) for rt in deps[r.dst]]
|
||||||
rp = max(all_dep_paths,key=lambda p:len(p))
|
rp = max(all_dep_paths, key=lambda p: len(p))
|
||||||
return rp
|
return rp
|
||||||
deps = route_deps(routes)
|
deps = route_deps(routes)
|
||||||
all_paths = [route_path(r,deps) for r in routes]
|
all_paths = [route_path(r, deps) for r in routes]
|
||||||
longest = max(all_paths,key=len)
|
longest = max(all_paths, key=len)
|
||||||
return longest
|
return longest
|
||||||
|
|
||||||
def find_loop(elem,dep_elem,deps,checked,path):
|
|
||||||
|
def find_loop(elem, dep_elem, deps, checked, path):
|
||||||
found_loop = False
|
found_loop = False
|
||||||
if dep_elem not in checked and deps.has_key(dep_elem):
|
if dep_elem not in checked and deps.has_key(dep_elem):
|
||||||
checked.add(dep_elem)
|
checked.add(dep_elem)
|
||||||
|
|
@ -55,10 +60,10 @@ def find_loop(elem,dep_elem,deps,checked,path):
|
||||||
if elem == c.dst and len(path) <= CYCLE_LIMIT:
|
if elem == c.dst and len(path) <= CYCLE_LIMIT:
|
||||||
found_loop = True
|
found_loop = True
|
||||||
break
|
break
|
||||||
(loop,path) = find_loop(elem,c.dst,deps,checked,path)
|
(loop, path) = find_loop(elem, c.dst, deps, checked, path)
|
||||||
if loop:
|
if loop:
|
||||||
found_loop = True
|
found_loop = True
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
del(path[-1])
|
del(path[-1])
|
||||||
return (found_loop,path)
|
return (found_loop, path)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue