In [1]:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import sqlite3
import json
import pandas as pd
import importlib
from IPython.core.display import display, HTML

import os
os.chdir("./shared_code")
import RHgenerate_states
import RHcomponents
import RHdisplay
import RHdistance_partition
import RHutilities

%matplotlib inline

In [4]:
#conn = sqlite3.connect('database\\rush_hour.db')
#cur = conn.cursor()

In [2]:
comb_class = RHgenerate_states.generate_states(4,2)

In [3]:
len(comb_class),comb_class[23]


Out[3]:
(18763346, (278158934713361378359121765203968, 1))

In [ ]:
importlib.reload(RHcomponents)
comps = RHcomponents.components(comb_class)

In [11]:
len(comps),sum(len(x.nodes()) for x in comps)


Out[11]:
(1361, 116650)

In [12]:
len(comps[0].nodes())


Out[12]:
308

In [14]:
solvable_comps = [g for g in comps if g.graph['solvable']==True]
unsolvable_comps = [g for g in comps if g.graph['solvable'] == False]

In [15]:
df_solvable = pd.DataFrame([len(x.nodes()) for x in solvable_comps])
df_unsolvable = pd.DataFrame([len(x.nodes()) for x in unsolvable_comps])

Here -

  • Select representative for each component
  • save json to file for each component (class_2_2_comp_39823484994949)
  • draw graph with d3

  • What I really want is real time back and forth to pull out components on the fly rather than build them all and save mass files to harddrive.


In [18]:
#df.describe()
#%matplotlib inline
df_solvable.hist(bins=20)


Out[18]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000250BD956EB8>]], dtype=object)

In [19]:
df_unsolvable.hist(bins=20)


Out[19]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000250BC07E9B0>]], dtype=object)

In [82]:
comp = comps[0]
min_int = min(comp.node[node]['board_int'] for node in comp.nodes())
min_int


Out[82]:
632587360075802098027458330624

In [16]:
df_solvable.describe()


Out[16]:
0
count 1024.000000
mean 107.013672
std 100.871674
min 1.000000
25% 20.000000
50% 60.000000
75% 178.000000
max 400.000000

In [17]:
df_unsolvable.describe()


Out[17]:
0
count 337.000000
mean 20.973294
std 20.752708
min 1.000000
25% 6.000000
50% 15.000000
75% 27.000000
max 96.000000

In [83]:
#node = 0
comp_repr_node = [comp[node] for node in comp.nodes() if comp.node[node]['board_int'] == min_int]
comp_repr_node


Out[83]:
[AtlasView({233: {}, 270: {}, 231: {}, 284: {}})]

In [113]:
len([g for g in comps if len(g.nodes())==1])


Out[113]:
18

In [37]:
n = solvable_comps[40].node[0]

#HTML(RHdisplay.svg_from_state(solvable_comps[40].nodes(0)))

HTML(RHdisplay.svg_from_state(n['board_int'],n['red_col']))


Out[37]:
XLZW

In [39]:
df = pd.DataFrame( [  [len(g.nodes()),g.graph['solvable'] ] for g in comps])

In [55]:
g = solvable_comps[40]
g


Out[55]:
<networkx.classes.graph.Graph at 0x250b11594a8>

In [58]:
RHdistance_partition.distance_partition(g)


Out[58]:
{'board_int': 207885568651978971296788249575424,
 'inner_nbrs': {11, 49},
 'is_soln_state': False,
 'outer_nbrs': {9},
 'red_col': 2,
 'soln_distance': 4}

In [72]:
df_dist = pd.DataFrame( [g.node[n]['soln_distance'] for n in g.nodes() ], columns = ['distance'])

In [74]:
df_dist.groupby(['distance']).size()


Out[74]:
distance
0    41
1    41
2    67
3    71
4    41
5    11
6     1
dtype: int64

In [76]:
HTML(RHdisplay.svg_from_state( g.node[0]['board_int'] , g.node[0]['red_col']))


Out[76]:
XLZW

In [77]:
len(solvable_comps) , len(unsolvable_comps)


Out[77]:
(1024, 337)

In [79]:
len(solvable_comps) + len(unsolvable_comps)


Out[79]:
1361

In [85]:
importlib.reload(RHdistance_partition)
for g in solvable_comps:
    RHdistance_partition.distance_partition(g)

In [142]:
for g in solvable_comps:
    g.graph['max_distance'] = max(g.graph['distance_partition'].keys())
    for node in g.graph['distance_partition'][g.graph['max_distance']]:
        break
    node_dict = g.node[node]
    split_board_int = RHutilities.split_int( node_dict['board_int'])
    g.graph['repr_board_int_s1'] = split_board_int[0]
    g.graph['repr_board_int_s2'] = split_board_int[1]
    g.graph['repr_red_col'] = node_dict['red_col']

In [162]:
df_max_dist = pd.DataFrame( \
            [ [g.graph['max_distance'],\
               g.graph['repr_board_int_s1'],\
               g.graph['repr_board_int_s2'],\
               g.graph['repr_red_col']\
               ,len(g.nodes())\
               ,g.size()\
               ,nx.density(g)\
              ]\
               for g in solvable_comps], columns=['distance','int_s1','int_s2','repr_red_col','num_nodes','num_edges','density'])

In [163]:
df_max_dist.sort_values(['distance'],ascending=False)


Out[163]:
distance int_s1 int_s2 repr_red_col num_nodes num_edges density
168 19 2748789558696 179633383279104 3 56 86 0.055844
769 17 2748789558696 175922545691136 3 80 149 0.047152
642 17 343598694837 25701168185664 4 116 260 0.038981
739 16 2748789776424 25701168185664 1 102 228 0.044263
830 15 2748789558696 670828544 3 84 172 0.049340
870 15 343598694837 21990330597696 4 144 344 0.033411
387 15 2748789558696 175922531535414 3 104 214 0.039955
572 15 21990316469568 670828544 2 86 161 0.044049
408 14 2748789776424 21990330597696 1 126 299 0.037968
506 14 175922531756544 68586307588 1 112 231 0.037162
455 14 175922531756544 68652368128 1 86 161 0.044049
765 14 175922531756544 68593647648 1 64 117 0.058036
505 13 22333915357509 54 1 172 432 0.029376
402 13 24739106218344 54 1 154 388 0.032934
20 12 197912848198464 237494511599616 1 102 164 0.031838
323 12 3092388470829 3710851743744 1 174 476 0.031626
795 12 175922531756800 17996806323437568 1 66 92 0.042890
88 12 197912848198464 905969664 1 126 234 0.029714
1 12 21990316663104 34493825024 1 112 231 0.037162
135 12 3092388470829 54 1 222 634 0.025845
693 12 197912848198464 3456 1 154 320 0.027162
532 12 21990316665152 175853140967424 1 66 92 0.042890
759 12 3092388470829 14155776 1 195 544 0.028760
508 12 175922531756548 17979489015300096 1 77 121 0.041353
776 12 175922531756576 17981413160648704 1 63 89 0.045571
149 11 21990316663136 2199023256063 1 159 378 0.030093
344 11 175922531756544 8650272 1 130 270 0.032200
637 11 21990316663104 1161019854422016 1 63 89 0.045571
345 11 24739643091304 0 1 165 388 0.028677
330 11 2748789776424 133955638 1 180 492 0.030540
... ... ... ... ... ... ... ...
1012 0 21990316441974 343665803525 5 3 2 0.666667
436 0 21990316703606 17592253153280 5 3 2 0.666667
1016 0 21990330597750 21990316441920 5 1 0 0.000000
468 0 21990316605814 11276634271252480 5 12 17 0.257576
815 0 21990316444534 193514784686080 5 12 17 0.257576
1018 0 15221639058817398 21990316441920 5 2 1 1.000000
1019 0 1407380252286390 1407380252282880 5 1 0 0.000000
1020 0 21990316441974 21990324830560 5 2 1 1.000000
587 0 21990316441974 22024676311360 5 5 4 0.400000
1022 0 21990316441974 21990330597696 5 1 0 0.000000
892 0 21990316441974 21990317490500 5 2 1 1.000000
1004 0 21990316441974 21990316469568 5 2 1 1.000000
738 0 21990316441974 21990853314880 5 5 4 0.400000
944 0 24189348086134 21990316441920 5 1 0 0.000000
957 0 21990316441974 17592253414912 5 6 5 0.333333
930 0 25701168185718 21990316441920 5 1 0 0.000000
931 0 22048298500470 21990316441920 5 2 1 1.000000
992 0 18001204453835126 17592253153280 5 6 5 0.333333
993 0 21990316441974 17996806390546432 5 6 5 0.333333
994 0 21990316441974 17660838412288 5 3 2 0.666667
995 0 21990316441974 15480573963272192 5 3 2 0.666667
932 0 21990316441974 15221639058817344 5 2 1 1.000000
800 0 11259042018295798 34359869440 5 3 2 0.666667
933 0 21990316441974 1147894518251840 5 5 4 0.400000
821 0 22058901700982 17592253153280 5 3 2 0.666667
1000 0 1429370568724854 67109120 5 12 17 0.257576
1001 0 21990316441974 25701168185664 5 1 0 0.000000
1002 0 21990316441974 22048298500416 5 2 1 1.000000
1003 0 21990316441974 21990316441974 5 1 0 0.000000
997 0 21990316441974 20341042708520 5 3 2 0.666667

1024 rows × 7 columns


In [164]:
board_int = RHutilities.combine_ints(21990316605814,11276634271252480)
red_col = 5
HTML(RHdisplay.svg_from_state(board_int,red_col))


Out[164]:
XLZW

In [166]:
df_max_dist.groupby(['distance']).agg(['count'])


Out[166]:
int_s1 int_s2 repr_red_col num_nodes num_edges density
count count count count count count
distance
0 34 34 34 34 34 34
1 165 165 165 165 165 165
2 73 73 73 73 73 73
3 18 18 18 18 18 18
4 221 221 221 221 221 221
5 93 93 93 93 93 93
6 55 55 55 55 55 55
7 72 72 72 72 72 72
8 110 110 110 110 110 110
9 53 53 53 53 53 53
10 55 55 55 55 55 55
11 50 50 50 50 50 50
12 11 11 11 11 11 11
13 2 2 2 2 2 2
14 4 4 4 4 4 4
15 4 4 4 4 4 4
16 1 1 1 1 1 1
17 2 2 2 2 2 2
19 1 1 1 1 1 1

In [119]:
df_max_dist.sort_values(by=['distance'] , ascending = False)


Out[119]:
distance int_s1 int_s2 repr_red_col
168 19 175922531535792 240243301154856 4
769 17 2748789558696 175922644781568 3
642 17 343598722053 25701168185664 2
739 16 28008 1924695198793728 2
830 15 140738035933224 2749850714112 1
870 15 27968 22341162893317 2
387 15 671312424 178670650027008 1
572 15 83886454 162728391737344 5
408 14 221504 24746353754152 1
506 14 175922531756544 343464214528 1
455 14 17592253155894 175922665488384 5
765 14 178121563179574 1071644672 5
505 13 27648 22333915163973 2
402 13 2748789776424 21990316441974 1
20 12 197912848198464 29686813949952 1
323 12 343609401389 1902704871866368 1
795 12 67330304 211038184147456 1
88 12 2934 197970830032896 5
1 12 9007233614700544 22058901700928 1
135 12 2748789558696 343598694837 3
693 12 374 197912847977014 5
532 12 21990316441974 17979214674266112 5
759 12 1311157 3093292908584 4
508 12 175922531756544 17979489016348672 1
776 12 175922531756576 17981413160648704 1
149 11 27968 24189348089848 2
344 11 175922531535792 8650272 4
637 11 1125904201810294 18001204453834752 5
345 11 140738025226672 24739105997160 4
330 11 432 2817374841896 4
... ... ... ... ...
1012 0 21990316441974 343665803525 5
436 0 21990316703606 17592253153280 5
1016 0 21990330597750 21990316441920 5
468 0 21990316605814 11276634271252480 5
815 0 21990316444534 193514784686080 5
1018 0 15221639058817398 21990316441920 5
1019 0 1407380252286390 1407380252282880 5
1020 0 21990316441974 21990324830560 5
587 0 21990316441974 22024676311360 5
1022 0 21990316441974 21990330597696 5
892 0 21990316441974 21990317490500 5
1004 0 21990316441974 21990316469568 5
738 0 21990316441974 21990853314880 5
944 0 24189348086134 21990316441920 5
957 0 21990316441974 17592253414912 5
930 0 25701168185718 21990316441920 5
931 0 22048298500470 21990316441920 5
992 0 18001204453835126 17592253153280 5
993 0 21990316441974 17996806390546432 5
994 0 21990316441974 17660838412288 5
995 0 21990316441974 15480573963272192 5
932 0 21990316441974 15221639058817344 5
800 0 11259042018295798 34359869440 5
933 0 21990316441974 1147894518251840 5
821 0 22058901700982 17592253153280 5
1000 0 1429370568724854 67109120 5
1001 0 21990316441974 25701168185664 5
1002 0 21990316441974 22048298500416 5
1003 0 21990316441974 21990316441974 5
997 0 21990316441974 20341042708520 5

1024 rows × 4 columns


In [135]:
df_max_dist.loc[df_max_dist['distance'] == 19]
#HTML(RHdisplay.svg_from_state(n['board_int'],n['red_col']))


Out[135]:
distance int_s1 int_s2 repr_red_col
168 19 175922531535792 240243301154856 4

In [137]:
board_int = RHutilities.combine_ints(2748789558696,240243301154856)
red_col = 4
HTML(RHdisplay.svg_from_state(board_int,red_col))


Out[137]:
XLZW

In [139]:



Out[139]:
{0: {0}, 1: {1}}

In [ ]: