unit main; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Math, ExtCtrls; const max_stad = 288; max_loop = 1000; tau_0 = 0.000001; alfa = 1; beta = 5; e = 5; rho = 0.5; Q = 100; type TTSPForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Label1: TLabel; Memo1: TMemo; OpenDialog1: TOpenDialog; Image1: TImage; procedure Button3Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; TPunt = record x,y: integer; end; Ttour = record t:array [1..max_stad] of integer; length: 0..max_stad; bl:array [1..max_stad] of boolean; //blacklist. bezocht = true end; var TSPForm1: TTSPForm1; map: array [1..max_stad] of TPunt; edges: array [1..max_stad, 1..max_stad] of real; best: integer; best_tour:Ttour; ant_pos: array [1..max_stad] of 1..max_stad; //ant_pos[i] => ant_i is in city map[ant_pos[i]] globstop:boolean; aantal_stad:1..max_stad; max_ants:integer; distarray: array [1..max_stad, 1..max_stad] of integer; function dist(a,b:integer):integer; function dist2(a,b:TPunt):integer; procedure draw_tour(tour:TTour); implementation {$R *.DFM} procedure TTSPForm1.Button3Click(Sender: TObject); var i,j,aantal:integer; DataFile: textfile; begin best := MaxInt; globstop:=false; //maak map {for s:=1 to max_stad do begin map[s].x := random(gridsize); map[s].y := random(gridsize); end;} if OpenDialog1.Execute then {$I-} AssignFile(datafile,OpenDialog1.FileName); Reset(datafile); if IOResult = 0 then begin {file bestaat} {$I+} ReadLn(datafile,aantal_stad); //aantal dagen dat volgt for i:=1 to aantal_stad do begin ReadLn(datafile,map[i].x,map[i].y); end; end; CloseFile(dataFile); max_ants := aantal_stad; //edges init for i:= 1 to aantal_stad do for j:=1 to aantal_stad do edges[i,j]:= tau_0; //place ants for i:=1 to max_ants do ant_pos[i] := i; //not randomly, but 1 ant on every city! //map misschien afdrukken for i:= 1 to aantal_stad do begin memo1.Lines.add(IntToStr(i)+': '+IntToStr(map[i].x)+' , '+IntToStr(map[i].y)); end; //dist_array init for i:= 1 to aantal_stad do for j:=1 to aantal_stad do distarray[i,j]:= dist2(map[i],map[j]); end; function dist(a,b:integer):integer; begin dist :=distarray[a,b]; end; function dist2(a,b:TPunt):integer; begin //dist := abs(a.x - b.x) + abs(a.y - b.y); dist2 :=round(sqrt( abs(a.x - b.x)*abs(a.x - b.x) + abs(a.y - b.y)*abs(a.y - b.y))); end; function tourToString(t:ttour):string; var i:integer; s:string; begin for i:= 1 to t.length do s:= s+' '+IntToStr(t.t[i]); tourToString := s; end; function tourLength(t:Ttour):integer; var i,som:integer; begin som:=0; for i:= 1 to aantal_stad do begin if (i maxx then maxx := map[i].x; if map[i].y > maxy then maxy := map[i].y; end; pict.Pen.Color:=clRed; if maxx > maxy then factor := tspform1.image1.width / maxx else factor := tspform1.image1.width / maxy; for i:= 1 to tour.length do pict.Ellipse(floor(factor*map[i].x),floor(factor*map[i].y),floor(factor*map[i].x)+5,floor(factor*map[i].y+1)+5); pict.Pen.Color:=clBlue; pict.MoveTo( floor(factor*map[tour.t[1]].x), floor(factor*map[tour.t[1]].y) ); for i:= 2 to tour.length do begin pict.LineTo( floor(factor*map[tour.t[i]].x), floor(factor*map[tour.t[i]].y) ); end; end; end.