[solved]how to search for via routes when there are no direc

Get help with using AutoHotkey and its commands and hotkeys
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 09:56

Works!
doing more testing now. :)
Thanks @Alphabravo

Code: Select all

data =
(
A   B   7
A   C   9
A   F   14
B   C   10
B   D   15
C   D   11
C   F   2
D   E   6
E   F   9
)
; --- mask following for simple data above
gosub, setup
loop, parse, data, `n, `r
    x := StrSplit(A_LoopField, "`t"), total := (x.4?x.4:0) + (x.6?x.6:0), res .= x.2 "`t" x.3 "`t" total "`n" 
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios
; --- end mask

nodes:=[], Distance := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:
Gui, add, DDL, xs vFrom gSubmit, % nodeList
Gui, add, DDL, x200 yp vTo gSubmit, % nodeList
Gui, add, Edit, xs w340 r10
Gui, show

Submit:
Gui, Submit, NoHide
res := Dijkstra(data, From)
Route := Done := "" , origin := to

if Distance[From, To]       ; direct flight
{
    route := From "`t>`t" To "`t" Distance[xFrom , xTo] "`n" 
    GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
    return
}

while (!Done)
    for xTo, xFrom in res.2
    {
        if (xTo <> To)
            continue
        else if !(xFrom)
            done := true
        else
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,   To := xFrom
    }
GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin]
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;-----------------------------------------------

setup:
data =
(
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return
 
John ... you working ?
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 09:58

error
http://gyazo.com/3aaa367e6eeb0c455af7f3be0ce68478

it is supposed to check both Delhi to Dehradun and Dehradun to Delhi ( the numbers for both are the same, i.e. if either one of them is there it can be used for reverse trip.

thanks
Attachments
Capture.PNG
Capture.PNG (6.26 KiB) Viewed 3340 times
John ... you working ?
User avatar
AlphaBravo
Posts: 391
Joined: 29 Sep 2013, 22:59

Re: how to search for via routes when there are no direct ro

10 Apr 2015, 11:22

error
http://gyazo.com/3aaa367e6eeb0c455af7f3be0ce68478

it is supposed to check both Delhi to Dehradun and Dehradun to Delhi ( the numbers for both are the same, i.e. if either one of them is there it can be used for reverse trip.

thanks
there is only one way to get to "Dehradun" which via "Delhi" and there is no path from "Delhi" to "Dehli"
not sure if "Dehli" is a typo!
Dehradun Delhi 5221 13980 1850
updated code:

Code: Select all

gosub, setup
data := RegExReplace(data, "\t\t", "`t0`t")		; replace empty fields with 0
loop, parse, data, `n, `r
	x := StrSplit(A_LoopField, "`t"), total := x.4 + x.6,		res .= x.2 "`t" x.3 "`t" total "`n" 
data := RegExReplace( res, "`am)^\D+$\R")		; remove lines with no connectios

nodes:=[], Distance := []
for each, line in StrSplit(data, "`n" , "`r")
	field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
	, Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
	nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:
Gui, add, DDL, xs vFrom gSubmit, % nodeList
Gui, add, DDL, x200 yp vTo gSubmit, % nodeList
Gui, add, Edit, xs w340 r40
Gui, +AlwaysOnTop
Gui, show

Submit:
Gui, Submit, NoHide
GuiControl, , Edit1, % ""
if !(From && To) || (From = To)
	return

res := Dijkstra(data, From)
Route := Done := xTo := xFrom := DirectFlight := "" , origin := to

if Distance[From, To]		; direct flight
	DirectFlight := "`n`nDirect Flight`n" From "`t>`t" To "`t" Distance[xFrom , xTo] "`t" Distance[From, To] "`n" 

if !res[1, To]				; no possible route
{
	GuiControl, , Edit1, no routing found
	return
}

Routing:
Loop % objCount(nodes)
	for xTo , xFrom in res.2
	{
		if (xTo = To)
		{
			route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`n" route,	To := xFrom
			if (xFrom = From)
				break, Routing
		}
	}

GuiControl, , Edit1, % "From`t>`tTo`tLength`n" . route . "`nTotal distance = " res.1[origin] . DirectFlight
return
functions are the same!
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

10 Apr 2015, 12:16

@AlphaBravo

thanks.

i pray this be the last. btw
:)

will do some testing.. and yes Dehli and Delhi are two different places..
John ... you working ?
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

10 Apr 2015, 13:52

Here is my new version (use "download" from this topic to keep the `t and not "select all" and ^v):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
setbatchlines, -1

test := ""
list_of_places := ""
routes := ""

gosub, set_var	; for better editing of source code

array_from_places := {}
array := strsplit(var, "`n")

loop, % array.maxindex()
{
	lines := array[A_index]
	array_ := strsplit(lines, "`t")	
	array_from_places[array_[2], array_[3]] := [array_[4], array_[5] = "" ? 0 : array_[5], array_[6]]
	list_of_places .= array_[2] . "|"
	list_of_places .= array_[3] . "|"
} 
stringtrimright, list_of_places, list_of_places, 1
sort, list_of_places, D| U

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gButtonOK, %list_of_places%  ; The ym option starts a new column of controls.
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gButtonOK, %list_of_places% 
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
guicontrol,, result, searching
found = 1
gosub, search_subroutine
if (test = "no connection found")
{
	switch(from, to)
	found = 2
	gosub, search_subroutine
}
GuiControl, enable, New
GuiControl,, New, start search
GuiControl,, Result, % test
return	

search_subroutine:
	routes =
	search(from, to)
	stringtrimright, routes, routes, 1
	cheap = 99999999
	cheapest =
	low = 999
	lowest =
	loop, parse, routes, `n
	{
		total = 0
		test =
		r := strsplit(a_loopfield, ",")
		for k, v in r
		{
			if (k = r.maxindex())
				break
			from := v
			to := r[k + 1]
			hltc := array_from_places[from, to, 1]
			dltc := array_from_places[from, to, 2]
			afc := array_from_places[from, to , 3]
			total += hltc
			total += afc
			test .= from " " to " " hltc " " dltc " " afc "`tTotal: " total "`n"
		}
		total2 = 0
		test2 =
		for k, v in r
		{
			if (a_index = r.maxindex())
				break
			from := r[r.maxindex() - k]
			to := r[r.maxindex() - k + 1]
			hltc := array_from_places[from, to, 1]
			dltc := array_from_places[from, to, 2]
			afc := array_from_places[from, to , 3]
			total2 += hltc
			total2 += afc
			test2 .= to " " from " " hltc " " dltc " " afc "`tTotal: " total2 "`n"
		}
		if (total < cheap)
		{
			cheap := total
			cheapest := test
			cheapest2 := test2
			if (r.maxindex() <= low)
			{
				low := r.maxindex()
				lowest := test
				lowest2 := test2
			}
		}
		if (r.maxindex() < low)
		{
			low := r.maxindex()
			lowest := test
			lowest2 := test2
		}
	}
	if !routes
		test = no connection found
	else if (found = 1)
		test := "cheapest:`n" cheapest "`n" "lowest:`n" lowest
			. "`nreverse cheapest:`n" cheapest2 "`nreverse lowest:`n" lowest2
	else
		test := "cheapest:`n" cheapest2 "`n" "lowest:`n" lowest2
			. "`nreverse cheapest:`n" cheapest "`nreverse lowest:`n" lowest
return

search(from, to, route := "")
{
	global array_from_places, routes
	if !from
		return
	route .= from ","
	for key, value in array_from_places[from]
	{
		if !key
			return
		if key in %route%
			continue
		if (key = to)
			routes .= route to "`n"
		else
			search(key, to, route)
	}
return
}

switch(byref x, byref y)
{
	z := x
	x := y
	y := z
}

set_var:
; separator = tab
var =
( C
; for testing purposes
0	a	b	1	1	1
0	a	c	1	1	1
0	a	g	20	0	20
0	b	c	2	2	2
0	b	g	8	8	8
0	c	d	3	3	3
0	c	g	1	1	1
0	c	a	1	1	1
0	d	e	4	4	4
0	e	f	5	5	5
0	f	g	6	6	6
;
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return

esc::ExitApp
Hubert
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

10 Apr 2015, 21:15

@Hubert
Works :)

another ( seems to be the last thing ) requirement, if the original var does not have route like Guwahati to Agartala but instead has Agartala to Guwahati can the code show empty OR null or no routes from Guwahati to Agartala but shows route from Agartala to Guwahati?

Thanks
John ... you working ?
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 05:14

@smorgasbord: you mean, the reverse route is only put into the comboboxes, but not into the result?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
setbatchlines, -1

test := list_of_places := routes := ""

gosub, set_var	; for better editing of source code

array_from_places := {}
array := strsplit(var, "`n")

loop, % array.maxindex()
{
	lines := array[A_index]
	array_ := strsplit(lines, "`t")	
	array_from_places[array_[2], array_[3]] := [array_[4], array_[5] = "" ? 0 : array_[5], array_[6]]
	list_of_places .= array_[2] . "|"
	list_of_places .= array_[3] . "|"
} 
stringtrimright, list_of_places, list_of_places, 1
sort, list_of_places, D| U

Gui, Add, Text,, From:
Gui, Add, ComboBox, vFrom ym gButtonOK, %list_of_places%  ; The ym option starts a new column of controls.
Gui, Add, Text, ym, To:
Gui, Add, ComboBox, vto ym gButtonOK, %list_of_places% 
Gui, Add, Text, xm+30 vResult w1000 h300
Gui, Show,, Simple Input Example
return  ; End of auto-execute section. The script is idle until the user does something.

GuiClose:
ButtonOK:
Gui, Submit, nohide  ; Save the input from the user to each control's associated variable.
guicontrol,, result, searching

cheapest := lowest := used := ""
gosub, search_subroutine
test1 := test

switch(from, to)
cheapest := lowest := used := ""
gosub, search_subroutine
test2 := test

test := test1 "`nreverse:`n`n" test2
GuiControl, enable, New
GuiControl,, New, start search
GuiControl,, Result, % test
return	

search_subroutine:
	routes =
	search(from, to)
	stringtrimright, routes, routes, 1
	cheap = 99999999
	cheapest =
	low = 999
	lowest =
	loop, parse, routes, `n
	{
		total = 0
		test =
		r := strsplit(a_loopfield, ",")
		for k, v in r
		{
			if (k = r.maxindex())
				break
			sfrom := v
			sto := r[k + 1]
			hltc := array_from_places[sfrom, sto, 1]
			dltc := array_from_places[sfrom, sto, 2]
			afc := array_from_places[sfrom, sto , 3]
			total += hltc
			total += afc
			test .= sfrom " " sto " " hltc " " dltc " " afc "`tTotal: " total "`n"
		}
		if (total < cheap)
		{
			cheap := total
			cheapest := test
			if (r.maxindex() <= low)
			{
				low := r.maxindex()
				lowest := test
			}
		}
		if (r.maxindex() < low)
		{
			low := r.maxindex()
			lowest := test
		}
	}
	if !routes
		test = no connection found`n
	else
		test := "cheapest:`n" cheapest "`n" "lowest:`n" lowest
return

search(from, to, route := "")
{
	global array_from_places, routes, used
	if !from or !to or (from = to)
		return
	route .= from ","
	for key, value in array_from_places[from]
	{
		if key in %route%
			continue
		if (key = to)
			routes .= route to "`n"
		else
			search(key, to, route)
	}
return
}

switch(byref x, byref y)
{
	z := x
	x := y
	y := z
}

set_var:
; separator = tab
var =
( C
; for testing purposes
0	a	b	1	1	1
0	a	c	1	1	1
0	a	g	20	0	20
0	b	c	2	2	2
0	b	g	8	8	8
0	c	d	3	3	3
0	c	g	1	1	1
0	c	a	1	1	1
0	d	e	4	4	4
0	e	f	5	5	5
0	f	g	6	6	6
;
1	Agartala	Guwahati	4831	10140	1850
2	Agartala	Kolkata	3891	8506	1850
3	Agartala	Silchar	4401		1850
4	Agatti	Bengaluru	10691		1850
5	Agatti	Chennai	10646		2150
6	Agatti	Kochi	8571		1850
7	Agatti	Kozhikode	7471		1850
8	Agra	Delhi	5681	15327	1850
9	Agra	Khajuraho	3891	8506	1850
10	Agra	Varanasi	4946	12951	1850
11	Agra	Mumbai	8571		2950
12	Ahmedabad	Chennai	7426	26186	2950
13	Ahmedabad	Delhi	6000	15502	2150
14	Ahmedabad	Hyderabad	7076	19532	2150
15	Ahmedabad	Mumbai	4351	13425	1850
16	Aizawl	Guwahati	5351		1850
17	Aizawl	Imphal	4466	7337	1850
18	Aizawl	Kolkata	4626	11108	1850
19	Allahabad	Delhi	6541		1850
20	Allahabad	Kanpur	4751		1850
21	Allahabad	Mumbai	9401		2950
22	Amritsar	Delhi	4366	12436	1850
23	Aurangabad	Delhi	8801	21747	2150
24	Aurangabad	Mumbai	4451	10133	1850
25	Bagdogra	Delhi	9716	21024	2950
26	Bagdogra	Guwahati	4536	8398	1850
27	Bagdogra	Kolkata	5436	13188	1850
28	Bengaluru	Chennai	4701	10691	1850
29	Bengaluru	Delhi	9901	28985	3650
30	Bengaluru	Goa	5151	13297	1850
31	Bengaluru	Hyderabad	5451	14823	1850
32	Bengaluru	Kochi	4501	10907	1850
33	Bengaluru	Kolkata	9701	30799	3650
34	Bengaluru	Mangalore	4936		1850
35	Bengaluru	Mumbai	6851	16356	2150
36	Bengaluru	Pune	6184	16506	1850
37	Bengaluru	Tirupati	5221		1850
38	Bengaluru	Trivandrum	5451	12473	1850
39	Bhopal	Delhi	4801	14856	1850
40	Bhopal	Indore	4281	8625	1850
41	Bhopal	Mumbai	5356	15560	1850
42	Bhubaneshwar	Chennai	8291	17520	2950
43	Bhubaneshwar	Delhi	9001	27424	2950
44	Bhubaneshwar	Kolkata	5406	11764	1850
45	Bhubaneshwar	Mumbai	9401	29518	2950
46	Bhubaneshwar	Port Blair	11216		2950
47	Bhuj	Mumbai	6541		1850
48	Chandigarh	Delhi	4301	10534	1850
49	Chandigarh	Mumbai	8836	26295	2950
50	Chennai	Coimbatore	4251	12166	1850
51	Chennai	Delhi	8456	28474	3650
52	Chennai	Goa	6311	16217	1850
53	Chennai	Hyderabad	4451	13732	1850
54	Chennai	Kochi	5251	14630	1850
55	Chennai	Kolkata	8566	27543	2950
56	Chennai	Madurai	4251	11801	1850
57	Chennai	Mumbai	8571	18623	2950
58	Chennai	Pune	7851	21017	2150
59	Chennai	Portblair	9411	24229	2950
60	Chennai	Trivandrum	5601	14637	1850
61	Chennai	Vishakhapatnam	4801	14673	1850
62	Coimbatore	Delhi	9751	32763	3650
63	Coimbatore	Kozhikode	4451	6739	1850
64	Coimbatore	Mumbai	8001	18331	2950
65	Dehradun	Delhi	5221	13980	1850
66	Dehli	Dharamsala	4821		1850
67	Delhi	Gaya	6851	19532	2150
68	Delhi	Goa	8821	25860	3650
69	Delhi	Guwahati	9811	24988	3650
70	Delhi	Gwalior	5051	9977	1850
71	Delhi	Hyderabad	8401	25748	2950
72	Delhi	Imphal	9081	27248	3650
73	Delhi	Indore	4851	15867	1850
74	Delhi	Jabalpur	6401		1850
75	Delhi	Jaipur	3551	8815	1850
76	Delhi	Jammu	4401	13181	1850
77	Delhi	Jodhpur	5706	14290	1850
78	Delhi	Kanpur	5701		1850
79	Delhi	Khajuraho	5651	15108	1850
80	Delhi	Kochi	12351	37731	3650
81	Delhi	Kolkata	9061	24251	2950
82	Delhi	Kozhikode	10051	32763	3650
83	Delhi	Kullu	6301		1850
84	Delhi	Leh	5501	15141	1850
85	Delhi	Lucknow	4821	12505	1850
86	Delhi	Ludhiana	4351		1850
87	Delhi	Mangalore	9901	29248	3650
88	Delhi	Mumbai	8951	22740	2950
89	Delhi	Pantnagar	4301		1850
90	Delhi	Nagpur	7171	17336	2150
91	Delhi	Pathankot	5101		1850
92	Delhi	Patna	7151	17265	2150
93	Delhi	Port Blair	21516		3650
94	Delhi	Pune	9401	28208	2950
95	Delhi	Raipur	7851	22112	2150
96	Delhi	Rajkot	9101		2150
97	Delhi	Ranchi	8811	20732	2950
98	Delhi	Srinagar	6201	13370	1850
99	Delhi	Surat	9101	20819	2150
100	Delhi	Tirupati	9016	23756	3650
101	Delhi	Trivandrum	12156	37731	3650
102	Delhi	Udaipur	5786	15382	1850
103	Delhi	Vadodra	7051	19853	2150
104	Delhi	Varanasi	5681	15327	1850
105	Delhi	Vijayawada	8566	26202	2950
106	Delhi	Vishakhapatnam	10401	30218	2950
107	Dibrugarh	Dimapur	3251	5106	1850
108	Dibrugarh	Guwahati	4801		1850
109	Dibrugarh	Kolkata	7401	14776	2150
110	Dibrugarh	Lilabari	4051		1850
111	Dimapur	Guwahati	4701		1850
112	Dimapur	Imphal	4401		1850
113	Dimapur	Kolkata	6101	13822	1850
114	Dimapur	Shillong	4101		1850
115	Gaya	Kolkata	4501	11744	1850
116	Gaya	Varanasi	4851	9518	1850
117	Goa	Kochi	5001	15159	1850
118	Goa	Hyderabad	5251	13657	1850
119	Goa	Mumbai	5321	11232	1850
120	Goa	Pune	4536	8874	1850
121	Goa	Srinagar	12351	38431	3650
122	Guwahati	Imphal	4901	9498	1850
123	Guwahati	Jorhat	3736		1850
124	Guwahati	Kolkata	5076	11465	1850
125	Guwahati	Lilabari	5151		1850
126	Guwahati	Silchar	5251		1850
127	Guwahati	Tezpur	4436		1850
128	Gwalior	Mumbai	8401	19551	2150
129	Hyderabad	Kolkata	9696	24985	2950
130	Hyderabad	Mumbai	5251	14980	1850
131	Hyderabad	Pune	5231	14265	1850
132	Hyderabad	Tirupati	4656	12571	1850
133	Hyderabad	Varanasi	8811	21806	2950
134	Hyderabad	Vijayawada	5051	10655	1850
135	Hyderabad	Vishakhapatnam	4946	12951	1850
136	Imphal	Kolkata	4281	11680	1850
137	Imphal	Silchar	4601		1850
138	Indore	Mumbai	4481	12637	1850
139	Jaipur	Mumbai	7851	18794	2150
140	Jammu	Leh	4886	9069	1850
141	Jammu	Srinagar	4403	6998	1850
142	Jamnagar	Mumbai	5181	12400	1850
143	Jodhpur	Mumbai	7686	18670	2150
144	Jodhpur	Udaipur	4231	8724	1850
145	Jorhat	Kolkata	4976		1850
146	Jorhat	Tezpur	4136		1850
147	Kanpur	Kolkata	7401		2150
148	Khajuraho	Varanasi	4936	12681	1850
149	Kochi	Kozhikode	3501	7283	1850
150	Kochi	Madurai	4301		1850
151	Kochi	Mumbai	8401	21634	2950
152	Kochi	Trivandrum	4301	8326	1850
153	Kolkata	Kochi	10051		3650
154	Kolkata	Lilabari	7800		2150
155	Kolkata	Mumbai	8486	23558	3650
156	Kolkata	Patna	5706		1850
157	Kolkata	Port Blair	11071	26781	2950
158	Kolkata	Ranchi	4536		1850
159	Kolkata	Shillong	5481		1850
160	Kolkata	Silchar	5001	11085	1850
161	Kolkata	Tezpur	5151		1850
162	Kozhikode	Chennai	5151		1850
163	Kozhikode	Kolkata	8456		3650
164	Kozhikode	Mumbai	9100	16513	2150
165	Kozhikode	Trivandrum	4391		1850
166	Kullu	Pathankot	4001		1850
167	Leh	Srinagar	4603	8283	1850
168	Lilabari	Tezpur	3881		1850
169	Lucknow	Mumbai	8051	24985	2950
170	Lucknow	Varanasi	4626	9607	1850
171	Ludhiana	Pathankot	4201		1850
172	Madurai	Mumbai	7851	23657	2950
173	Mangalore	Mumbai	6086	15761	1850
174	Mumbai	Nagpur	5001	15159	1850
175	Mumbai	Raipur	9500	20692	2150
176	Mumbai	Rajkot	5281	12473	1850
177	Mumbai	Ranchi	9751	22813	2950
178	Mumbai	Srinagar	9016	23756	3650
179	Mumbai	Trivandrum	10201	23901	2950
180	Mumbai	Udaipur	4786	15407	1850
181	Mumbai	Varanasi	9696	24511	2950
182	Mumbai	Vishakhapatnam	9101	24872	2950
183	Patna	Ranchi	4603		1850
184	PortBlair	Vishakhapatnam	9696	24511	2950
185	Raipur	Bhubaneshwar	4281	9977	1850
186	Raipur	Nagpur	5181	12660	1850
187	Raipur	Vishakhapatnam	4251	11606	1850
188	Shillong	Jorhat	4551		1850
189	Silchar	Tezpur	4101		1850
190	Tirupati	Vijayawada	5406		1850
191	Vishakhapatnam	Bhubaneshwar	5321	10518	1850
)
return

esc::ExitApp
I hope you now feel like your birthday or your highest public holiday (like Christmas in Germany) :!:

Hubert
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 05:25

@Hubert
man! this is it. :)

give me some time to ponder over it.
I am 99.9% sure that it is complete.

:)
thanks again to @Hubert and @Alphabravo for putting in so much of time+patience+knowledge+intelligence into it.
All hail All in here!

and yes Party is surely to be held. hehe
John ... you working ?
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 07:32

Not yet
Agartala to Delhi

Agartala to Guwahati to Kolkata to Delhi ; is the shortest route

but we see...
Attachments
agarta.PNG
John ... you working ?
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 08:39

sorry but there is even a shorter route:

Agartala --> Kolkata --> Delhi

but only, if you supply the connection Kolkata --> Delhi :!:

Hubert
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 09:08

Yes, the connection is there it can be any way either A to B or B to A
so there is no need to supply the connection.



?

thanks and regards
John ... you working ?
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 09:20

@smorgasbord: you mean, the reverse route is only put into the comboboxes, but not into the result?
This was my assumption due to your statement:
another ( seems to be the last thing ) requirement, if the original var does not have route like Guwahati to Agartala but instead has Agartala to Guwahati can the code show empty OR null or no routes from Guwahati to Agartala but shows route from Agartala to Guwahati?
What you obviously now want was already solved with a previous post

Hubert
User avatar
AlphaBravo
Posts: 391
Joined: 29 Sep 2013, 22:59

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 10:03

It seems to me that smorgasbord has two contradicting statements there!
Am I to assume my final code works for your?
http://ahk.uk.to/?p=574e6a
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 11:36

@alphabravo: I found no problems with the connections I tested.

Hubert
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 12:00

It seems to me that smorgasbord has two contradicting statements there!
Am I to assume my final code works for your?
http://ahk.uk.to/?p=574e6a
yes this works. but can it show for the routes whether it were taken directly from the table given, like that of Hubert's
( that is it shows whether reverse routes were found or direct routes were found even for the intermediate journeys?
John ... you working ?
User avatar
AlphaBravo
Posts: 391
Joined: 29 Sep 2013, 22:59

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 16:06

I wish you show us an example of an expected output, but try this http://ahk.uk.to/?p=c17d40
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

11 Apr 2015, 21:50

@Alphabravo

YES!! this shows both ways and even shows which route was used direct or reverse.!!

give me some more time.

seems the topic is solved.
:)
John ... you working ?
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

14 Apr 2015, 23:16

Code: Select all

Loop, %A_ScriptDir%\*.*
{
   if A_loopfilename contains _.txt
    FileList = %FileList%%A_LoopFileName%`n
}
Sort, FileList, R  ; The R option sorts in reverse order. See Sort for other options.
Loop, parse, FileList, `n
{
    if A_LoopField =  ; Ignore the blank item at the end of the list.
        continue
    ;~ MsgBox, 4,, File number %A_Index% is %A_LoopField%.  Continue?
    ;~ if ( list_files != "" )
    list_files .= A_LoopField . "|"
    IfMsgBox, No
        break
}

;======================================================================================

Gui, add, Text,, select file:
Gui, add, DDL, xs vList w200 gSubmit, % list_files ; select file:
Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:

Gui, add, DDL, xs vFrom gSubmit
Gui, add, DDL, x200 yp vTo gSubmit
Gui, add, Edit, xs w340 r10
;~ Gui, +AlwaysOnTop
Gui, show

Submit:
gui, submit, nohide
;~ MsgBox % list

;~ return
fileread, data, %list%
MsgBox % data
sleep 500
;~ data := RegExReplace(data, "\t\t", "`t0`t")     ; replace empty fields with 0
;~ loop, parse, data, `n, `r, `r`n, `n`r
    ;~ MsgBox % A_loopfield

loop, parse, data, `n, `r, `r`n, `n`r
{
     x := StrSplit(A_LoopField, "`t"), total := x.4 + x.6,       res .= x.2 "`t" x.3 "`t" total "`n" 
     ;~ MsgBox % total
 }
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios

nodes:=[], Distance := [], Direct := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Direct[field.1,field.2] := field.3 , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

GuiControl, , from, % nodeList
GuiControl, , To, % nodeList


;~ Gui, Submit, NoHide
GuiControl, , Edit1, % ""
if !(From && To) || (From = To)
    return

res := Dijkstra(data, From)
Route := Done := xTo := xFrom := DirectFlight := "" , origin := to

if Distance[From, To]       ; direct flight
    DirectFlight := "`n`nNon-Stop Flight`n" From "`t>`t" To "`t" Distance[xFrom , xTo] "`t" Distance[From, To] "`t" (Direct[xFrom, xTo]? "Direct" : "Reverse") "`n" 

if !res[1, To]              ; no possible route
{
    GuiControl, , Edit1, no routing found
    return
}

Routing:
Loop % objCount(nodes)
    for xTo , xFrom in res.2
    {
        if (xTo = To)
        {
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`t" (Direct[xFrom, xTo]? "Direct" : "Reverse") "`n" route
			To := xFrom
            if (xFrom = From)
                break, Routing
        }
    }

GuiControl, , Edit1, % "From`t>`tTo`tLength`tDirect`n" . route . "`nTotal distance = " res.1[origin] . DirectFlight
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;----------------------------------------------
return
got stuck again. updating it.. :)

thanks and regards
John ... you working ?
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: [unsolved]how to search for via routes when there are no

15 Apr 2015, 10:05

@Alphabrave & @Hubert
sorry, i did not explain it at all.
i have added a gui DDN list from which the text file is to be selected that contains the %DATA% that is to be processed.

can the same be processed ?? in the same manner?

thanks and regards
John ... you working ?
hd0202
Posts: 61
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: [unsolved]how to search for via routes when there are no

15 Apr 2015, 11:41

I have changed your code and marked the changed/inserted lines with "; hd":

Code: Select all

Loop, %A_ScriptDir%\*.*
{
   if A_loopfilename contains _.txt
    FileList = %FileList%%A_LoopFileName%`n
}
Sort, FileList, R  ; The R option sorts in reverse order. See Sort for other options.
Loop, parse, FileList, `n
{
    if A_LoopField =  ; Ignore the blank item at the end of the list.
        continue
    ;~ MsgBox, 4,, File number %A_Index% is %A_LoopField%.  Continue?
    ;~ if ( list_files != "" )
    list_files .= A_LoopField . "|"
    IfMsgBox, No
        break
}

;======================================================================================

Gui, add, Text,, select file:
Gui, add, DDL, xs vList w200 gSubmit1, % list_files ; select file:	; hd
Gui, add, Text,, From:
Gui, add, Text, x200 yp, To:

Gui, add, DDL, xs vFrom gSubmit
Gui, add, DDL, x200 yp vTo gSubmit
Gui, add, Edit, xs w340 r10
;~ Gui, +AlwaysOnTop
Gui, show
return	; hd

Submit1:	; hd
gui, submit, nohide
;~ MsgBox % list

;~ return
fileread, data, %a_scriptdir%\%list%	; hd
MsgBox % data
sleep 500
;~ data := RegExReplace(data, "\t\t", "`t0`t")     ; replace empty fields with 0
;~ loop, parse, data, `n, `r, `r`n, `n`r
    ;~ MsgBox % A_loopfield

loop, parse, data, `n, `r, `r`n, `n`r
{
     x := StrSplit(A_LoopField, "`t"), total := x.4 + x.6,       res .= x.2 "`t" x.3 "`t" total "`n" 
     ;~ MsgBox % total
 }
data := RegExReplace( res, "`am)^\D+$\R")       ; remove lines with no connectios
nodes:=[], Distance := [], Direct := []
for each, line in StrSplit(data, "`n" , "`r")
    field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
    , Direct[field.1,field.2] := field.3 , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3

for node, v in nodes
    nodeList .= (nodeList?"|":"") node (A_Index=1?"|":"")

GuiControl, , from, % nodeList
GuiControl, , To, % nodeList
return	; hd

submit:	; hd
gui, submit, nohide	; hd

;~ Gui, Submit, NoHide
GuiControl, , Edit1, % ""
if !(From && To) || (From = To)
    return

res := Dijkstra(data, From)
Route := Done := xTo := xFrom := DirectFlight := "" , origin := to

if Distance[From, To]       ; direct flight
    DirectFlight := "`n`nNon-Stop Flight`n" From "`t>`t" To "`t" Distance[xFrom , xTo] "`t" Distance[From, To] "`t" (Direct[xFrom, xTo]? "Direct" : "Reverse") "`n" 

if !res[1, To]              ; no possible route
{
    GuiControl, , Edit1, no routing found
    return
}

Routing:
Loop % objCount(nodes)
    for xTo , xFrom in res.2
    {
        if (xTo = To)
        {
            route := xFrom "`t>`t" xTo "`t" Distance[xFrom , xTo] "`t" (Direct[xFrom, xTo]? "Direct" : "Reverse") "`n" route
			To := xFrom
            if (xFrom = From)
                break, Routing
        }
    }

GuiControl, , Edit1, % "From`t>`tTo`tLength`tDirect`n" . route . "`nTotal distance = " res.1[origin] . DirectFlight
return

esc::
GuiClose:
ExitApp
return

;-----------------------------------------------
Dijkstra(data, start){
    nodes := [], dist := [], Distance := [], dist := [], prev := [], Q := [], min := "x"
    for each, line in StrSplit(data, "`n" , "`r")
        field := StrSplit(line,"`t"), nodes[field.1] := 1, nodes[field.2] := 1
        , Distance[field.1,field.2] := field.3, Distance[field.2,field.1] := field.3
    dist[start] := 0, prev[start] := ""

    for node in nodes 
    {
        if (node <> start)
            dist[node] := "x"
            , prev[node] := ""
        Q[node] := 1
    }
    
    while % ObjCount(Q)
    {
        u := MinDist(Q, dist).2
        for node, val in Q
            if (node = u) 
            {
                q.Remove(node)
                break
            }
            
        for v, length in Distance[u] 
        {
            alt := dist[u] + length
            if (alt < dist[v])
                dist[v] := alt  
                , prev[v] := u
        }
    }   
    return [dist, prev]
}
;-----------------------------------------------
MinDist(Q, dist){
    for node , val in Q
        if A_Index=1
            min := dist[node], minNode := node
        else
            min := min < dist[node] ? min : dist[node]  , minNode := min < dist[node] ? minNode : node      
    return [min,minNode]
}
ObjCount(Obj){
    for key, val in Obj
        count := A_Index
    return count
}
;----------------------------------------------
return
Hubert

Return to “Ask For Help”

Who is online

Users browsing this forum: alexooo, AlphaBravo, Decoy, kryptooo, Mipha, SAbboushi, swagfag, Syrup, Xtra and 59 guests