From 6c4935c9c2144d4a07515322b176a6970f9575c4 Mon Sep 17 00:00:00 2001 From: ctincorvia Date: Thu, 8 Dec 2016 16:10:10 -0600 Subject: [PATCH] Clingo implementatino of TSP --- clingo/README | 29 ++++++++++++++++++++++ clingo/tsp.lp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 clingo/README create mode 100644 clingo/tsp.lp diff --git a/clingo/README b/clingo/README new file mode 100644 index 0000000..284d335 --- /dev/null +++ b/clingo/README @@ -0,0 +1,29 @@ +Charles Incorvia (Ci2923) + +The traveling salesman problem implemented in Clingo. + +Clingo is a declarative programming languaged descened from Prolog. + +The clingo implementation is declarative, and thus not exactly the greedy algorithm, but in the background gringo is using greedy algorithms. + +In order to run this program, clingo 4.5.4 or later is required. + +It's available free here : http://potassco.sourceforge.net/ + +The command to run the program is: + +clingo tsp.lp + +Clingo does not take inputs from I/O, so all of the information regarding the traveling salesman problem is within the file. + +The path() terms indicate the paths from city to city available to the salesman. +The place() terms are the cities and their names. +The const t is the number of cities the salesman is planning to visit. +The first location variable indicates the salesman's starting city. + +The program will output UNSATISFIABLE if there's no way to visit all of the cities and return to the home city without visiting any cities twice. + +Otherwise, it will output SATISFIABLE and list the steps to the smallest possible path in the form of: + +travel(City, Time). + diff --git a/clingo/tsp.lp b/clingo/tsp.lp new file mode 100644 index 0000000..d6be447 --- /dev/null +++ b/clingo/tsp.lp @@ -0,0 +1,67 @@ +% THE TRAVELING SALESMAN + +% All of the paths from city to city +path(austin, houston, 238; houston, dallas, 163; dallas, austin, 212). + +%Paths are bidirectional +path(City1, City2) :- path(City2, City1). + +% A list of all of the cities +place(austin; houston; dallas). + +% number of cities on tour +#const t=3. + +%start location +location(austin, 0). + +% The salesman must end in his home city +:- not location(City, t), location(City, 0). + + + + + + + +% choose where to travel at each timestep +% there are exactly as many cities as there are timesteps +{ travel(City, Time) : place(City) } 1 :- Time = 0..t-1. + +%if the salesman travels TO somewhere, he's already been there +%this way, we can travel to our start city at the end +visited(City, Time) :- travel(City, Time). + +%we can't travel to a city we've been to before +:- travel(City, Time1), visited(City, Time2), Time1 > Time2. + +%you can't go anywhere there is not a path to +:- travel(City_end, Time), location(City_start, Time), not path(City_start, City_end, _). + +%if you travel from somewhere too somewhere, distance happens +distance(D) :- travel(City_end, Time), location(City_start, Time), path(City_start, City_end, D). + + +%can't travel to where you already are +:- travel(City, Time), location(City, Time). + +%Traveling gets you places +location(City, Time + 1) :- travel(City, Time). + + +%uniqueness of location +-location(City2, T) :- location(City1, T), place(City2), City1 != City2. + +%commonsense intertia +location(City, T + 1) :- location(City, T), not -location(City, T + 1), place(City), T = 0..t-1. + + +% every city must be visited by the end +:- place(City), not visited(City, _). + +% tell clingo to keep trying until it fins a solution with optimal distance. +total(N) :- N = #sum{L : distance(L)}. +#minimize{N : total(N)}. + +#show total/1. +#show travel/2. \ No newline at end of file