Author Topic: Quick Basic Efficiency Assistance  (Read 62 times)

Offline Reyth

  • Contributor
  • Posts: 2
Quick Basic Efficiency Assistance
« on: February 14, 2017, 09:21:45 PM »
5 DIM trp(18000, 6): PRINT TIME$
10 RANDOMIZE TIMER
20 r = INT(RND * 12) + 1: s = INT(RND * 12) + 1: t = INT(RND * 12) + 1
30 u = INT(RND * 12) + 1: v = INT(RND * 12) + 1: w = INT(RND * 12) + 1

40 IF r = s OR r = t OR r = u OR r = v OR r = w THEN 20
50 IF s = t OR s = u OR s = v OR s = w THEN 20
60 IF t = u OR t = v OR t = w THEN 20
70 IF u = v OR u = w THEN 20
80 IF v = w THEN 20

90 ef = 0: FOR i = 1 TO 18000: IF ef <> 0 THEN GOTO 110 'advance loop _LIMIT (4096000):
 100 IF trp(i, 1) = 0 THEN ef = i
110 NEXT i

120 IF ef = 0 THEN 140 'process existing selections for opposites

130 trp(ef, 1) = r: trp(ef, 2) = s: trp(ef, 3) = t: trp(ef, 4) = u: trp(ef, 5) = v: trp(ef, 6) = w: GOTO 20

140 'process selections for duplicates
150 c = 0: dup = 0: LOCATE 2, 1: PRINT "A"; TIME$: FOR i = cl TO 17999: FOR j = 1 TO 18000: ' _LIMIT (16384000)
  155 IF i = j OR c <> 0 THEN 235
  160 IF trp(i, 1) = trp(j, 1) OR trp(i, 1) = trp(j, 2) OR trp(i, 1) = trp(j, 3) OR trp(i, 1) = trp(j, 4) OR trp(i, 1) = trp(j, 5) OR trp(i, 1) = trp(j, 6) THEN dup = dup + 1
  170 IF trp(i, 2) = trp(j, 1) OR trp(i, 2) = trp(j, 2) OR trp(i, 2) = trp(j, 3) OR trp(i, 2) = trp(j, 4) OR trp(i, 2) = trp(j, 5) OR trp(i, 2) = trp(j, 6) THEN dup = dup + 1
  180 IF trp(i, 3) = trp(j, 1) OR trp(i, 3) = trp(j, 2) OR trp(i, 3) = trp(j, 3) OR trp(i, 3) = trp(j, 4) OR trp(i, 3) = trp(j, 5) OR trp(i, 3) = trp(j, 6) THEN dup = dup + 1
  190 IF trp(i, 4) = trp(j, 1) OR trp(i, 4) = trp(j, 2) OR trp(i, 4) = trp(j, 3) OR trp(i, 4) = trp(j, 4) OR trp(i, 4) = trp(j, 5) OR trp(i, 4) = trp(j, 6) THEN dup = dup + 1
  200 IF trp(i, 5) = trp(j, 1) OR trp(i, 5) = trp(j, 2) OR trp(i, 5) = trp(j, 3) OR trp(i, 5) = trp(j, 4) OR trp(i, 5) = trp(j, 5) OR trp(i, 5) = trp(j, 6) THEN dup = dup + 1
  210 IF trp(i, 6) = trp(j, 1) OR trp(i, 6) = trp(j, 2) OR trp(i, 6) = trp(j, 3) OR trp(i, 6) = trp(j, 4) OR trp(i, 6) = trp(j, 5) OR trp(i, 6) = trp(j, 6) THEN dup = dup + 1
  230 IF dup = 6 THEN trp(j, 1) = 0: c = i: cl = i - 1: IF cl < 1 THEN cl = 1
235 dup = 0: NEXT j, i: IF c <> 0 THEN LOCATE 3, 1: PRINT "                  ": LOCATE 3, 1: PRINT c; TIME$: GOTO 20
237 cl = 1


240 'process selections for opposites
250 sf = 0: LOCATE 3, 1: PRINT "B"; TIME$: FOR i = cl TO 17999: FOR j = i TO 18000: ' _LIMIT (16384000)
  255 IF c <> 0 THEN 330
  260 IF trp(i, 1) = trp(j, 1) OR trp(i, 1) = trp(j, 2) OR trp(i, 1) = trp(j, 3) OR trp(i, 1) = trp(j, 4) OR trp(i, 1) = trp(j, 5) OR trp(i, 1) = trp(j, 6) THEN 330 'advance loop
  270 IF trp(i, 2) = trp(j, 1) OR trp(i, 2) = trp(j, 2) OR trp(i, 2) = trp(j, 3) OR trp(i, 2) = trp(j, 4) OR trp(i, 2) = trp(j, 5) OR trp(i, 2) = trp(j, 6) THEN 330 'advance loop
  280 IF trp(i, 3) = trp(j, 1) OR trp(i, 3) = trp(j, 2) OR trp(i, 3) = trp(j, 3) OR trp(i, 3) = trp(j, 4) OR trp(i, 3) = trp(j, 5) OR trp(i, 3) = trp(j, 6) THEN 230 'advance loop
  290 IF trp(i, 4) = trp(j, 1) OR trp(i, 4) = trp(j, 2) OR trp(i, 4) = trp(j, 3) OR trp(i, 4) = trp(j, 4) OR trp(i, 4) = trp(j, 5) OR trp(i, 4) = trp(j, 6) THEN 330 'advance loop
  300 IF trp(i, 5) = trp(j, 1) OR trp(i, 5) = trp(j, 2) OR trp(i, 5) = trp(j, 3) OR trp(i, 5) = trp(j, 4) OR trp(i, 5) = trp(j, 5) OR trp(i, 5) = trp(j, 6) THEN 330 'advance loop
  310 IF trp(i, 6) = trp(j, 1) OR trp(i, 6) = trp(j, 2) OR trp(i, 6) = trp(j, 3) OR trp(i, 6) = trp(j, 4) OR trp(i, 6) = trp(j, 5) OR trp(i, 6) = trp(j, 6) THEN 330 'advance loop
  320 trp(j, 1) = 0: sf = 1: c = j: cl = i
330 NEXT j, i
340 IF sf = 1 OR c > 0 THEN LOCATE 4, 1: PRINT c; TIME$: GOTO 20

350 'ouptut selections
355 OPEN "Random_Streets.txt" FOR OUTPUT AS #1
360 FOR i = 1 TO 18000
 370 PRINT #1, trp(i, 1); trp(i, 2); trp(i, 3); trp(i, 4); trp(i, 5); trp(i, 6)
380 NEXT i: PRINT TIME$: CLOSE #1
INPUT a$


The above code will randomize 180000 sets of 6 digits ranging from 1 to 12, removing all duplicates and complete opposites from the list.  I have gotten to the point where I would like more of these combinations and it is taking over 8 hours for the "de-duplicating" process to complete.

This is where I need help in simply making this code as efficient as possible so as to cut the time down to the maximum for its completion.

I am willing to accept a complete rewrite if the following is maintained:

1) The resulting sets of 6 numbers contain no duplicates: a) within themselves (AABCDE, BBACDE etc.), b) in any other element (AAAAAB and BAAAAA cannot both exist in the final list), c) no exact opposite duplication (1 2 3 4 5 6 and 7 8 9 10 11 12 are exact opposites and cannot exist together in the final list)

2) A simple notepad text file (.txt) is generated with all 180000 sets of numbers 6 to a line with no special characters at all

3) Compatibility with the QB64 (Windows Quick Basic) is maintained in the code

Of course I am able to accept workarounds with my existing code as well.

Thanks in advance for your help!

PS My apologies for using multiple IF...THEN statements instead of an additional FOR...NEXT loop but I wanted to make the logic as clear to myself as possible. :shrug:
« Last Edit: February 14, 2017, 09:29:40 PM by Reyth »

Offline Reyth

  • Contributor
  • Posts: 2
Re: Quick Basic Efficiency Assistance
« Reply #1 on: February 15, 2017, 07:35:02 AM »
I see this post was read 8 times.  I entered my hydro-powered meditation chamber this morning (the shower) thinking about this problem.  The solution instantly came to me!  Random was no longer my friend!  Why not just dispense with the array and output all combinations directly to a file?  I then reasoned at the computer that I could sort out the "shadows" by simply insuring that at least 1 number remains below 7.

Here is the code that resulted from my hydro meditation:

5 OPEN "6_Output_Test.txt" FOR OUTPUT AS #1
10 FOR i = 1 TO 12: FOR j = 1 TO 12: FOR k = 1 TO 12: FOR l = 1 TO 12: FOR m = 1 TO 12: FOR n = 1 TO 12

      20 IF i = j OR i = k OR i = l OR i = m OR i = n THEN 50
      21 IF j = k OR j = l OR j = m OR j = n THEN 50 'advance loop
      22 IF k = l OR k = m OR k = n THEN 50 'advance loop
      23 IF l = m OR l = n THEN 50 'advance loop
      24 IF m = n THEN 50 'advance loop

      40 PRINT #1, i, j, k, l, m, n: c = c + 1: LOCATE 1, 1: PRINT c

50 NEXT n, m, l, k, j, i: CLOSE #1

60 OPEN "6_output_Test.txt" FOR INPUT AS #1
70 OPEN "6_Output_Sort_Test.txt" FOR OUTPUT AS #2

80 FOR i = 1 TO 12: FOR j = 1 TO 12: FOR k = 1 TO 12: FOR l = 1 TO 12: FOR m = 1 TO 12: FOR n = 1 TO 12
      90 INPUT #1, a, b, c, d, e, f
      100 IF a < 7 OR b < 7 OR c < 7 OR d < 7 OR e < 7 OR f < 7 THEN PRINT #2, a, b, c, d, e, f: cc = cc + 1: LOCATE 1, 1: PRINT cc
110 NEXT n, m, l, k, j, i

WARNING: This code will generate a "read beyond file end" error upon completion.  Just ignore it and close out; all the data is in the file.

664561 duplicate free combinations are now in my possession!  I am ALL POWERFUL!!  MUWAHAHAAHAHAHAAHAHAAH!
« Last Edit: February 15, 2017, 08:00:57 AM by Reyth »