Example #1
0
File: tests.py Project: AIRising/AI
				4=B(c,9)
					5=Y(c,10)
						6=P(*,11)
			3=B(u,-)
		2=P(u,-)
"""

def csp_test_1_getargs():
    return [ "moose_csp_problem", "forward_checking" ]

def csp_test_1_testanswer(val, original_val = None):
    return ( val == EXPECTED_FC_MOOSE_TREE )

make_test(type = 'FUNCTION',
          getargs = csp_test_1_getargs,
          testanswer = csp_test_1_testanswer,
          expected_val = EXPECTED_FC_MOOSE_TREE,
          name = 'csp_solver_tree'
          )

#def csp_test_2_getargs():
#    return [ "moose_csp_problem", "forward_checking_prop_singleton" ]

#def csp_test_2_testanswer(val, original_val = None):
#    return ( val == EXPECTED_FCPS_MOOSE_TREE )

#make_test(type = 'FUNCTION',
#          getargs = csp_test_2_getargs,
#          testanswer = csp_test_2_testanswer,
#          expected_val = EXPECTED_FCPS_MOOSE_TREE,
#          name = 'csp_solver_tree'
#          )
# MIT 6.034 Lab 8: Bayesian Inference

from tester import make_test, get_tests
from nets import *

lab_number = 8 #for tester.py

def get_ancestors_0_getargs() :  #TEST 1
    return [net_basic.copy(), 'A']
get_ancestors_0_expected = set()
def get_ancestors_0_testanswer(val, original_val = None) :
    return val == get_ancestors_0_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = get_ancestors_0_getargs,
          testanswer = get_ancestors_0_testanswer,
          expected_val = str(get_ancestors_0_expected),
          name = 'get_ancestors')

def get_ancestors_1_getargs() :  #TEST 2
    return [net_basic.copy(), 'C']
get_ancestors_1_expected = set('AB')
def get_ancestors_1_testanswer(val, original_val = None) :
    return val == get_ancestors_1_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = get_ancestors_1_getargs,
          testanswer = get_ancestors_1_testanswer,
          expected_val = str(get_ancestors_1_expected),
          name = 'get_ancestors')

def get_ancestors_2_getargs() :  #TEST 3
    return [net_dsep.copy(), 'F']
Example #3
0
expected_accuracy = 1.0

def neural_net_test_testanswer(val, original_val = None):
    return abs(val - expected_accuracy) < 0.01

network_maker_func = "make_neural_net_two_layer"
network_min_size = 3
max_iterations = 10000

def neural_net_size_testanswer(val, original_val = None):
    return val <= network_min_size

make_test(type = 'FUNCTION',
          getargs = lambda: [network_maker_func],
          testanswer = neural_net_size_testanswer,
          expected_val = "your network must have <= %d neural units"\
          %(network_min_size),
          name = 'neural_net_size_tester'
          )

make_test(type = 'FUNCTION',
          getargs = lambda: [network_maker_func,
                             'and_data',
                             'and_test_data',
                             max_iterations],
          testanswer = neural_net_test_testanswer,
          expected_val = message %("AND", expected_accuracy),
          name = 'neural_net_tester'
          )

make_test(type = 'FUNCTION',
Example #4
0

def get_descendants_0_getargs():
    return [net_basic, 'A']


get_descendants_0_expected = set('C')


def get_descendants_0_testanswer(val, original_val=None):
    return val == get_descendants_0_expected


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=get_descendants_0_getargs,
          testanswer=get_descendants_0_testanswer,
          expected_val=str(get_descendants_0_expected),
          name='get_descendants')


def get_descendants_1_getargs():
    return [net_dsep, 'D']


get_descendants_1_expected = set('FG')


def get_descendants_1_testanswer(val, original_val=None):
    return val == get_descendants_1_expected

    return (set(dict1.keys()) == set(dict2.keys())
            and all([approx_equal(dict1[key], dict2[key], epsilon)
                     for key in dict1.keys()]))


#### NEURAL NETS ###############################################################

## stairstep
#T=0, x>T -> 1
def stairstep_0_getargs() :  #TEST 1
    return [randnum()]
def stairstep_0_testanswer(val, original_val = None) :
    return val == 1
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = stairstep_0_getargs,
          testanswer = stairstep_0_testanswer,
          expected_val = "1",
          name = 'stairstep')

#T=0, x<T -> 0
def stairstep_1_getargs() :  #TEST 2
    return [-randnum(), 0]
def stairstep_1_testanswer(val, original_val = None) :
    return val == 0
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = stairstep_1_getargs,
          testanswer = stairstep_1_testanswer,
          expected_val = "0",
          name = 'stairstep')

#T>0, x=T -> 1
Example #6
0
    from sets import Set as set, ImmutableSet as frozenset


### TEST 1 ###

# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

test_short_answer_1_getargs = "ANSWER_1"

def test_short_answer_1_testanswer(val, original_val = None):
    return str(val) == '2'

make_test(type = 'VALUE',
          getargs = test_short_answer_1_getargs,
          testanswer = test_short_answer_1_testanswer,
          expected_val = "correct value of ANSWER_1 ('1', '2', or '3')",
          name = test_short_answer_1_getargs
          )


### TEST 2 ###

# Because 'not' is coded in two separate ways.  You and I can
# tell what was meant to happen, but the forward chaining doesn't
# understand English, it just sees meaningless bits, and those do
# not match, in this case.

test_short_answer_2_getargs = "ANSWER_2"

def test_short_answer_2_testanswer(val, original_val = None):
    return str(val) == 'no'
Example #7
0
all_graphs = get_graphs()
GRAPH_0 = all_graphs['GRAPH_0']
GRAPH_1 = all_graphs['GRAPH_1']
GRAPH_2 = all_graphs['GRAPH_2']
GRAPH_3 = all_graphs['GRAPH_3']
GRAPH_FOR_HEURISTICS = all_graphs['GRAPH_FOR_HEURISTICS']
GRAPH_FOR_HEURISTICS_TRICKY = all_graphs['GRAPH_FOR_HEURISTICS_TRICKY']

##########################################################################
### OFFLINE TESTS (HARDCODED ANSWERS)

#### PART 1: Helper Functions #########################################

make_test(
    type='FUNCTION',  #TEST 1
    getargs=[GRAPH_1, ['a', 'c', 'b', 'd']],
    testanswer=lambda val, original_val=None: val == 11,
    expected_val=11,
    name='path_length')

make_test(
    type='FUNCTION',  #TEST 2
    getargs=[GRAPH_2, ['D', 'C', 'A', 'D', 'E', 'G', 'F']],
    testanswer=lambda val, original_val=None: val == 53,
    expected_val=53,
    name='path_length')

make_test(
    type='FUNCTION',  #TEST 3
    getargs=[GRAPH_1, ['a']],
    testanswer=lambda val, original_val=None: val == 0,
    expected_val=0,
Example #8
0
## is_game_over_connectfour


#has chain len 4 -> True
def is_game_over_connectfour_0_getargs():  #TEST 1
    return [PLAYER_ONE1_WON]


def is_game_over_connectfour_0_testanswer(val, original_val=None):
    return val == True


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=is_game_over_connectfour_0_getargs,
          testanswer=is_game_over_connectfour_0_testanswer,
          expected_val="True",
          name='is_game_over_connectfour')


#all chains len < 4, board full -> True
def is_game_over_connectfour_1_getargs():  #TEST 2
    return [BOARD_FULL_TIED]


def is_game_over_connectfour_1_testanswer(val, original_val=None):
    return val == True


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=is_game_over_connectfour_1_getargs,
Example #9
0
from tester import make_test, get_tests
from nets import *

def get_descendants_0_getargs() :
    return [net_basic, 'A']
get_descendants_0_expected = set('C')
def get_descendants_0_testanswer(val, original_val = None) :
    return val == get_descendants_0_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = get_descendants_0_getargs,
          testanswer = get_descendants_0_testanswer,
          expected_val = str(get_descendants_0_expected),
          name = 'get_descendants')

def get_descendants_1_getargs() :
    return [net_dsep, 'D']
get_descendants_1_expected = set('FG')
def get_descendants_1_testanswer(val, original_val = None) :
    return val == get_descendants_1_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = get_descendants_1_getargs,
          testanswer = get_descendants_1_testanswer,
          expected_val = str(get_descendants_1_expected),
          name = 'get_descendants')

def get_descendants_2_getargs() :
    return [net_dsep, 'B']
get_descendants_2_expected = set('CDEFG')
def get_descendants_2_testanswer(val, original_val = None) :
    return val == get_descendants_2_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
Example #10
0

def initialize_2_getargs():  #TEST 1
    return [["PointA"]]


initialize_2_expected = {"PointA": 1.0}


def initialize_2_testanswer(val, original_val=None):
    return val == initialize_2_expected


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=initialize_2_getargs,
          testanswer=initialize_2_testanswer,
          expected_val=str(initialize_2_expected),
          name='initialize_weights')


def initialize_3_getargs():  #TEST 2
    return [["-6", "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"]]


initialize_3_expected = {
    "-6": 1.0 / 12.0,
    "-5": 1.0 / 12.0,
    "-4": 1.0 / 12.0,
    "-3": 1.0 / 12.0,
    "-2": 1.0 / 12.0,
    "-1": 1.0 / 12.0,
Example #11
0
test_short_answer_1_getargs = "ANSWER_1"


# noinspection PyUnusedLocal
def test_short_answer_1_testanswer(val, original_val=None):
    return str(val) == "2"


# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

make_test(
    test_type="VALUE",
    getargs=test_short_answer_1_getargs,
    testanswer=test_short_answer_1_testanswer,
    expected_val="2",
    name=test_short_answer_1_getargs,
)

# TEST 2

test_short_answer_2_getargs = "ANSWER_2"


# noinspection PyUnusedLocal
def test_short_answer_2_testanswer(val, original_val=None):
    return str(val) == "no"


# Because 'not' is coded in two separate ways.  You and I can
Example #12
0
last_senate_people = read_congress_data('S109.ord')
last_senate_votes = read_vote_data('S109desc.csv')


def euclidean_distance_1_getargs():
    return [[1, 2, 3], [4, 5, 6]]


def euclidean_distance_1_testanswer(val, original_val=None):
    return (abs(val - math.sqrt(27)) < 0.00001)


make_test(type='FUNCTION',
          getargs=euclidean_distance_1_getargs,
          testanswer=euclidean_distance_1_testanswer,
          expected_val=math.sqrt(27),
          name='euclidean_distance')


def euclidean_distance_1_getargs():
    return [[1, 2, 3], [4, 5, 6]]


def euclidean_distance_1_testanswer(val, original_val=None):
    return (abs(val - math.sqrt(27)) < 0.00001)


make_test(type='FUNCTION',
          getargs=euclidean_distance_1_getargs,
          testanswer=euclidean_distance_1_testanswer,
Example #13
0
### TEST 1 ###

# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

test_short_answer_1_getargs = "ANSWER_1"


def test_short_answer_1_testanswer(val, original_val=None):
    return str(val) == '2'


make_test(type='VALUE',
          getargs=test_short_answer_1_getargs,
          testanswer=test_short_answer_1_testanswer,
          expected_val="correct value of ANSWER_1 ('1', '2', or '3')",
          name=test_short_answer_1_getargs)

### TEST 2 ###

# Because 'not' is coded in two separate ways.  You and I can
# tell what was meant to happen, but the forward chaining doesn't
# understand English, it just sees meaningless bits, and those do
# not match, in this case.

test_short_answer_2_getargs = "ANSWER_2"


def test_short_answer_2_testanswer(val, original_val=None):
    return str(val) == 'no'
Example #14
0
#### NEURAL NETS ###############################################################


## stairstep
#T=0, x>T -> 1
def stairstep_0_getargs():  #TEST 1
    return [randnum()]


def stairstep_0_testanswer(val, original_val=None):
    return val == 1


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=stairstep_0_getargs,
          testanswer=stairstep_0_testanswer,
          expected_val="1",
          name='stairstep')


#T=0, x<T -> 0
def stairstep_1_getargs():  #TEST 2
    return [-randnum(), 0]


def stairstep_1_testanswer(val, original_val=None):
    return val == 0


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=stairstep_1_getargs,
Example #15
0
lab_number = 0

#### Multiple Choice ###########################################################

ANSWER_1_getargs = "ANSWER_1"  #TEST 1


def ANSWER_1_testanswer(val, original_val=None):
    if val == None or val == '':
        raise NotImplementedError
    return val == False


make_test(type="VALUE",
          getargs=ANSWER_1_getargs,
          testanswer=ANSWER_1_testanswer,
          expected_val=("(the correct answer, either True or False)"),
          name=ANSWER_1_getargs)

ANSWER_2_getargs = "ANSWER_2"  #TEST 2


def ANSWER_2_testanswer(val, original_val=None):
    if val == None or val == '':
        raise NotImplementedError
    return val == "D"


make_test(type="VALUE",
          getargs=ANSWER_2_getargs,
          testanswer=ANSWER_2_testanswer,
# goal node is unknown.  In this case, even a finite local maximum will
# lead to a wrong answer: hill climbing will pick one of the nodes with
# the best score within the local maximum, which is still (by definition)
# not as high as the score of the actual goal node, the global maximum.

# Finally, without backtracking, hill climbing will take just one path,
# guaranteeing that it always goes uphill, but not necessarily up the right
# hill.  When it reaches a node from which it can only go downhill, then its
# answer depends on the second criterion: if the score of the goal node is
# known, then it can report failure explicitly; otherwise it may return a
# local maximum as its answer.


make_test(type = 'VALUE',
          getargs = ANSWER1_getargs,
          testanswer = ANSWER1_testanswer,
          expected_val = "False",
          name = ANSWER1_getargs
          )


### TEST 2 ###

ANSWER2_getargs = "ANSWER2"

def ANSWER2_testanswer(val, original_val = None):
    return ( val == False )

# Best first search will give an optimal path iff it has an admissible heuristic.

make_test(type = 'VALUE',
          getargs = ANSWER2_getargs,
Example #17
0
# Another common use of hill climbing techniques is where the score of the
# goal node is unknown.  In this case, even a finite local maximum will
# lead to a wrong answer: hill climbing will pick one of the nodes with
# the best score within the local maximum, which is still (by definition)
# not as high as the score of the actual goal node, the global maximum.

# Finally, without backtracking, hill climbing will take just one path,
# guaranteeing that it always goes uphill, but not necessarily up the right
# hill.  When it reaches a node from which it can only go downhill, then its
# answer depends on the second criterion: if the score of the goal node is
# known, then it can report failure explicitly; otherwise it may return a
# local maximum as its answer.

make_test(type='VALUE',
          getargs=ANSWER1_getargs,
          testanswer=ANSWER1_testanswer,
          expected_val="False",
          name=ANSWER1_getargs)

### TEST 2 ###

ANSWER2_getargs = "ANSWER2"


def ANSWER2_testanswer(val, original_val=None):
    return (val == False)


# Best first search will give an optimal path iff it has an admissible heuristic.

make_test(type='VALUE',
Example #18
0
all_graphs = get_graphs()
GRAPH_0 = all_graphs['GRAPH_0']
GRAPH_1 = all_graphs['GRAPH_1']
GRAPH_2 = all_graphs['GRAPH_2']
GRAPH_3 = all_graphs['GRAPH_3']
GRAPH_FOR_HEURISTICS = all_graphs['GRAPH_FOR_HEURISTICS']

##########################################################################
### OFFLINE TESTS (HARDCODED ANSWERS)

#### PART 1: Helper Functions #########################################

if RUN_ADDITIONAL_TESTS:
    make_test(type = 'FUNCTION',
              getargs = [GRAPH_1, ['a', 'c', 'b', 'd']],
              testanswer = lambda val, original_val=None: val == 11,
              expected_val = 11,
              name = 'path_length')

make_test(type = 'FUNCTION',
          getargs = [GRAPH_2, ['D', 'C', 'A', 'D', 'E', 'G', 'F']],
          testanswer = lambda val, original_val=None: val == 53,
          expected_val = 53,
          name = 'path_length')

make_test(type = 'FUNCTION',
          getargs = [GRAPH_1, ['a']],
          testanswer = lambda val, original_val=None: val == 0,
          expected_val = 0,
          name = 'path_length')
Example #19
0
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,2,2,1,1,2,0 ),
                                          ( 0,2,1,2,1,2,0 ),
                                          ( 2,1,2,1,1,1,0 ),
                                          ),
                                        current_player = 2)

ANSWER1_getargs = "ANSWER1"

def ANSWER1_testanswer(val, original_val = None):
    return ( val == 3 )

make_test(type = 'VALUE',
          getargs = ANSWER1_getargs,
          testanswer = ANSWER1_testanswer,
          expected_val = "3",
          name = ANSWER1_getargs
          )

ANSWER2_getargs = "ANSWER2"

def ANSWER2_testanswer(val, original_val = None):
    return ( val == 2 )

make_test(type = 'VALUE',
          getargs = ANSWER2_getargs,
          testanswer = ANSWER2_testanswer,
          expected_val = "2",
          name = ANSWER2_getargs
          )
Example #20
0
# MIT 6.034 Lab 0: Getting Started

from tester import make_test, get_tests
from point_api import Point
import random
random.seed() # Change to "random.seed(n)" to make the random tests always return the same value

ANSWER_1_getargs = 'ANSWER_1'  #TEST 1
def ANSWER_1_testanswer(val, original_val = None):
    return val == 'B'
make_test(type = 'VALUE',
          getargs = ANSWER_1_getargs,
          testanswer = ANSWER_1_testanswer,
          expected_val = ('(the correct letter A, B, or C)'),
          name = ANSWER_1_getargs)

#### Warm-up: Exponentiation ###################################################

def cube_0_getargs():  #TEST 2
    return [10]
def cube_0_testanswer(val, original_val = None):
    return val == 1000
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = cube_0_getargs,
          testanswer = cube_0_testanswer,
          expected_val = "1000",
          name = 'cube')

def cube_1_getargs():  #TEST 3
    return [1]
def cube_1_testanswer(val, original_val = None):
Example #21
0
senate_votes = read_vote_data('S110desc.csv')

house_people = read_congress_data('H110.ord')
house_votes = read_vote_data('H110desc.csv')

last_senate_people = read_congress_data('S109.ord')
last_senate_votes = read_vote_data('S109desc.csv')


def euclidean_distance_1_getargs():
    return [ [1,2,3], [4,5,6] ]
def euclidean_distance_1_testanswer(val, original_val = None):
    return ( abs(val - math.sqrt(27)) < 0.00001 )
make_test(type = 'FUNCTION',
          getargs = euclidean_distance_1_getargs,
          testanswer = euclidean_distance_1_testanswer,
          expected_val = math.sqrt(27),
          name = 'euclidean_distance'
          )

def euclidean_distance_1_getargs():
    return [ [1,2,3], [4,5,6] ]
def euclidean_distance_1_testanswer(val, original_val = None):
    return ( abs(val - math.sqrt(27)) < 0.00001 )
make_test(type = 'FUNCTION',
          getargs = euclidean_distance_1_getargs,
          testanswer = euclidean_distance_1_testanswer,
          expected_val = math.sqrt(27),
          name = 'euclidean_distance'
          )

def euclidean_distance_2_getargs():
Example #22
0

def get_ancestors_0_getargs():  #TEST 1
    return [net_basic.copy(), 'A']


get_ancestors_0_expected = set()


def get_ancestors_0_testanswer(val, original_val=None):
    return val == get_ancestors_0_expected


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=get_ancestors_0_getargs,
          testanswer=get_ancestors_0_testanswer,
          expected_val=str(get_ancestors_0_expected),
          name='get_ancestors')


def get_ancestors_1_getargs():  #TEST 2
    return [net_basic.copy(), 'C']


get_ancestors_1_expected = set('AB')


def get_ancestors_1_testanswer(val, original_val=None):
    return val == get_ancestors_1_expected

Example #23
0
except NameError:
    from sets import Set as set, ImmutableSet as frozenset

### TEST 1 ###

test_short_answer_1_getargs = "ANSWER_1"

def test_short_answer_1_testanswer(val, original_val = None):
    return str(val) == '2'

# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

make_test(type = 'VALUE',
          getargs = test_short_answer_1_getargs,
          testanswer = test_short_answer_1_testanswer,
          expected_val = "2",
          name = test_short_answer_1_getargs
          )


### TEST 2 ###

test_short_answer_2_getargs = "ANSWER_2"

def test_short_answer_2_testanswer(val, original_val = None):
    return str(val) == 'no'

# Because 'not' is coded in two separate ways.  You and I can
# tell what was meant to happen, but the forward chaining doesn't
# understand English, it just sees meaningless bits, and those do
# not match, in this case.
Example #24
0
                     for key in dict1.keys()]))

def classifier_approx_equal(H1, H2, epsilon=0.00000001):
  return (len(H1) == len(H2) and \
    all([H1[i][0] == H2[i][0] and \
      approx_equal(H1[i][1], H2[i][1], epsilon) for i in range(len(H1))]))


def initialize_2_getargs() :  #TEST 1
    return [["PointA"]]
initialize_2_expected = {"PointA":1.0}
def initialize_2_testanswer(val, original_val = None) :
    return val == initialize_2_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = initialize_2_getargs,
          testanswer = initialize_2_testanswer,
          expected_val = str(initialize_2_expected),
          name = 'initialize_weights')

def initialize_3_getargs() :  #TEST 2
    return [["-6","-5","-4","-3","-2","-1","0","1","2","3","4","5"]]
initialize_3_expected = {"-6":1.0/12.0,"-5":1.0/12.0,"-4":1.0/12.0,"-3":1.0/12.0,"-2":1.0/12.0,"-1":1.0/12.0,"0":1.0/12.0,"1":1.0/12.0,"2":1.0/12.0,"3":1.0/12.0,"4":1.0/12.0,"5":1.0/12.0}
def initialize_3_testanswer(val, original_val = None) :
    return val == initialize_3_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = initialize_3_getargs,
          testanswer = initialize_3_testanswer,
          expected_val = str(initialize_3_expected),
          name = 'initialize_weights')

Example #25
0
                  endgame_score_connectfour, endgame_score_connectfour_faster,
                  minimax_search)
INF = float('inf')



## is_game_over_connectfour

#has chain len 4 -> True
def is_game_over_connectfour_0_getargs() :  #TEST 1
    return [PLAYER_ONE1_WON]
def is_game_over_connectfour_0_testanswer(val, original_val = None) :
    return val == True
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = is_game_over_connectfour_0_getargs,
          testanswer = is_game_over_connectfour_0_testanswer,
          expected_val = "True",
          name = 'is_game_over_connectfour')

#all chains len < 4, board full -> True
def is_game_over_connectfour_1_getargs() :  #TEST 2
    return [BOARD_FULL_TIED]
def is_game_over_connectfour_1_testanswer(val, original_val = None) :
    return val == True
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = is_game_over_connectfour_1_getargs,
          testanswer = is_game_over_connectfour_1_testanswer,
          expected_val = "True",
          name = 'is_game_over_connectfour')

#all chains len < 4, board not full -> False
Example #26
0
expected_accuracy = 1.0

def neural_net_test_testanswer(val, original_val = None):
    return abs(val - expected_accuracy) < 0.01

network_maker_func = "make_neural_net_two_layer"
network_min_size = 3
max_iterations = 10000

def neural_net_size_testanswer(val, original_val = None):
    return val <= network_min_size

make_test(type = 'FUNCTION',
          getargs = lambda: [network_maker_func],
          testanswer = neural_net_size_testanswer,
          expected_val = "your network must have <= %d neural units"\
          %(network_min_size),
          name = 'neural_net_size_tester'
          )

make_test(type = 'FUNCTION',
          getargs = lambda: [network_maker_func,
                             'and_data',
                             'and_test_data',
                             max_iterations],
          testanswer = neural_net_test_testanswer,
          expected_val = message %("AND", expected_accuracy),
          name = 'neural_net_tester'
          )

make_test(type = 'FUNCTION',
Example #27
0
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,2,2,1,1,2,0 ),
                                          ( 0,2,1,2,1,2,0 ),
                                          ( 2,1,2,1,1,1,0 ),
                                          ),
                                        current_player = 2)

ANSWER1_getargs = "ANSWER1"

#test1
def ANSWER1_testanswer(val, original_val = None):
    return ( val == 3 )

make_test(type = 'VALUE',
          getargs = ANSWER1_getargs,
          testanswer = ANSWER1_testanswer,
          expected_val = "3",
          name = ANSWER1_getargs
          )

ANSWER2_getargs = "ANSWER2"

#test2
def ANSWER2_testanswer(val, original_val = None):
    return ( val == 2 )

make_test(type = 'VALUE',
          getargs = ANSWER2_getargs,
          testanswer = ANSWER2_testanswer,
          expected_val = "2",
          name = ANSWER2_getargs
          )
Example #28
0
from tester import make_test, get_tests
from utils import *

lab_number = 9 #for tester.py

F = Fraction #lazy alias

def initialize_2_getargs() :  #TEST 1
    return [["PointA"]]
initialize_2_expected = {"PointA":1}
def initialize_2_testanswer(val, original_val = None) :
    return val == initialize_2_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = initialize_2_getargs,
          testanswer = initialize_2_testanswer,
          expected_val = str(initialize_2_expected),
          name = 'initialize_weights')

def initialize_3_getargs() :  #TEST 2
    return [["-6","-5","-4","-3","-2","-1","0","1","2","3","4","5"]]
initialize_3_expected = {"-6":F(1,12),"-5":F(1,12),"-4":F(1,12),
                         "-3":F(1,12),"-2":F(1,12),"-1":F(1,12),
                         "0":F(1,12),"1":F(1,12),"2":F(1,12),
                         "3":F(1,12),"4":F(1,12),"5":F(1,12)}
def initialize_3_testanswer(val, original_val = None) :
    return val == initialize_3_expected
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = initialize_3_getargs,
          testanswer = initialize_3_testanswer,
          expected_val = str(initialize_3_expected),
    "Generates a random float 0 < n < max_val"
    return random() * randint(1, int(max_val))


## dot_product
def dot_product_0_getargs():  #TEST 1
    return [(3, -7), [2.5, 10]]


def dot_product_0_testanswer(val, original_val=None):
    return approx_equal(val, -62.5)


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=dot_product_0_getargs,
          testanswer=dot_product_0_testanswer,
          expected_val="-62.5",
          name='dot_product')


def dot_product_1_getargs():  #TEST 2
    return [[4], (5, )]


def dot_product_1_testanswer(val, original_val=None):
    return val == 20


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=dot_product_1_getargs,
          testanswer=dot_product_1_testanswer,
Example #30
0
File: tests.py Project: AIRising/AI

def hill_climbing_6_getargs():
    hill_climbing_timing["START"] = time.time()
    return [hill_climbing_test_6_graph, "1", hill_climbing_test_6_goal]


def hill_climbing_6_testanswer(val, original_val=None):
    elapsed = time.time() - hill_climbing_timing["START"]
    return elapsed < 5 and val[-1] == hill_climbing_test_6_goal


make_test(
    type="FUNCTION",
    getargs=hill_climbing_6_getargs,
    testanswer=hill_climbing_6_testanswer,
    expected_val=("hill climbing to take less than one second and get to %s" % hill_climbing_test_6_goal),
    name="hill_climbing",
)


#### TEST 16 ###

# def beam_search_1_getargs():
#    return [ NEWGRAPH1, 'S', 'G', 2 ]

# beam_search_1_answer = list('')

# def beam_search_1_testanswer(val, original_val = None):
#    return ( val == beam_search_1_answer )
Example #31
0
File: tests.py Project: AIRising/AI
        (0, 0, 0, 0, 0, 0, 0),
        (0, 2, 2, 1, 1, 2, 0),
        (0, 2, 1, 2, 1, 2, 0),
        (2, 1, 2, 1, 1, 1, 0),
    ),
    current_player=2,
)

ANSWER1_getargs = "ANSWER1"


def ANSWER1_testanswer(val, original_val=None):
    return val == 3


make_test(type="VALUE", getargs=ANSWER1_getargs, testanswer=ANSWER1_testanswer, expected_val="3", name=ANSWER1_getargs)

ANSWER2_getargs = "ANSWER2"


def ANSWER2_testanswer(val, original_val=None):
    return val == 2


make_test(type="VALUE", getargs=ANSWER2_getargs, testanswer=ANSWER2_testanswer, expected_val="2", name=ANSWER2_getargs)


def run_test_search_1_getargs():
    return ["minimax", "WINNING_BOARD", 2, "focused_evaluate"]

Example #32
0
### TEST 1 ###

# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

ANSWER_1_getargs = "ANSWER_1"

def ANSWER_1_testanswer(val, original_val = None):
    if val == '':
        raise NotImplementedError
    return str(val) == '2'

make_test(type = 'VALUE',
          getargs = ANSWER_1_getargs,
          testanswer = ANSWER_1_testanswer,
          expected_val = "correct value of ANSWER_1 ('1', '2', '3', or '4')",
          name = ANSWER_1_getargs
          )


### TEST 2 ###

# Backwards chaining does not produce assertions, so neither
# part of the rule will apper as a new assertion

ANSWER_2_getargs = "ANSWER_2"

def ANSWER_2_testanswer(val, original_val = None):
    if val == '':
        raise NotImplementedError
    return str(val) == '4'
    values, otherwise False"""
    return (set(dict1.keys()) == set(dict2.keys())
            and all([approx_equal(dict1[key], dict2[key], epsilon)
                     for key in dict1.keys()]))


# WIRING A NEURAL NET

nn_half_getargs = 'nn_half'
def nn_half_testanswer(val, original_val = None):  #TEST 1
    if val == []:
        raise NotImplementedError
    return val == [1]
make_test(type = 'VALUE',
          getargs = nn_half_getargs,
          testanswer = nn_half_testanswer,
          expected_val = ('(list indicating correct minimum number of neurons '
                          + 'per layer)'),
          name = nn_half_getargs)

nn_angle_getargs = 'nn_angle'
def nn_angle_testanswer(val, original_val = None):  #TEST 2
    if val == []:
        raise NotImplementedError
    return val == [2, 1]
make_test(type = 'VALUE',
          getargs = nn_angle_getargs,
          testanswer = nn_angle_testanswer,
          expected_val = ('(list indicating correct minimum number of neurons '
                          + 'per layer)'),
          name = nn_angle_getargs)
Example #34
0
from read_graphs import get_graphs
all_graphs = get_graphs()
GRAPH_0 = all_graphs['GRAPH_0']
GRAPH_1 = all_graphs['GRAPH_1']
GRAPH_2 = all_graphs['GRAPH_2']
GRAPH_3 = all_graphs['GRAPH_3']
GRAPH_FOR_HEURISTICS = all_graphs['GRAPH_FOR_HEURISTICS']

##########################################################################
### OFFLINE TESTS (HARDCODED ANSWERS)

#### PART 1: Helper Functions #########################################

make_test(type = 'FUNCTION',  #TEST 1
          getargs = [GRAPH_1, ['a', 'c', 'b', 'd']],
          testanswer = lambda val, original_val=None: val == 11,
          expected_val = 11,
          name = 'path_length')

make_test(type = 'FUNCTION',  #TEST 2
          getargs = [GRAPH_2, ['D', 'C', 'A', 'D', 'E', 'G', 'F']],
          testanswer = lambda val, original_val=None: val == 53,
          expected_val = 53,
          name = 'path_length')

make_test(type = 'FUNCTION',  #TEST 3
          getargs = [GRAPH_1, ['a']],
          testanswer = lambda val, original_val=None: val == 0,
          expected_val = 0,
          name = 'path_length')
Example #35
0
# goal node is unknown.  In this case, even a finite local maximum will
# lead to a wrong answer: hill climbing will pick one of the nodes with
# the best score within the local maximum, which is still (by definition)
# not as high as the score of the actual goal node, the global maximum.

# Finally, without backtracking, hill climbing will take just one path,
# guaranteeing that it always goes uphill, but not necessarily up the right
# hill.  When it reaches a node from which it can only go downhill, then its
# answer depends on the second criterion: if the score of the goal node is
# known, then it can report failure explicitly; otherwise it may return a
# local maximum as its answer.


make_test(type = 'VALUE',
          getargs = ANSWER1_getargs,
          testanswer = ANSWER1_testanswer,
          expected_val = "False",
          name = ANSWER1_getargs
          )


### TEST 2 ###

ANSWER2_getargs = "ANSWER2"

def ANSWER2_testanswer(val, original_val = None):
    return ( val == False )

# Best first search will give an optimal path iff it has an admissible heuristic.

make_test(type = 'VALUE',
          getargs = ANSWER2_getargs,
Example #36
0
# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

ANSWER_1_getargs = "ANSWER_1"


def ANSWER_1_testanswer(val, original_val=None):
    if val == '':
        raise NotImplementedError
    return str(val) == '2'


make_test(type='VALUE',
          getargs=ANSWER_1_getargs,
          testanswer=ANSWER_1_testanswer,
          expected_val="correct value of ANSWER_1 ('1', '2', '3', or '4')",
          name=ANSWER_1_getargs)

### TEST 2 ###

# Backwards chaining does not produce assertions, so neither
# part of the rule will apper as a new assertion

ANSWER_2_getargs = "ANSWER_2"


def ANSWER_2_testanswer(val, original_val=None):
    if val == '':
        raise NotImplementedError
    return str(val) == '4'
Example #37
0
## is_game_over_connectfour


#has chain len 4 -> True
def is_game_over_connectfour_0_getargs():  #TEST 1
    return [PLAYER_ONE1_WON]


def is_game_over_connectfour_0_testanswer(val, original_val=None):
    return val == True


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=is_game_over_connectfour_0_getargs,
          testanswer=is_game_over_connectfour_0_testanswer,
          expected_val="True",
          name='is_game_over_connectfour')


#all chains len < 4, board full -> True
def is_game_over_connectfour_1_getargs():  #TEST 2
    return [BOARD_FULL_TIED]


def is_game_over_connectfour_1_testanswer(val, original_val=None):
    return val == True


make_test(type='FUNCTION_ENCODED_ARGS',
          getargs=is_game_over_connectfour_1_getargs,
Example #38
0
				4=B(c,9)
					5=Y(c,10)
						6=P(*,11)
			3=B(u,-)
		2=P(u,-)
"""

def csp_test_1_getargs():
    return [ "moose_csp_problem", "forward_checking" ]

def csp_test_1_testanswer(val, original_val = None):
    return ( val == EXPECTED_FC_MOOSE_TREE )

make_test(type = 'FUNCTION',
          getargs = csp_test_1_getargs,
          testanswer = csp_test_1_testanswer,
          expected_val = EXPECTED_FC_MOOSE_TREE,
          name = 'csp_solver_tree'
          )

def csp_test_2_getargs():
    return [ "moose_csp_problem", "forward_checking_prop_singleton" ]

def csp_test_2_testanswer(val, original_val = None):
    return ( val == EXPECTED_FCPS_MOOSE_TREE )

make_test(type = 'FUNCTION',
          getargs = csp_test_2_getargs,
          testanswer = csp_test_2_testanswer,
          expected_val = EXPECTED_FCPS_MOOSE_TREE,
          name = 'csp_solver_tree'
          )
				4=B(c,9)
					5=Y(c,10)
						6=P(*,11)
			3=B(u,-)
		2=P(u,-)
"""

def csp_test_1_getargs():
    return [ "moose_csp_problem", "forward_checking" ]

def csp_test_1_testanswer(val, original_val = None):
    return ( val == EXPECTED_FC_MOOSE_TREE )

make_test(type = 'FUNCTION',
          getargs = csp_test_1_getargs,
          testanswer = csp_test_1_testanswer,
          expected_val = EXPECTED_FC_MOOSE_TREE,
          name = 'csp_solver_tree'
          )

def csp_test_2_getargs():
    return [ "moose_csp_problem", "forward_checking_prop_singleton" ]

def csp_test_2_testanswer(val, original_val = None):
    return ( val == EXPECTED_FCPS_MOOSE_TREE )

make_test(type = 'FUNCTION',
          getargs = csp_test_2_getargs,
          testanswer = csp_test_2_testanswer,
          expected_val = EXPECTED_FCPS_MOOSE_TREE,
          name = 'csp_solver_tree'
          )
Example #40
0
from tester import make_test, get_tests
from point_api import Point
import random
random.seed(
)  # Change to "random.seed(n)" to make the random tests always return the same value

ANSWER_1_getargs = 'ANSWER_1'  #TEST 1


def ANSWER_1_testanswer(val, original_val=None):
    return val == 'B'


make_test(type='VALUE',
          getargs=ANSWER_1_getargs,
          testanswer=ANSWER_1_testanswer,
          expected_val=('(the correct letter A, B, or C)'),
          name=ANSWER_1_getargs)

#### Warm-up: Exponentiation ###################################################


def cube_0_getargs():  #TEST 2
    return [10]


def cube_0_testanswer(val, original_val=None):
    return val == 1000


make_test(type='FUNCTION_ENCODED_ARGS',
Example #41
0
except NameError:
    from sets import Set as set, ImmutableSet as frozenset

### TEST 1 ###

test_short_answer_1_getargs = "ANSWER_1"

def test_short_answer_1_testanswer(val, original_val = None):
    return str(val) == '2'

# The antecedent checks data, it does not add any -- it lists the
# questions asked to see if the rule should fire.

make_test(type = 'VALUE',
          getargs = test_short_answer_1_getargs,
          testanswer = test_short_answer_1_testanswer,
          expected_val = "2",
          name = test_short_answer_1_getargs
          )


### TEST 2 ###

test_short_answer_2_getargs = "ANSWER_2"

def test_short_answer_2_testanswer(val, original_val = None):
    return str(val) == 'no'

# Because 'not' is coded in two separate ways.  You and I can
# tell what was meant to happen, but the forward chaining doesn't
# understand English, it just sees meaningless bits, and those do
# not match, in this case.
Example #42
0
                  endgame_score_connectfour, endgame_score_connectfour_faster,
                  minimax_search)
INF = float('inf')



## is_game_over_connectfour

#has chain len 4 -> True
def is_game_over_connectfour_0_getargs() :  #TEST 1
    return [PLAYER_ONE1_WON]
def is_game_over_connectfour_0_testanswer(val, original_val = None) :
    return val == True
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = is_game_over_connectfour_0_getargs,
          testanswer = is_game_over_connectfour_0_testanswer,
          expected_val = "True",
          name = 'is_game_over_connectfour')

#all chains len < 4, board full -> True
def is_game_over_connectfour_1_getargs() :  #TEST 2
    return [BOARD_FULL_TIED]
def is_game_over_connectfour_1_testanswer(val, original_val = None) :
    return val == True
make_test(type = 'FUNCTION_ENCODED_ARGS',
          getargs = is_game_over_connectfour_1_getargs,
          testanswer = is_game_over_connectfour_1_testanswer,
          expected_val = "True",
          name = 'is_game_over_connectfour')

#all chains len < 4, board not full -> False