ASCAN

ASCAN(a,[d])
a
array
[d]
direction argument ; 0 or omitted scan by array, -1 by rows, 1 by clms

Array SCAN, 3 in 1 function, SCAN by row, by column, by array

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
ASCAN, Array SCAN, 3 in 1 function, SCAN by row, by column, by array. This is my take of tboulden's SCANBYROW/BYCOL.
Uses only new!! SCAN lambda helper function, no need of byrow,bycol,makearray, or lambda as arguments.
Excel Formula:
=LAMBDA(a,[d],
    LET(y,IF(d=1,TRANSPOSE(a),a),s,SCAN(0,y,LAMBDA(v,a,v+a)),r,ROWS(s),c,COLUMNS(s),sr,SEQUENCE(r)-1,
       x,s-IF(d,IFERROR(INDEX(INDEX(s,,c),sr)*sr^0,0),0),
       IF(d=1,TRANSPOSE(x),x)
   )
)
LAMBDA 1.1.3.xlsx
ABCDEFGHIJKLMNOPQRST
1d,omitted (by array)d,-1 (by rows)d,1 (by clms)
2sample 1a=ASCAN(A3:D6)=ASCAN(A3:D6,-1)=ASCAN(A3:D6,1)
3123413610136101234
45678152128365111826681012
5910111245556678919304215182124
613141516911051201361327425828323640
7
8d,omitted (by array)d,-1 (by rows)d,1 (by clms)
9sample 2a=ASCAN(A10#)=ASCAN(A10#,-1)=ASCAN(A10#,1)
10123413610136101234
115678152128365111826681012
12910111245556678919304215182124
1313141516911051201361327425828323640
14171819201531711902101735547445505560
15212223242312532763002143669066727884
16
17Note: Did not set an extra argument for any kind of "initial value", since the only thing it does is too simple,
18adds a constant value to the final outcome , like in 10+ASCAN(a)
19
ASCAN post
Cell Formulas
RangeFormula
F2,P9,K9,F9,P2,K2F2=FORMULATEXT(F3)
F3:I6F3=ASCAN(A3:D6)
K3:N6K3=ASCAN(A3:D6,-1)
P3:S6P3=ASCAN(A3:D6,1)
A10:D15A10=SEQUENCE(6,4)
F10:I15F10=ASCAN(A10#)
K10:N15K10=ASCAN(A10#,-1)
P10:S15P10=ASCAN(A10#,1)
Dynamic array formulas.
 
Upvote 0
You are very good. If you made this using a mobile, you are a genius.✌️ To upload something use a cloud downloadable link.
I got it, you need kind of countif byrow then a min btw 2 arrays and a sum byrow. No problem. We do not need countif at all. Or concat and mid.
Here is a simple function concept a countif by criteria function, (ar,array; cr,criteria)
CIF(ar,cr)
Excel Formula:
=LAMBDA(ar, cr, DROP(REDUCE(0, cr, LAMBDA(v, i, HSTACK(v, BYROW(N(ar = i), LAMBDA(x, SUM(x)))))), , 1))

And the formula that does all:
=LET(s,{0,1,2,3},BYROW(MAP(CIF(A1#,s),CIF(F1#,s),LAMBDA(x,y,MIN(x,y))),LAMBDA(x,SUM(x))))
Hope you'll like it. 😊🙏

Note: For @smozgur . The latest update is amazing. Great job ✌️✌️!!!!

Book1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
10110103133
22301201133
30221333211
40100121311
53203131111
60001302022
70122121122
81223330011
90220213311check results both methods
100003221011=AND(K1#=N1#)
111203103133TRUE
123121122333
130320001233
142200003233
152100322122
160032303033
171210312322
181110131033
193131010011
203320223122
211133220111
222320233122
233210012344
24=RANDARRAY(23,4,0,3,1)=RANDARRAY(23,4,0,3,1)K1: your formula
25=MMULT(MAP(MID(BYROW(A1:D23,LAMBDA(x,CONCAT(COUNTIF(x,SEQUENCE(,4,0,))))),{1,2,3,4},1)*1,MID(BYROW(F1:I23,LAMBDA(y,CONCAT(COUNTIF(y,SEQUENCE(,4,0,))))),{1,2,3,4},1)*1,LAMBDA(x,y,MIN(x,y))),{1;1;1;1})
26
27N1:
28=LET(s,{0,1,2,3},BYROW(MAP(CIF(A1#,s),CIF(F1#,s),LAMBDA(x,y,MIN(x,y))),LAMBDA(x,SUM(x))))
29All steps covered by the single cell formula
30
31This is what CIF delivers ( seq(,4,0) <=> {0,1,2,3} )=MAP(A33#,F33#,LAMBDA(x,y,MIN(x,y)))
32=CIF(A1#,{0,1,2,3})=CIF(F1#,SEQUENCE(,4,0))=BYROW(K33#,LAMBDA(x,SUM(x)))
332200120112003
341111121011103
351120001300101
363100021101001
371012030100011
383100201120002
391120031001102
400121200200011
412020011200101
423001112010001
431111120111013
440211012101113
452011211020103
462020201120103
472110012101102
482011200220013
491210011201102
501300120112003
510202310001001
521012012100112
530202112001001
541021011200112
551111111111114
56
Sheet2
Cell Formulas
RangeFormula
A1:D23,F1:I23A1=RANDARRAY(23,4,0,3,1)
K1:K23K1=MMULT(MAP(MID(BYROW(A1:D23,LAMBDA(x,CONCAT(COUNTIF(x,SEQUENCE(,4,0,))))),{1,2,3,4},1)*1,MID(BYROW(F1:I23,LAMBDA(y,CONCAT(COUNTIF(y,SEQUENCE(,4,0,))))),{1,2,3,4},1)*1,LAMBDA(x,y,MIN(x,y))),{1;1;1;1})
N1:N23N1=LET(s,{0,1,2,3},BYROW(MAP(CIF(A1#,s),CIF(F1#,s),LAMBDA(x,y,MIN(x,y))),LAMBDA(x,SUM(x))))
Q10,P32,F32,A32Q10=FORMULATEXT(Q11)
Q11Q11=AND(K1#=N1#)
A24,F24A24=FORMULATEXT(A1)
K25K25=FORMULATEXT(K1)
K28K28=FORMULATEXT(N1)
K31K31=FORMULATEXT(K33)
A33:D55A33=CIF(A1#,{0,1,2,3})
F33:I55F33=CIF(F1#,SEQUENCE(,4,0))
K33:N55K33=MAP(A33#,F33#,LAMBDA(x,y,MIN(x,y)))
P33:P55P33=BYROW(K33#,LAMBDA(x,SUM(x)))
Dynamic array formulas.
Lambda Functions
NameFormula
CIF=LAMBDA(ar,cr,DROP(REDUCE(0,cr,LAMBDA(v,i,HSTACK(v,BYROW(N(ar=i),LAMBDA(x,SUM(x)))))),,1))
 
Hi Xlambda,

The function will wait for me to return to the computer.
I discovered something in a certain function that probably no one noticed, very significant, and I want to investigate more, in a few days, and you will be the first to update, I promise.

Thanks,
David
 
Note: For @smozgur . The latest update is amazing. Great job ✌️✌️!!!!
@Xlambda: Glad to hear it helps. Thanks for the feedback!
By the way, if you enter the lambda function with line breaks, then it is displayed better in the meta section.
Cell Formulas
RangeFormula
B2B2=LAMBDA(a,[d], LET(y,IF(d=1,TRANSPOSE(a),a),s,SCAN(0,y,LAMBDA(v,a,v+a)),r,ROWS(s),c,COLUMNS(s),sr,SEQUENCE(r)-1, x,s-IF(d,IFERROR(INDEX(INDEX(s,,c),sr)*sr^0,0),0), IF(d=1,TRANSPOSE(x),x) ) )
 
Hi Xlambda,

Regarding the first formula,He is not right .in total, in sum, it is true, the idea in the formula that it returns, that there is a match in location=SEQUENCE(,4,0,1)=criteria, and sum.
And the two criteria in the countifs function are not always equal.

Thanks,
David
 
Hi David,

I'm lost. For the context you provided and what you requested:
I will give two examples about the same two ranges of data, in the ranges there are numbers between 0 and 3 .A1:D23 ,and F1:I23.The ranges has similar values.

first function,
=COUNTIFS(A1:D1,SEQUENCE(,4,0,1),F1:I1,SEQUENCE(,4,0,1)),
I want to sum each line, in one formula.
you only need this:
=BYROW(N(A1#=F1#),LAMBDA(x,SUM(x)))
For the Sum is no need of any countifs or third vector reference seq(,4,0) because the outcome is very simple. The formula calculates the sum of matching values positions by row for 2 arrays same dimensions, whatever the values are.

For other infinite tasks that I cannot guess them all, other tasks that require useful use of explicit COUNTIFS replacement methods, I have already provided the CIF(ar,cr) function that can be adapted to any need, like second task you requested.
There are also tons of ways to make a formula like byrow to spill results, even using COUNTIFS whenever specific tasks allow us.
So, if you need the results that
=COUNTIFS(A1:D1,SEQUENCE(,4,0,1),F1:I1,SEQUENCE(,4,0,1))
returns, before the sum, is very easy using CIF
=CIF(IF(A1#=F1#,A1#),SEQUENCE(,4,0))
(all single cell formulas of course)

PS: You should consider using the amazing add-in interface to post spreadsheets XL2BB - Excel Range to BBCode
and also consider asking questions here: there are so many beautiful Excel minds that can solve tasks faster and better than me. 😉

On this minisheet, using random distributions, you can test indefinitely as long as you want, the acuracy of formulas you wanted by hitting F9. Same for second task already posted. ✌
Book1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1321301120100110100
2012132110100110100
3013313120000000000
4122302310010110010
5000031110000000000
6011011001100221100
7221333200000000000
8103203330001110001
9320033031001221001
10113100120000000000
11100133001000111000
12232320200020220020
13203220221020331020
14002233320010110010
15311112100100110100
16003132320001110001
17022300221010221010
18022302311010221010
19213122130010110010
20202120131010221010
21100132220000000000
22020122100010110010
23322311320000000000
24=RANDARRAY(23,4,0,3,1)=RANDARRAY(23,4,0,3,1)↑↑↑↑↑↑↑↑
25↑↑=SUM(K1:N1)=BYROW(N(A1#=F1#),LAMBDA(x,SUM(x)))
26↑↑drag and dropsingle form.↑↑
27↑↑check sum↑↑
28↑↑=AND(P1:P23=S1#)↑↑
29↑↑TRUE↑↑
30↑↑↑↑
31=COUNTIFS(A1:D1,SEQUENCE(,4,0),F1:I1,SEQUENCE(,4,0))=CIF(IF(A1#=F1#,A1#),SEQUENCE(,4,0))
32drag and dropsingle form.
33check COUNTIFS
34=AND(V1#=K1:N23)
35TRUE
36
Sheet3
Cell Formulas
RangeFormula
A1:D23,F1:I23A1=RANDARRAY(23,4,0,3,1)
S1:S23S1=BYROW(N(A1#=F1#),LAMBDA(x,SUM(x)))
V1:Y23V1=CIF(IF(A1#=F1#,A1#),SEQUENCE(,4,0))
K1:N23K1=COUNTIFS(A1:D1,SEQUENCE(,4,0),F1:I1,SEQUENCE(,4,0))
P1:P23P1=SUM(K1:N1)
A24,F24A24=FORMULATEXT(A1)
P25,S25P25=FORMULATEXT(P1)
S28,V34S28=FORMULATEXT(S29)
S29S29=AND(P1:P23=S1#)
K31,V31K31=FORMULATEXT(K1)
V35V35=AND(V1#=K1:N23)
Dynamic array formulas.
 
Hi Xlambda,

Here is what I promised,
The take function, together with the countif function, produces accumulation, take also works with sum ifs,I think also average ifs, I will give several examples:

1.=COUNTIF(TAKE(A1:D23,,{1,2,3,4}),SEQUENCE(4,,0))
2.=COUNTIF(TAKE(A1:D23,SEQUENCE(23,,1),4),SEQUENCE(,4,0))

3.Similar, but in reverse order ,
=COUNTIF(TAKE(A1:D23,SEQUENCE(23,,-23),4),SEQUENCE(,4,0))

4.countifs,=LET(x,SEQUENCE(23,,1),y,SEQUENCE(,4,0),COUNTIFS(TAKE(A1:D23,x,4),y,TAKE(F1:I23,x,4),y))

5.with map,
=LET(a,SEQUENCE(23,,1),b,SEQUENCE(,4,0),MAP(COUNTIF(TAKE(A1:D23,a,4),b),COUNTIF(TAKE(F1:I23,a,4),b),LAMBDA(x,y,MIN(x,y))))

Thanks,
David
 
Hi David,

I've called this before a static illusion of an expandable range that has no real iterative DNA versatility. (try to add a simple 0 ( take(...)+0 ) or any regular function and colapses)
Can never replace, for example a custom made bycol/byrow that spills, or scan byrow or any other native or custom-made lambda helper function with real iterative versatility that can do any function.
The fact that it works with ...IFS, but only cumulative, not simple byrow or bycol, makes them useful only when you will ever need these very specific scenarios. ✌
My goal was always to find ways to replace the range able only ...IFS functions.😉. This is what happens when we play with excel, we find things that work and try to invent a use for it.
This will lead to great discoveries. 😊
In a loop for i=1 to n take is cool because it can replace index(ar,sequence(i)) with take(ar,i), a simple way for dynamic expandable range in a loop.
PS: Still no minisheet add in? 😊
 
Latest challenge of @MrExcel, a super cool algorithm challenge in general.
File link in the description or here
The first draft solution I posted it at the comments section, this is second one. There are also other ways to approach this, I will cover here only the simplest ones, shortest route, and optimized clusters.
If there is any interest or questions, I will be happy to cover stuff about the function's concepts and tricks used.
From the beginning I have simplified the initial data and replaced the long names with index numbers "k" => first clm is a sequence "s" =seq(146)
Note: Define all the names and functions for everything to work properly (they call each other). For simplicity I made it as modular as possible.
The concept is simple. For a starting point or a reference index k we calculate the shortest path to cover all elements route, 2 ways. Finally, we iterate these methods for each element to find which starting point produces the shortest path.
Defined names: pl: range ; ll: formula
pl defined name: (places) refers to the range of the orig data sorted in alphabetic order
Excel Formula:
=Sheet1!$B$2:$D$147
ll: defined name: (lat and long) replaces names with seq, defined name with a formula:
Excel Formula:
=HSTACK(SEQUENCE(ROWS(pl)),TAKE(pl,,-2))

Tool functions: (k or K refers to a single index number, s or S refers to a sequence of numbers
FL(k,s) Filters out an index nr. k (or entire row that has first clm an index nr.) from a sequence s (or an array that has first clm a seq)
Excel Formula:
=LAMBDA(k,s,FILTER(s,ISNA(XMATCH(TAKE(s,,1),TAKE(k,,1)))))
DK(k,s,[min]) Distance calculator btw an index nr k and a seq of numbers s . If min=1 => the minimum distance and its index nr. is extracted
Excel Formula:
=LAMBDA(k, s, [min],
    LET(
        x, TAKE(k, , 1),
        y, TAKE(s, , 1),
        f, FL(x, y),
        a, INDEX(ll, x, 2),
        b, INDEX(ll, f, 2),
        c, INDEX(ll, x, 3),
        d, INDEX(ll, f, 3),
        e, ACOS(SIN(RADIANS(a)) * SIN(RADIANS(b)) + COS(RADIANS(a)) * COS(RADIANS(b)) * COS(RADIANS(d) - RADIANS(c))) * 3958,
        IF(min, TAKE(SORT(HSTACK(f, e), 2), 1), e)
    )
)
DS(s,[sum]) Distance between consecutive elements of a sequence s . If sum=1 sums all the distances
Excel Formula:
=LAMBDA(s,[sum],LET(m,MAP(DROP(s,-1),DROP(s,1),LAMBDA(x,y,DK(x,y))),u,VSTACK(0,m),IF(sum, SUM(u),u)))

Main functions:
MK( k ,[ s ],[ lst ] ) Minimum consecutive distances between an index k as reference starting point and elements of a seq s, If s omitted, entire seq is used (146 places) . If lst =1 the summary of starting point k and total route distance is returned. It's an iterative function. Recursion can be used also.
Excel Formula:
=LAMBDA(k, [s], [lst],
    LET(
        a, FL(k, IF(ISOMITTED(s), SEQUENCE(ROWS(pl)), s)),
        r, REDUCE(HSTACK(k, 0), SEQUENCE(ROWS(a)), LAMBDA(v, i, VSTACK(v, DK(TAKE(v, -1), FL(v, a), 1)))),
        IF(lst, r, HSTACK(k, SUM(TAKE(r, , -1))))
    )
)
CK(k,cl,[lst]) Cluster optimization for a starting point k, a cluster radius cl. If MK can take a sample s of entire seq, CK by default uses entire "population"
If lst (list arg) =1 entire new optimized route is listed, very probably with a new starting point.
if lst is omitted, only initial k and total of the new optimized route is returned.
Note: CK calls a function that does the cluster condition distribution extraction CSCAN Consecutive SCAN. I use this custom-made lambda helper function when I need to extract the terms thar are in a certain consecutive relationship.
CSCAN alone, to be presented properly with examples of its versatility needs at least 15 posts, so, some other time.😒
CK(k , cl ,[ lst ])
Excel Formula:
=LAMBDA(k, cl, [lst],
    LET(
        m, MK(k, , 1),
        c, CSCAN(TAKE(m, , -1), LAMBDA(x, y, AND(x < cl, y < cl)), TAKE(m, , 1)),
        f, TAKE(c, , 1),
        x, MC(f),
        y, MK(INDEX(x, 1, 1), f, 1),
        z, XMATCH(TAKE(y, , 1), f),
        a, INDEX(c, z, SEQUENCE(, COLUMNS(c))),
        b, IF(a = "", NA(), a),
        t, TOCOL(b, 2),
        d, DS(t),
        u, SUM(d),
        IF(lst, VSTACK(HSTACK(k, u), HSTACK(t, d)), HSTACK(k, u))
    )
)
CK calls this: CSCAN(v,fn,[a],[cnt],[flt])
Excel Formula:
=LAMBDA(v, fn, [a], [cnt], [flt],
    LET(
        f, LAMBDA(k, i, IF(i = 1, 1, IF(fn(INDEX(v, i), INDEX(v, i - 1)), k, k + 1))),
        s, SCAN(0, SEQUENCE(ROWS(v)), f),
        e, DROP(FREQUENCY(s, UNIQUE(s)), -1),
        q, e >= SEQUENCE(, MAX(e)),
        c, SCAN(0, --q, SUM),
        x, INDEX(IF(ISOMITTED(a), v, a), c),
        y, IF(q, x, ""),
        w, SWITCH(cnt, 0, y, 1, HSTACK(e, y), 2, e),
        IF(flt, FILTER(w, e > 1), w)
    )
)

Now, previous functions MK(minimum or shortest distances) and CK (cluster optimized shortest distances) needed a reference index nr. k , as starting point.
We need a function to iterate the calculations for all indexes s and summarize the results in order to be able to pick the best one.
MC([ s ],[ mc ],[ cl ]) Minimum or Cluster method results for entire "population" of references s (our references represent all places in our example, more elements can be extracted or added, and everything will be dynamically calculated)
s: if omitted =seq(rows(pl)). if not omitted has relevance only for MK calc (when mc arg. is omitted) Note: s is ignored if mc=1 , when CK is triggered
mc: if omitted MK iterates for all k in s ; if not omitted CK iterates for all k in entire s
cl: cluster radius, only if mc=1, otherwise it has no effect.
MC( [ s ],[mc],[cl])
Excel Formula:
=LAMBDA([s], [mc], [cl],
    LET(
        a, IF(ISOMITTED(s), SEQUENCE(ROWS(pl)), s),
        m, MAP(a, LAMBDA(x, TEXTJOIN(",", , IF(mc, CK(x, cl), MK(x, a))))),
        SORT(--HSTACK(TEXTBEFORE(m, ","), TEXTAFTER(m, ",")), 2)
    )
)
Book2
ABCDEFGHIJK
1pl (names ascending order)=ll
2Abajo Peak, Utah37.8809-109.383137.8809-109.383=ROWS(F2#)
3Ahu Tongariki, Rapa Nui (Easter Island)-27.1126-109.1682-27.1126-109.168146
4Aiguestortes National Park, Spain42.60.99342.60.99
5Alblittkopf Mountain, Austria47.0846410.33855447.0846410.33855
6Ameenpur, Telangana, India17.578.3196517.578.3196
7Andenes, Norway69.3192316.11369669.3192316.11369
8Aoraki / Mount Cook, Southern Alps, New Zealand-43.7344170.157-43.7344170.15
9Atacama Desert, Chile-24-708-24-70
10Auroa Point, New Zealand-39.0715177.90189-39.0715177.9018
11Badwater Basin, Death Valley, California36.239-116.8411036.239-116.841
12Baharyia Oasis Salt Lake, Eqypt28.35228.9321128.35228.932
13Bell Park Pier, South Africa-28.958829.4338912-28.958829.43389
14Blue Lagoon, Iceland63.8806-22.0171363.8806-22.017
15Cala Tuent Mallorca, Spain39.852.7861439.852.786
16Cape Evans, Ross Island, Antarctica-76.7829168.51515-76.7829168.515
17Cape of Good Hope, South Africa-3418.417416-3418.4174
18Castle Hill, New Zealand-43.2516172.761317-43.2516172.7613
19Cathedral Gorge, Australia-17.4805128.372718-17.4805128.3727
20Cayo Muerto, Venezuela10.92978-68.26051910.92978-68.2605
Sheet1
Cell Formulas
RangeFormula
F1,J2F1=FORMULATEXT(F2)
F2:H147F2=ll
J3J3=ROWS(F2#)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
pl=Sheet1!$B$2:$D$147F2


Book2
ABCDEFGHIJKLMNOPQRST
1Tool functions
2FL(k,s) Filter functionDK(k,s,[min]) Dist btw k and each elem of sDS(s,[sum]) Distances between consecutive terms of a seq s
3
4k4=FL(B4,B6:B9)k11min,omittedsum,omitted
56=DK(H4,H6:H10)=DS(N6:N10)
6s65s321261.935s320
752453227.425454457.3141
84120448.43961203675.7609
9214133.05862141474.40684
10779928.763779961.3049
11
12Versatility FLreasoningreasoning
13k can be 2D array1st term - dist btw 11 and 321st term - dist btw 32 and 32
14s can be 2D array2nd term - dist btw 11 and 452nd term - dist btw 45 and 32
15Result keeps "s" structure................
16
17k7a=FL(B17:C19,B21:C27)min,1sum,1check
182b91234.1=DK(H4,H6:H10,1)=DS(N6:N10,1)=SUM(P6#)
195234634.4314133.0586218568.78718568.79
201256.89
21s91234.1435345!!! Cool statistical data !!!!
22634.4320120.54Sum of Consecutive distances off all places in alphab order
235876.345
241256.89=DS(SEQUENCE(146),1)
2535345717697.18km
2672345
2720120.54Total route if we keep alphab. order=717,698 km
28
Sheet2
Cell Formulas
RangeFormula
D4,P24,P18,R18,J18,E17,P5,J5D4=FORMULATEXT(D5)
D5:D7D5=FL(B4,B6:B9)
J6:J10J6=DK(H4,H6:H10)
P6:P10P6=DS(N6:N10)
E18:F22E18=FL(B17:C19,B21:C27)
J19:K19J19=DK(H4,H6:H10,1)
P19P19=DS(N6:N10,1)
R19R19=SUM(P6#)
P25P25=DS(SEQUENCE(146),1)
Dynamic array formulas.


Book2
ABCDEFGHIJKLMN
1
2Main functions:
3MK(k,[ s ],[lst]) ; MC([ s ],[mc],[cl]) ( for minimum method, to trigger only MK iterations mc,cl arguments are omitted)
4
5Task 1: If we start from k=100Task 2: All routes sizes for all starting points
6? Shortest route to cover also these "places"
7s27lst,1shortest route if we start from 27
8128=MK(100,B7:B15,1)=MC(B7:B15)=MK(27,B7:B15,1)
95710002722330.68 =>270
1021128568.14519822689.141002021.723
1143431144.1912124755.75128568.1451
1298272445.39712824842.48431144.191
13140216995.732724958.03214723.72
1475759484324961.15575948
1510076240.13910025026.3776240.139
16140259.813814025131.23140259.8138
17981424.9515725699.75981424.951
18
19Route starting from k=100 covering all "s" has a total of:=SUM(L9:L17)
20lst,omitt.22330.68
21=MK(100,B7:B14)
2210025026.37
23
24In the ranking of routes, starting from 100 is the 7th
25
Sheet4
Cell Formulas
RangeFormula
H8,D21,L19,D8,K8H8=FORMULATEXT(H9)
D9:E17D9=MK(100,B7:B15,1)
H9:I17H9=MC(B7:B15)
K9:L17K9=MK(27,B7:B15,1)
L20L20=SUM(L9:L17)
D22:E22D22=MK(100,B7:B14)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
pl=Sheet1!$B$2:$D$147D9, D22, H9, K9
 
Book2.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1
2Main functions:
3CK(k,cl,[lst]);
4CK concept: with the help of CSCAN (Consecutive Scan), the function takes MK(k,,1) distrib and groups it for clusters < cl km radius.
5The "tangent" values of these clusters are iterated with MC to find shortest route btw clusters
6Last step is to add the inner cluster values to the sequence of tangent sequence.
7
8CK method, same starting k and different cluster sizes cl.(we can write functions to iterate by cluster size, if we want)
9cl,300cl,500cl,700route distrib with MKtotal routeData interpretation
10=CK(100,300,1)=CK(100,500,1)=CK(100,700,1)MK method for k=100no cluster method
1110087615.1510087448.6610090724.42=MK(100,,1)=MK(100)the reference k starting point
12660990290100010092743.571total of the new route, with clusters rearranged and
13191979.668142543.6626121588.49773115.9907with a new starting point, for different cluster values
1449915.365430508.5571117292.10415550.40737cluster method summary
1596289.028820977.9272144149.399714254.6058Note: Same starting point k and same cluster size cl, will always produce same result
1620741.717688339.835642357.636156401.3614
1788339.835649704.4787111538.476225.37508=CK(100,300)
18301228.01396289.028814133.058622832.7584410087615.152
19142508.557147880.19861061181.06880134.5152=CK(100,500) - CK method, For same k, best route was for cl=500km 87,448 km
2063384.1693711548.171261049.8865384.6265710087448.663 - Without cluster optimization (MK) delivers 92743km
218221.8697313490.11446128542.756451.07941=CK(100,700)
226439.55923107504.8411100568.14514443.7255310090724.424=O12-O20
23181.194248311.36743115.9907119103.93985294.908km saved
2413765.8888757223.30095550.407378120.99846
2574171.3241311513.53814254.605861516.1575
2678158.21615821.1604556401.361473219.7705
2794505.004110410.31109225.37508131192.4239
2879386.49522459.4312832.7584495421.8409
291153.077359762654.18380134.515239560.6473
30102131.7838338.777565384.62657368.87761
311051.5762292480.161451.0794150148.2438
32132146.511886126.03754443.72553136116.8656
33113263.9723140169.7012119103.939812254.59811
3487743.7165924.3138118120.998462781.22959
35127241.14396586.9009161516.157523436.1895
362158.372735916.3219273219.770512351.75995
3725109.09861788.68822131192.4239120198.5529
38139499.68852450.3157495421.840935397.5617
39138408.14838415.9738339560.647332506.6696
40261466.344794.75602368.87761421248.356
Sheet3
Cell Formulas
RangeFormula
B10,R22,N17,N19,N21,N11,K11,H10,E10B10=FORMULATEXT(B11)
B11:C157B11=CK(100,300,1)
E11:F157E11=CK(100,500,1)
H11:I157H11=CK(100,700,1)
K12:L157K12=MK(100,,1)
N12:O12N12=MK(100)
N18:O18N18=CK(100,300)
N20:O20N20=CK(100,500)
N22:O22N22=CK(100,700)
R23R23=O12-O20
Dynamic array formulas.
Named Ranges
NameRefers ToCells
pl=Sheet1!$B$2:$D$147B11, E11, H11, K12, N12, N18, N20, N22

Book2.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1
2Main functions:
3 MC([ s ],[mc],[cl]) MC, Minimum/Cluster function, based on its arguments, can trigger MK or CK to iterate for all places ,( all k's , or for entire sequence "s" )
4Final results:
5CK best route for k=15
6MC triggering MKMC triggering CKMK' best route for k=77as we see real st.point =61
7s,mc.cl all omitteds,omitted,mc,1,cl,300MK routecheck=CK(15,300,1)checking using DS, consec distances function
8=MC()=MC(,1,300)=MK(77,,1)=MK(77)1583274.89=DS(N9:N154,1)
97787570.871583274.897707787570.8761083274.89
1010987582.82284635.8910946.1490973219.7705
1110387619.99884635.897114.7883131192.4239Starting points of both methods MK/CK
121887726.110784635.892489.6574295421.8409k
134787733.628484793.468415.9738339560.647377Milford Sound, The South Island of New Zealand87570.87
146287761.32484816.371757.29867368.8776161Lake Bucura, Retezat Mountains, Romania83274.89best
151787808.29784921.295988.6882250148.2438
166587855.177784983.036516.32192136116.8656
1714087938.3810985026.519286.9009112254.59811Conclusions/Notes:
189287940.265785055.671404.3138112781.22959 - When I saw Bill's cluster idea, I knew that CSCAN is a different original approach to solve it.
197587957.442085151.0886169.701211449.2174 - As long that all modules are defined a simple call of a function does everything, for any distrib of places.
20987963.375485151.089126.037514133.05862 - Did not test many cluster sizes, could be better results than 300km radius, just stopped as soon I get a result lower than 85000 😊
214087964.188885151.08114236.995423326.0616 - There are more methods to solve this, one of them using brute random combinations but the sample is too big.
2211488023.4311285151.08981240.27512351.75995For small data sets can be very useful and does not fail, I use it for different scenarios.
238688063.5212785151.08101408.5611120198.5529After 10M iterations the smallest DS I got was 500k km. Top iterations we can do in excel is around 53M (million) but
243588275.3613985151.08182571.55935397.5617still not even a fraction of close, for this case.
255988361.6112485343.211031185.08132506.6696 - Depending on background calculations MC for CK can take over 2min, rest of functions are fast.
268488396.221385358.531181335.456421248.356 - Did not expect best starting point not to be an outer place, power of algorithms and iterations can surprise us.
272488403.210285668.4671529.213121342.5012 - Not extensively tested for glitches or other bugs, did it on a Sunday afternoon, the results check out fine.
28788520.4113285692.251431159.49117292.1041
2911888761.449585867.57146370.8181144149.3997
30288816.41185923.2651700.220729948.1424
3110488929.16586042.7340858.5219621350.888
325888931.584086042.7341629.1726110675.0347
333188952.095186042.7385664.662393990.7464
3410189283.66086042.731291666.47160469.1876
355189517.566286042.73751877.8840970.5594
3612689543.487586042.731391272.68341629.1726
379889593.329386042.7387103.752185664.6623
3812889712.7314486042.73127241.14391291666.471
3910689734.52686065.452158.37273751877.88
406789850.917486065.4525109.09861391272.683
Sheet5
Cell Formulas
RangeFormula
N7,Q8,K8,H8,E8,B8N7=FORMULATEXT(N8)
N8:O154N8=CK(15,300,1)
B9:C154B9=MC()
E9:F154E9=MC(,1,300)
H9:I154H9=MK(77,,1)
K9:L9K9=MK(77)
Q9Q9=DS(N9:N154,1)
R13:R14R13=INDEX(pl,Q13:Q14,1)
S13S13=L9
S14S14=Q9
Dynamic array formulas.
Named Ranges
NameRefers ToCells
pl=Sheet1!$B$2:$D$147B9, E9, H9, K9, N8, Q9, R13
 
I love this analysis and stopping once you get below 85,000 with the best score of 83275.

A day ago, I set up a Solver model and ran it through solver with a higher solve time limit. It ran for an hour and somehow, Solver came up with a minimum distance of 76,326 miles.

I really figured that Solver would try your random model, but clearly, Solver is smarter than I thought. It appears to me that Solver managed to find small clusters that are 4, 3, 2, 2,3, 2, 3, locations big and then organized the clusters. Or did Solver actually come up with something better by dumb luck? I asked Solver for the optimization reports, but they won't produce them for a model with over 32 variables.

I will e-mail you the spreadsheet, but here is the sequence that solver came up with: (This is | delimited - use TEXTSPLIT to unwind: Great Wall of China, China|Gungnamji Pond, Republic of Korea|Mount Fuji, Japan|Uzon Caldera, Russia|Matanuska Glacier, Alaska|Wedgemount Lake, British Columbia, Canada|Mount Shuksan, North Cascades National Park|Tom **** & Harry Mountain, Oregon|Clear Lake, Oregon|Crater Lake, Oregon|Sonora Peak, California|Mirror Lake, Yosemite National Park, California|Taft Point, Yosemite National Park, California|Vasquez Rocks Natural Area Park, California|Racetrack Playa, Death Valley, California|Badwater Basin, Death Valley, California|Waterton Lake, Alberta, Canada|Palisades Beach, Grand Canyon National Park, Arizona|Lake Powell, Hite Overlook|Lake Powell, Escalante River|Monument Valley Navajo Tribal Park, Arizona|Warren Canyon, Utah|Abajo Peak, Utah|Maroon Lake, Aspen, Colorado|Mirror Lake, Wyoming|Dunas De Bilbao, Mexico|White Sands, New Mexico|Playa El Requesón, Mexico|Motu Rimatiai, Tetiaroa|Matavai Bay, Tahiti, French Polynesia|Ahu Tongariki, Rapa Nui (Easter Island)|Isabela Island, Galápagos|Chichen Itza, Yucatán State, Mexico|Parque Nacional Palo Verde, Costa Rica|Crego Park Lake, Lansing, Michigan|Mural de la Prehistoria, Viñales, Cuba|Islote Sucre (Johnny Cay), Colombia|Machu Picchu, Peru|Vinicunca, Peru (Rainbow Mountain)|Atacama Desert, Chile|Laguna Santa Rosa, Nevado Tres Cruces National Park, Chile|El Chaltén, Los Glaciares National Park, Argentina|Rio De Las Vueltas, Argentina|Laguna Torre, Santa Cruz Province, Argentina|Salar de Uyuni, Bolivia|Cayo Muerto, Venezuela|Lencois Maranhenses, Brazil|Namib Desert, Namibia|Cape of Good Hope, South Africa|Golden Gate Highlands, South Africa|Bell Park Pier, South Africa|Mafia Island Marine Park, Tanzania|Erta Ale Volcano, Ethiopia|Forgotten Pyramids, Meroë, Sudan|The Great Temple of Ramesses II, Abu Simbel, Egypt|The Temple of Horus at Edfu, Egypt|Court of Amenhotep III, Luxor Temple, Egypt|Wadi Rum, Jordan|The Monastery Ad Deir, Petra|Dead Sea, Wadi Mujib, Jordan|Jebel El Tih, Egypt|Funerary Complex of Djoser, Egypt|Baharyia Oasis Salt Lake, Eqypt|White Desert National Park (Sahara el Beyda), Egypt|Pamukkale, Turkey|Malyovishki Lakes, Bulgaria|Varlaam Monastery, Meteora, Greece|Sahara Desert, Algeria|Great Pyramid Of Giza, Egypt|Deryouk Plain, Iran|Harireh Historical City, Kish, Iran|The Lut Desert, Iran|Tang Mud Volcano, Iran|Yenkit Beach, Oman|Lake Bucura, Retezat Mountains, Romania|Monte Piana, Italy|The Dolomites, Italy|Hörnle Mountain, Germany|Alblittkopf Mountain, Austria|Julier Pass, Switzerland|Mittelgipfel, Bernese Alps, Switzerland|Col Du Lautaret, Hautes-Alpes, France|Lac Lérié, Plateau d'Emparis, France|Dent de Crolles from La Pravouta, France|Aiguestortes National Park, Spain|Lac de Peyrelade, Hautes-Pyrénées, France|Port Esportiu de Calafat, Spain|Cala Tuent Mallorca, Spain|Todra Gorge, Morocco|Ursa Beach, Portugal|Llyn Idwall, Wales, United Kingdom|High Cup Nick, England|Loch Grannoch, Scotland|Glencoe, Scotland|Loch Coruisk, Scotland|Isle of Skye, Scotland|Vestrahorn, Iceland|Skaftafell Glacier, Iceland|Jokulsarlon Glacier Lagoon, Iceland|Virkisfell, Iceland|Hverfjall Volcano Crater, Iceland|Thorsmork, South Highlands of Iceland|Þingvellir (Thingvellir) National Park, Iceland|Blue Lagoon, Iceland|Snæfellsjökull Volcano, Iceland|Kulusuk, Greenland|Ililissat Icefjord - Greenland|Eysturoy, Faroe Islands|Finnøya, Norway|Ytresand Beach, Norway|Sennesvik, Norway|Andenes, Norway|Nordenskjold Glacier, Svalbard|Nuorgam, Finland|Lake Kel Suu, Kyrgyzstan|Taj Mahal, Yamuna River, India|Vabbinfaru Island, North Malé Atoll, Republic of Maldives|Ameenpur, Telangana, India|Sibinskie Lakes, Kazakhstan|Path to Everest, Nepal, Himalayas|Rongbuk Glacier, Mt. Everest, Tibet|Lhoknga Beach, Indonesia|Yuanyang Rice Terraces, China|Jiuzhaigou Valley, China|Orkhon Valley, Mongolia|Lake Baikal Sunset, Russia|Xe Bang Fai River, Laos|Tengger Caldera Volcano, Indonesia|Cathedral Gorge, Australia|Raja Ampat Island, New Guinea|Port Olry, Vanuatu|Plage de Ouano - New Caledonia|South Piha Beach, New Zealand|Auroa Point, New Zealand|Mount Ruapehu, New Zealand|Wharariki Beach, Nelson, New Zealand|Onetahua, New Zealand (Farewell Spit)|Lake Rotoiti, New Zealand|Lake Angelus, New Zealand|Castle Hill, New Zealand|Craigieburn Range, Southern Alps, New Zealand|Mount Binser, Arthur's Pass National Park, New Zealand|Aoraki / Mount Cook, Southern Alps, New Zealand|Shotover River, Queenstown, New Zealand|Milford Sound, The South Island of New Zealand|Cape Evans, Ross Island, Antarctica

Solver's solution is shown in the X-Y chart. The red line segment between points 4 and 5 would really jump across the international date line from Russia to Alaska.

It is pretty strange sitting here, admitting that I was beaten fairly soundly by Solver, but with no idea how Solver managed to solve this.
 

Attachments

  • WeekTwo10.png
    WeekTwo10.png
    21.4 KB · Views: 8

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top