Skip to content

Commit

Permalink
Clingo implementatino of TSP
Browse files Browse the repository at this point in the history
  • Loading branch information
ctincorvia committed Dec 8, 2016
1 parent b7735b6 commit 6c4935c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions clingo/README
Original file line number Diff line number Diff line change
@@ -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).

67 changes: 67 additions & 0 deletions clingo/tsp.lp
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 6c4935c

Please sign in to comment.