-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7735b6
commit 6c4935c
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |