示例#1
0
def test_appear_as_subpart():
    '''
    Test 'appear_as_subpart' function on some examples
    
    '''

    # no
    pa_1 = ((2, 2, 2), (0, 3, 0), (1, 2, 0))
    # yes
    pa_2 = ((2, 2, 2), (0, 2, 0), (1, 2, 0))

    # yes
    pa_3 = ((0, 2), (0, 2), (1, 1))

    pb = ((9, 9, 9, 9, 9, 0, 0, 0), (0, 0, 0, 0, 1, 2, 2, 2),
          (0, 0, 0, 0, 1, 0, 2, 0), (0, 0, 1, 1, 1, 1, 2, 0), (0, 0, 0, 1, 0,
                                                               1, 1, 0))

    #    pprint.pprint(pa)
    #    pprint.pprint(pb)

    ta_1 = TetrisPart(pa_1)
    ta_2 = TetrisPart(pa_2)
    ta_3 = TetrisPart(pa_3)
    tb = TetrisPart(pb)
    tb.display('\nPart b')
    ta_1.display('\nSubpart of part b?  No')
    ta_2.display('\nSubpart of part b?  Yes')
    ta_3.display('\nSubpart of part b?  Yes')

    test_passed = (appear_as_subpart(pa_2, pb) and appear_as_subpart(pa_3, pb)
                   and not appear_as_subpart(pa_1, pb))

    return test_passed
def test_action_result_broad():   #USER ADDED
    '''
    Load some parts, choose first actions and see results. Visually inspect 
    
    '''
    initial_state = load_state('workbenches/wb_06_i.txt')        
    ap = AssemblyProblem_3(initial_state)
    actions = ap.actions(initial_state)
    display_state(initial_state, "INITIAL: ")
    print("Num Actions: ", len(actions))
    
    for i in range(0, len(actions)):
        if i >-10: #can limit how many to show, here
            if len(actions[i])==3:
                pa, pu, offset = actions[i]
                pa_ = TetrisPart(pa)
                pu_ = TetrisPart(pu)
                print("\n\nACTION #", i)
                print("Part Above:")
                pa_.display()
                print("Part Under:")
                pu_.display()
                print("Offset: ", offset)
                
            elif len(actions[i])==2:
                part, r = actions[i]
                p = TetrisPart(part)
                print("\n\nACTION #", i)
                print("Part: ")
                p.display()
                print("Rotation: ", r*90)
            new_state = ap.result(initial_state, actions[i])
            display_state(new_state, "Result:")
def test_action_result_deep():   #USER ADDED
    '''
    Load some parts, and keep building randomly until only one part exists.
    '''
    
    initial_state = load_state('workbenches/wb_08_i.txt')        
    ap = AssemblyProblem_3(initial_state)
    
    state = initial_state
    display_state(state, "INITIAL: ")
    assert(state == initial_state)
    i = 1
    
    while len(make_state_canonical(state)) is not 1:
        
        actions = ap.actions(state)
        action = random.choice(actions)
        
        if len(action)==3:
            pa, pu, offset = action
            pa_ = TetrisPart(pa)
            pu_ = TetrisPart(pu)
            print("\n\nACTION #", i, ", DROP")
            print("Part Above:")
            pa_.display()
            print("Part Under:")
            pu_.display()
            print("Offset: ", offset)
        elif len(action)==2:
            p, r = action
            part = TetrisPart(p)
            print("\n\nACTION #",i, ", ROTATION")
            print("Part:")
            part.display()
            print("Rotate: ", r*90)
        
        new_state = ap.result(state, action)
        assert(new_state != state)
        display_state(new_state, "Result: ")
        state = new_state
        i +=1
        
    print("\n\nFully Built.")
    test_passed = len(state) == 1
    print("Test Action and Result Passed: ", test_passed)
def test_appear_as_subpart(): #USER EDITED
    '''
    Test 'appear_as_subpart' function on some examples
    
    '''

    # no
    pa_1 =   ( (2, 2, 2),
             (0, 3, 0),
             (1, 2, 0))
    # yes
    pa_2 =   ( (2, 2, 2),
             (0, 2, 0),
             (1, 2, 0))

    # yes
    pa_3 =   ( (0, 2),
             (0, 2),
             (1, 1))
    
    # should be no
    pa_4 = ((0, 0),
            (0, 0),
            (9, 9))
    
    # yes
    pa_5 = ((0, 0, 0, 0, 2),
            (1, 1, 1, 1, 2))
    
    #no
    pa_6 = ((1, 0, 1, 2, 2),
            (1, 1, 1, 0, 2))

    pb =   ((9, 9, 9, 9, 9, 0, 0, 0),
   (0, 0, 0, 0, 1, 2, 2, 2),
   (0, 0, 0, 0, 1, 0, 2, 0),
   (0, 0, 1, 1, 1, 1, 2, 0),
   (0, 0, 0, 1, 0, 1, 1, 0))
    
#    pprint.pprint(pa)
#    pprint.pprint(pb)

    ta_1 = TetrisPart(pa_1) 
    ta_2 = TetrisPart(pa_2) 
    ta_3 = TetrisPart(pa_3) 
    ta_4 = TetrisPart(pa_4)
    ta_5 = TetrisPart(pa_5)
    ta_6 = TetrisPart(pa_6)
    tb = TetrisPart(pb)
    tb.display('\nPart b')
    ta_1.display('\nSubpart of part b?  No')
    ta_2.display('\nSubpart of part b?  Yes')
    ta_3.display('\nSubpart of part b?  Yes')
    ta_4.display('\nSubpart of part b?  No (without rotation)')
    ta_5.display('\nSubpart of part b?  Yes')
    ta_6.display('\nSubpart of part b?  No')


    test_passed =  (  
            appear_as_subpart(pa_2, pb)
            and 
            appear_as_subpart(pa_3, pb)
            and 
            appear_as_subpart(pa_5, pb)
            and 
            not  appear_as_subpart(pa_1, pb) 
            and
            not  appear_as_subpart(pa_4, pb)
            and
            not  appear_as_subpart(pa_6, pb)
            )
    
    return test_passed
def test_cost_rotated_subpart(): #USER ADDED
    '''
    Test 'cost_rotated_subpart' function on some examples
    
    '''

    # 2
    pa_1 = ((0, 0),
            (0, 0),
            (9, 9))
    # 0
    pa_2 =   ((1, 2, 2),
              (1, 0, 2),
              (1, 1, 2))

    # infinity
    pa_3 =   ((6, 6),
              (1, 8))
    
    # 1
    pa_4 = ((0, 1),
            (1, 2))
    
    # 3
    pa_5 = ((2, 2, 2, 1),
            (2, 0, 1, 1))

    pb =   ((9, 9, 9, 9, 9, 0, 0, 0),
            (0, 0, 0, 0, 1, 2, 2, 2),
            (0, 0, 0, 0, 1, 0, 2, 0),
            (0, 0, 1, 1, 1, 1, 2, 0),
            (0, 0, 0, 1, 0, 1, 1, 0))
    
#    pprint.pprint(pa)
#    pprint.pprint(pb)

    ta_1 = TetrisPart(pa_1) 
    ta_2 = TetrisPart(pa_2) 
    ta_3 = TetrisPart(pa_3) 
    ta_4 = TetrisPart(pa_4)
    ta_5 = TetrisPart(pa_5)
    tb = TetrisPart(pb)
    tb.display('\nPart b')
    ta_1.display('\nExists after 2 rotations.')
    ta_2.display('\nExists after 0 rotations.')
    ta_3.display('\nDoes not exist.')
    ta_4.display('\nExists after 1 rotation.')
    ta_5.display('\nExists after 3 rotations.')

    test_passed =  (  
            cost_rotated_subpart(pa_1,pb), 
            cost_rotated_subpart(pa_2,pb),
            cost_rotated_subpart(pa_3,pb),
            cost_rotated_subpart(pa_4,pb),
            cost_rotated_subpart(pa_5,pb)
            )
    
    #print(test_passed)
    
    return test_passed == (
                           2, 
                           0, 
                           np.inf, 
                           1, 
                           3)