Dictionary Search for adjacent items with same features

Question:

I have a dictionary encoding x,y,z and color information for a finite Lego assembly like this "p_xx_yy": ['color', z]:

enter image description here

"myDict" : {
    'p_00_00':[ 'y', 1 ], 'p_00_01':[ 'y', 1 ], 'p_00_02':[ 'g', 0 ], 'p_00_03':[ 'w', 1 ], 'p_00_04':[ 'g', 0 ], 'p_00_05':[ 'o', 1 ], 'p_00_06':[ 'g', 0 ], 'p_00_07':[ 'g', 0 ], 'p_00_08':[ 'g', 0 ], 'p_00_09':[ 'g', 0 ], 'p_00_10':[ 'g', 0 ], 'p_00_11':[ 'g', 0 ], 'p_00_12':[ 'g', 0 ], 'p_00_13':[ 'g', 0 ], 'p_00_14':[ 'g', 0 ], 'p_00_15':[ 'g', 0 ], 'p_00_16':[ 'g', 0 ], 'p_00_17':[ 'g', 0 ], 'p_00_18':[ 'g', 0 ], 'p_00_19':[ 'w', 1 ], 'p_00_20':[ 'g', 0 ], 'p_00_21':[ 'y', 1 ], 'p_00_22':[ 'y', 1 ],
    'p_01_00':[ 'g', 0 ], 'p_01_01':[ 'g', 0 ], 'p_01_02':[ 'g', 0 ], 'p_01_03':[ 'g', 0 ], 'p_01_04':[ 'g', 0 ], 'p_01_05':[ 'g', 0 ], 'p_01_06':[ 'g', 0 ], 'p_01_07':[ 'g', 1 ], 'p_01_08':[ 'g', 0 ], 'p_01_09':[ 'g', 0 ], 'p_01_10':[ 'g', 0 ], 'p_01_11':[ 'g', 0 ], 'p_01_12':[ 'g', 0 ], 'p_01_13':[ 'g', 0 ], 'p_01_14':[ 'g', 0 ], 'p_01_15':[ 'g', 1 ], 'p_01_16':[ 'g', 0 ], 'p_01_17':[ 'g', 0 ], 'p_01_18':[ 'g', 0 ], 'p_01_19':[ 'g', 0 ], 'p_01_20':[ 'g', 0 ], 'p_01_21':[ 'g', 0 ], 'p_01_22':[ 'g', 0 ],
    'p_02_00':[ 'b', 1 ], 'p_02_01':[ 'b', 1 ], 'p_02_02':[ 'g', 0 ], 'p_02_03':[ 'b', 1 ], 'p_02_04':[ 'g', 0 ], 'p_02_05':[ 'g', 0 ], 'p_02_06':[ 'g', 0 ], 'p_02_07':[ 'g', 0 ], 'p_02_08':[ 'g', 0 ], 'p_02_09':[ 'g', 0 ], 'p_02_10':[ 'g', 0 ], 'p_02_11':[ 'g', 0 ], 'p_02_12':[ 'g', 0 ], 'p_02_13':[ 'g', 0 ], 'p_02_14':[ 'g', 0 ], 'p_02_15':[ 'g', 0 ], 'p_02_16':[ 'g', 0 ], 'p_02_17':[ 'g', 0 ], 'p_02_18':[ 'g', 0 ], 'p_02_19':[ 'b', 1 ], 'p_02_20':[ 'g', 0 ], 'p_02_21':[ 'b', 1 ], 'p_02_22':[ 'b', 1 ],
    'p_03_00':[ 'g', 0 ], 'p_03_01':[ 'g', 0 ], 'p_03_02':[ 'g', 0 ], 'p_03_03':[ 'g', 0 ], 'p_03_04':[ 'g', 0 ], 'p_03_05':[ 'g', 0 ], 'p_03_06':[ 'g', 0 ], 'p_03_07':[ 'g', 0 ], 'p_03_08':[ 'g', 0 ], 'p_03_09':[ 'g', 0 ], 'p_03_10':[ 'g', 0 ], 'p_03_11':[ 'g', 0 ], 'p_03_12':[ 'g', 0 ], 'p_03_13':[ 'g', 0 ], 'p_03_14':[ 'g', 0 ], 'p_03_15':[ 'g', 0 ], 'p_03_16':[ 'g', 0 ], 'p_03_17':[ 'g', 0 ], 'p_03_18':[ 'g', 0 ], 'p_03_19':[ 'g', 0 ], 'p_03_20':[ 'g', 0 ], 'p_03_21':[ 'g', 0 ], 'p_03_22':[ 'g', 0 ],
    'p_04_00':[ 'g', 0 ], 'p_04_01':[ 'r', 1 ], 'p_04_02':[ 'g', 0 ], 'p_04_03':[ 'g', 0 ], 'p_04_04':[ 'g', 0 ], 'p_04_05':[ 'g', 0 ], 'p_04_06':[ 'g', 0 ], 'p_04_07':[ 'g', 0 ], 'p_04_08':[ 'g', 0 ], 'p_04_09':[ 'g', 0 ], 'p_04_10':[ 'g', 0 ], 'p_04_11':[ 'g', 0 ], 'p_04_12':[ 'g', 0 ], 'p_04_13':[ 'g', 0 ], 'p_04_14':[ 'g', 0 ], 'p_04_15':[ 'g', 0 ], 'p_04_16':[ 'g', 0 ], 'p_04_17':[ 'g', 0 ], 'p_04_18':[ 'g', 0 ], 'p_04_19':[ 'g', 0 ], 'p_04_20':[ 'g', 0 ], 'p_04_21':[ 'r', 1 ], 'p_04_22':[ 'g', 0 ],
    'p_05_00':[ 'g', 0 ], 'p_05_01':[ 'g', 0 ], 'p_05_02':[ 'g', 0 ], 'p_05_03':[ 'g', 0 ], 'p_05_04':[ 'g', 0 ], 'p_05_05':[ 'g', 0 ], 'p_05_06':[ 'g', 0 ], 'p_05_07':[ 'g', 0 ], 'p_05_08':[ 'g', 0 ], 'p_05_09':[ 'g', 0 ], 'p_05_10':[ 'g', 0 ], 'p_05_11':[ 'g', 0 ], 'p_05_12':[ 'g', 0 ], 'p_05_13':[ 'g', 0 ], 'p_05_14':[ 'g', 0 ], 'p_05_15':[ 'g', 0 ], 'p_05_16':[ 'g', 0 ], 'p_05_17':[ 'g', 0 ], 'p_05_18':[ 'g', 0 ], 'p_05_19':[ 'g', 0 ], 'p_05_20':[ 'g', 0 ], 'p_05_21':[ 'g', 0 ], 'p_05_22':[ 'g', 0 ],
    'p_06_00':[ 'g', 0 ], 'p_06_01':[ 'g', 0 ], 'p_06_02':[ 'g', 0 ], 'p_06_03':[ 'g', 0 ], 'p_06_04':[ 'g', 0 ], 'p_06_05':[ 'g', 0 ], 'p_06_06':[ 'g', 0 ], 'p_06_07':[ 'g', 0 ], 'p_06_08':[ 'g', 0 ], 'p_06_09':[ 'g', 0 ], 'p_06_10':[ 'g', 0 ], 'p_06_11':[ 'g', 0 ], 'p_06_12':[ 'g', 0 ], 'p_06_13':[ 'g', 0 ], 'p_06_14':[ 'g', 0 ], 'p_06_15':[ 'g', 0 ], 'p_06_16':[ 'g', 0 ], 'p_06_17':[ 'g', 0 ], 'p_06_18':[ 'g', 0 ], 'p_06_19':[ 'g', 0 ], 'p_06_20':[ 'g', 0 ], 'p_06_21':[ 'g', 0 ], 'p_06_22':[ 'g', 0 ],
    'p_07_00':[ 'g', 0 ], 'p_07_01':[ 'g', 0 ], 'p_07_02':[ 'g', 0 ], 'p_07_03':[ 'g', 0 ], 'p_07_04':[ 'g', 0 ], 'p_07_05':[ 'g', 0 ], 'p_07_06':[ 'g', 0 ], 'p_07_07':[ 'g', 1 ], 'p_07_08':[ 'g', 0 ], 'p_07_09':[ 'g', 0 ], 'p_07_10':[ 'g', 0 ], 'p_07_11':[ 'g', 0 ], 'p_07_12':[ 'g', 0 ], 'p_07_13':[ 'g', 0 ], 'p_07_14':[ 'g', 0 ], 'p_07_15':[ 'g', 1 ], 'p_07_16':[ 'g', 0 ], 'p_07_17':[ 'g', 0 ], 'p_07_18':[ 'g', 0 ], 'p_07_19':[ 'g', 0 ], 'p_07_20':[ 'g', 0 ], 'p_07_21':[ 'g', 0 ], 'p_07_22':[ 'g', 0 ],
    'p_08_00':[ 'g', 0 ], 'p_08_01':[ 'g', 0 ], 'p_08_02':[ 'g', 0 ], 'p_08_03':[ 'g', 0 ], 'p_08_04':[ 'g', 0 ], 'p_08_05':[ 'g', 0 ], 'p_08_06':[ 'g', 0 ], 'p_08_07':[ 'w', 1 ], 'p_08_08':[ 'g', 0 ], 'p_08_09':[ 'r', 1 ], 'p_08_10':[ 'r', 1 ], 'p_08_11':[ 'g', 0 ], 'p_08_12':[ 'y', 1 ], 'p_08_13':[ 'y', 1 ], 'p_08_14':[ 'g', 0 ], 'p_08_15':[ 'l', 1 ], 'p_08_16':[ 'g', 0 ], 'p_08_17':[ 'o', 1 ], 'p_08_18':[ 'g', 0 ], 'p_08_19':[ 'g', 0 ], 'p_08_20':[ 'g', 0 ], 'p_08_21':[ 'g', 0 ], 'p_08_22':[ 'g', 0 ],
    'p_09_00':[ 'g', 0 ], 'p_09_01':[ 'g', 0 ], 'p_09_02':[ 'g', 0 ], 'p_09_03':[ 'g', 0 ], 'p_09_04':[ 'g', 0 ], 'p_09_05':[ 'g', 0 ], 'p_09_06':[ 'g', 0 ], 'p_09_07':[ 'w', 1 ], 'p_09_08':[ 'g', 0 ], 'p_09_09':[ 'r', 1 ], 'p_09_10':[ 'r', 1 ], 'p_09_11':[ 'g', 0 ], 'p_09_12':[ 'y', 1 ], 'p_09_13':[ 'y', 1 ], 'p_09_14':[ 'g', 0 ], 'p_09_15':[ 'l', 1 ], 'p_09_16':[ 'g', 0 ], 'p_09_17':[ 'g', 0 ], 'p_09_18':[ 'g', 0 ], 'p_09_19':[ 'g', 0 ], 'p_09_20':[ 'g', 0 ], 'p_09_21':[ 'g', 0 ], 'p_09_22':[ 'g', 0 ],
    'p_10_00':[ 'g', 0 ], 'p_10_01':[ 'g', 0 ], 'p_10_02':[ 'g', 0 ], 'p_10_03':[ 'g', 0 ], 'p_10_04':[ 'g', 0 ], 'p_10_05':[ 'g', 0 ], 'p_10_06':[ 'g', 0 ], 'p_10_07':[ 'w', 1 ], 'p_10_08':[ 'g', 0 ], 'p_10_09':[ 'r', 1 ], 'p_10_10':[ 'g', 0 ], 'p_10_11':[ 'g', 0 ], 'p_10_12':[ 'y', 1 ], 'p_10_13':[ 'g', 0 ], 'p_10_14':[ 'g', 0 ], 'p_10_15':[ 'g', 0 ], 'p_10_16':[ 'g', 0 ], 'p_10_17':[ 'g', 0 ], 'p_10_18':[ 'g', 0 ], 'p_10_19':[ 'g', 0 ], 'p_10_20':[ 'g', 0 ], 'p_10_21':[ 'g', 0 ], 'p_10_22':[ 'g', 0 ]
}

and I want to locate and return the x, y, z information of a specific lego cube/brick knowing that no brick and cube of the same color and height z will be adjacent.

The lego names are encoded with color, type (c: cube, b: brick), position/s p_xx_yy and z, like this:

"oc3":["p_08_17", 1], 
"yb1":["p_00_00", "p_00_01", 1], 
"yb2":["p_00_21", "p_00_22", 1], 

the first letter indicates the color: o, y for example for olive and yellow, while the second letter indicates if it is a 2x2 lego cube or 2x4 lego brick.

enter image description here

I want to locate this finite set of blocks/bricks in the dictionary above after each pick-place operation, to do so I have created the following function:

def key2pos(s):
    o = s.split('_')
    return [int(o[1]), int(o[2])]

def getNeighbour(lego, placed):
    c0 = lego[0]
    # cube
    if(lego[1]=='c'):

        for pos_ in placed:
            c = placed[pos_][0] 
            if(c == c0):
                return pos_

    # Brick
    elif(lego[1]=='b'):
        pos1 = None
        pos2 = None
        for pos_ in placed:
            x,y = key2pos(pos_)
            z = placed[pos_][1]
            c = placed[pos_][0]

            if((pos1 is not None) and (z == z0) and (c == c0) and ((abs(x-x0)+abs(y-y0))==1)):
                pos2 = pos_
                return pos1, pos2

            elif((pos1 is None) and (c==c0)):
                pos1 = pos_
                x0,y0 = key2pos(pos1)
                z0 = placed[pos1][1]

Where lego is the lego name i.e. "rb1" from the finite set that I have, and placed is a list of dictionaries each of which includes the newly placed legos i.e.

placed = [{'p_08_04':['r', 2]}, {'p_08_05':['r', 2]}]

The problem with my past function is that:

if there is a cube (one position 'p_xx_yy') and a brick (two positions) of the same color placed in different places the function will not work properly as pos1 might hold a cube position looking for a neighbor position of the same color!.

Can you please tell me how can I solve this issue please? thanks in advance.

Asked By: Bilal

||

Answers:

The solution was to store the blocks’ positions taking into account that I have a finite set of blocks, and to be sure that the position doesn’t belong to another Lego:

    self.lego_map = {
        "simple" : {
            "wc1":["p_08_08", 1],
            "wc2":["p_09_08", 1],
            "rc1":["p_08_10", 1], 
            "rc2":["p_09_10", 1],
            "yc1":["p_09_12", 1],
            "yb1":["p_08_12", "p_08_13", 1, False],
            "bb1":["p_00_00", "p_00_01", 1, False],
            "bb2":["p_00_21", "p_00_22", 1, False],
            "rb1":["p_02_00", "p_02_01", 1, False],
            "rb2":["p_02_21", "p_02_22", 1, False]
        },
        "complex": {
            "wc1":["p_00_03", 1],
            "wc2":["p_00_19", 1],
            "wc3":["p_08_07", 1],
            "wc4":["p_09_07", 1],
            "wc5":["p_10_07", 1],
            "rc1":["p_04_01", 1],
            "rc2":["p_04_21", 1],
            "rc3":["p_09_09", 1],
            "rc4":["p_10_09", 1],
            "rc5":["p_09_10", 1],
            "yc1":["p_10_12", 1],
            "bc1":["p_02_03", 1], 
            "bc2":["p_02_19", 1],
            "bc3":["p_09_04", 1],
            "lc1":["p_08_15", 1], 
            "lc2":["p_09_15", 1],
            "oc1":["p_00_05", 1], 
            "oc2":["p_00_17", 1],
            "oc3":["p_08_17", 1],
            "yb1":["p_00_00", "p_00_01", 1, False], 
            "yb2":["p_00_21", "p_00_22", 1, False], 
            "yb3":["p_08_12", "p_08_13", 1, False],
            "yb4":["p_09_12", "p_09_13", 1, False],
            "bb1":["p_02_00", "p_02_01", 1, False], 
            "bb2":["p_02_21", "p_02_22", 1, False],
            "bb3":["p_08_04", "p_08_05", 1, False],
            "rb1":["p_08_09", "p_08_10", 1, False]
        }
    }
    self.occupied_positions = []
    self.fillOccupiedPositions()

def fillOccupiedPositions(self):
    """
    
    """
    self.occupied_positions = []

    for lego in self.lego_map[self.model]:
        if(lego[1]=='c'):
            self.occupied_positions.append(self.lego_map[self.model][lego][0])
        else:
            self.occupied_positions.append(self.lego_map[self.model][lego][0])
            self.occupied_positions.append(self.lego_map[self.model][lego][1])

Answered By: Bilal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.