예제 #1
0
def main():  #Nolan put everyones methods together and made main.
    stdout = "Please pick from the following options to determine the board size:"
    stdout += "\n a) 4\n b) 8\n c) 16\n>: "
    response = ""
    while response not in ["a", "A", "b", "B", "c", "C"]:
        response = raw_input(stdout)
        if response not in ["a", "A", "b", "B", "c", "C"]:
            print "*Incorrect input. Please enter the letter that corresponds to your choice\n"
    if response in ["a", "A"]:
        board = nqueens.Board(4)
    elif response in ["b", "B"]:
        board = nqueens.Board(8)
    else:
        board = nqueens.Board(16)
    board.rand()
    stay_in_loop = True
    while stay_in_loop:
        try:
            T = float(raw_input("Enter a value for T: "))
            min_T = float(
                raw_input("Enter a value for threshold of termination: "))
            decay = float(raw_input("Enter a value for the rate of decay: "))
            print ""
            stay_in_loop = False
        except:
            print "Invalid value entered. Try again.\n"
    schedule = Simulated_Annealing(T, board, min_T, decay)
    print "##############################"
    print "decay rate is: " + str(decay)
    print "##############################"
    print " "
    print " "
    print " "
    schedule.annealing()
예제 #2
0
def ConsoleMain(
):  #This main method is primarily for testing. The one that outputs to a file is below.
    stdout = "Please pick from the following options to determine the board size:"
    stdout += "\n a) 4\n b) 8\n c) 16\n>: "
    response = ""
    while response not in ["a", "A", "b", "B", "c", "C"]:
        response = raw_input(stdout)
        if response not in ["a", "A", "b", "B", "c", "C"]:
            print "*Incorrect input. Please enter the letter that corresponds to your choice\n"
    if response in ["a", "A"]:
        board = nqueens.Board(4)
    elif response in ["b", "B"]:
        board = nqueens.Board(8)
    else:
        board = nqueens.Board(16)
    board.rand()
    stay_in_loop = True
    while stay_in_loop:
        try:
            T = float(raw_input("Enter a value for T: "))
            min_T = float(
                raw_input("Enter a value for threshold of termination: "))
            decay = float(raw_input("Enter a value for the rate of decay: "))
            print ""
            stay_in_loop = False
        except:
            print "Invalid value entered. Try again.\n"
    schedule = Simulated_Annealing(T, board, min_T, decay)
    print decay
    schedule.annealing(True)
예제 #3
0
def main():

    board_sizes = [4, 8, 16]
    thresh_values = [.000001, .0000001, .00000001]
    decay_rate = [0.9, 0.75, 0.5]

    for x in board_sizes:
        print("**********************************")
        print("Board Size: ", x)
        print("**********************************")
        for j in range(len(decay_rate)):
            print("decayRate: %s ThresHold: %e" %
                  (decay_rate[j], thresh_values[j]))
            print("######################################")
            count_h_value = 0
            for i in range(10):
                print('Run', i)
                print('Initial: ')
                board = nqueens.Board(x)
                board.rand()
                board.printBoard()
                print("h-value: ", nqueens.numAttackingQueens(board))
                answer = simulated_annealing(decay_rate[j], board,
                                             thresh_values[j])
                print("Final h_value: ", answer.h)
                answer.printBoard()
                count_h_value += answer.h
            print("--------------------------------------")
            print("Average h value: ", count_h_value / 10)
            print("--------------------------------------")
예제 #4
0
 def test_correct_size4(self):
     test = nqueens.Board(4)
     test.place_queen(0, 2)
     test.place_queen(1, 0)
     test.place_queen(2, 3)
     test.place_queen(3, 1)
     self.assertEqual(test.verify_board(), 0, 'Board should pass')
예제 #5
0
def main():
    print('in main')
    pairs = [[0.9, 0.000001], [0.75, 0.0000001], [0.5, 0.00000001]]
    board_size = [4, 8, 16]
    for i in pairs:
        print("#############################################\n",
              "Decay Rate: ", i[0], "    T Threshold: ", i[1], "\n"
              "#############################################")
        for n in board_size:
            print("*********************\n", "  Board Size: ", n, "\n"
                  "*********************")
            average_h = 0.0
            for g in range(0, 10):
                startingBoard = nqueens.Board(n)
                startingBoard.rand()
                final_board = simulatedAnnealing(startingBoard, i[0], i[1])
                print("*********************\n", "    Run ", g + 1, "\n"
                      "*********************")
                print("Initial Board: ")
                startingBoard.printBoard()
                print("Initial Board h-value ", startingBoard.h)
                print("Final Board: ")
                final_board.printBoard()
                print("Final Board h-value: ", final_board.h)
                average_h += final_board.h
            average_h = average_h / 10
            print("Average h-cost of final solutions: ", average_h)
예제 #6
0
def Main():
    for i in range(2, 5):
        print("********************\n", "Board Size: ", 2**i,
              "\n********************\n", "######################################\n",
              "Decay Rate: 0.9 T Threshold: 0.000001\n", "######################################")
        b = nqueens.Board(2**i)
        
        bhAvg = 0
        for j in range(1, 11):
            print("Run ", j)
            b.rand()
            print("Initial Board:")
            b.printBoard()
            b.h = nqueens.numAttackingQueens(b)
            print("Initial h-value: ", b.h)
            b = simulatedAnnealing(b, 0.9, 0.000001)
            print("Final Board:")
            b.printBoard()
            print("Final h-value: ", b.h, "\n")
            bhAvg = bhAvg + b.h
        bhAvg = bhAvg/10
        print("********************\n", "Average H-Value: ", bhAvg,
          "\n********************\n")
        print("######################################\n",
              "Decay Rate: 0.75 T Threshold: 0.0000001\n", "######################################")
        bhAvg = 0
        for j in range(1, 11):
            print("Run ", j)
            b.rand()
            print("Initial Board:")
            b.printBoard()
            b.h = nqueens.numAttackingQueens(b)
            print("Initial h-value: ", b.h)
            b = simulatedAnnealing(b, 0.75, 0.0000001)
            print("Final Board:")
            b.printBoard()
            print("Final h-value: ", b.h, "\n")
            bhAvg = bhAvg + b.h
        bhAvg = bhAvg/10
        print("********************\n", "Average H-Value: ", bhAvg,
          "\n********************\n")    
        print("######################################\n",
              "Decay Rate: 0.5 T Threshold: 0.00000001\n", "######################################")
        bhAvg = 0
        for j in range(1, 11):
            print("Run ", j)
            b.rand()
            print("Initial Board:")
            b.printBoard()
            b.h = nqueens.numAttackingQueens(b)
            print("Initial h-value: ", b.h)
            b = simulatedAnnealing(b, 0.5, 0.00000001)
            print("Final Board:")
            b.printBoard()
            print("Final h-value: ", b.h, "\n")
            bhAvg = bhAvg + b.h
        bhAvg = bhAvg/10
        print("********************\n", "Average H-Value: ", bhAvg,
          "\n********************\n")
예제 #7
0
 def test_x_out_of_scope(self):
     test = nqueens.Board(4)
     test.place_queen(0, 0)
     test.place_queen(5, 3)
     self.assertEqual(
         test.verify_board(),
         0,
         'ERROR:root:Error placing queen 5,3: list index out of range',
     )
예제 #8
0
 def test_initialize(self):
     """Procedure:
         1. initialize the board
         ------------
         Verification:
         2. check board size
     """
     test = nqueens.Board(4)
     self.assertEqual(test.n, 4, 'Incorrect Board Size')
예제 #9
0
def main():
    print('In main')
    run_count = 0

    for size in board_size:
        print("Board size:", size)
        for each in simulation_pairs:
            h_values = []
            print("Decay Rate =", each[0], "T Threshold =", each[1])
            print("\n")
            for x in range(0, 10):
                print("Run", x)
                startingBoard = nq.Board(size)
                startingBoard.rand()
                print("Initial Board:")
                startingBoard.printBoard()
                print_initial_h(startingBoard)
                temp = simulatedAnnealing(startingBoard, each[1], each[0])
                h_values.append(temp)
            print("Average h-value =", (sum(h_values) / len(h_values)))
예제 #10
0
 def test_diagonal_fail_size1000(self):
     test = nqueens.Board(1000)
     test.place_queen(0, 0)
     test.place_queen(999, 999)
     self.assertEqual(test.verify_board(), 2,
                      'Looking for 2 Queen misplaced')
예제 #11
0
 def test_vertical_fail_size1000(self):
     test = nqueens.Board(1000)
     test.place_queen(0, 1)
     test.place_queen(850, 1)
     self.assertEqual(test.verify_board(), 2,
                      'Looking for 2 Queen misplaced')
예제 #12
0
 def test_horizontal_fail_size1000(self):
     test = nqueens.Board(1000)
     test.place_queen(0, 0)
     test.place_queen(0, 962)
     self.assertEqual(test.verify_board(), 2,
                      'Looking for 2 Queen misplaced')
예제 #13
0
 def test_diagonal_fail(self):
     test = nqueens.Board(4)
     test.place_queen(0, 0)
     test.place_queen(3, 3)
     self.assertEqual(test.verify_board(), 2,
                      'Looking for 2 Queen misplaced')
예제 #14
0
def main():
    T = 100

    #Board size of 4, and the subsequent output loops
    bSize = 4
    b = nqueens.Board(bSize)
    b.rand()
    decayRate = 0.9
    tHold = 0.000001
    print("Board Size: " + str(bSize))
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = 0.75
    tHold = 0.0000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = .5
    tHold = 0.00000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    ##############################################################################################################

    #Boardsize of 8, and the subsequent output loops
    bSize = 8
    b = nqueens.Board(bSize)
    b.rand()
    decayRate = 0.9
    tHold = 0.000001
    print("Board Size: " + str(bSize))
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = 0.75
    tHold = 0.0000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = .5
    tHold = 0.00000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    ###############################################################################################################################

    #Board size of 16, and the subsequent output loops
    bSize = 16
    b = nqueens.Board(bSize)
    b.rand()
    decayRate = 0.9
    tHold = 0.000001
    print("Board Size: " + str(bSize))
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = 0.75
    tHold = 0.0000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1

    decayRate = .5
    tHold = 0.00000001
    x = 0
    while (x < 10):
        print(
            "\n#####################################################\n\nRun " +
            str(x + 1))
        print("Initial Board State: ")
        b.printBoard()
        print("\nh value: " + str(b.h))
        print("Decay Rate: " + str(decayRate) + "       " + "T Threshold: " +
              str(tHold))
        d = annealing(b, T, decayRate, tHold)
        print("")
        print("Final board: ")
        d.printBoard()
        print("Final h value: " + str(d.h))
        x += 1
예제 #15
0
파일: anneal.py 프로젝트: tgriff34/ITCS3153
def acceptance_probability(delta_e, T):
    return decimal.Decimal(decimal.Decimal(math.e)**(decimal.Decimal(delta_e) / decimal.Decimal(T)))


print("######################################")
print("Decay rate 0.9 T Threshold 0.000001" )
print("######################################")
for i in range(0, 3):
    if i == 0: 
        print("*************")
        print("Board Size: 4")
        print("*************")
        average_h = 0
        for j in range(0, 10):
            b = nqueens.Board(4)
            b.rand()
            print "Initial board:"
            b.printBoard()
            print "Initial h value: ", b.numAttackingQueens()
            average_h = average_h + anneal(b, .000001, .9)
        print "Average h-cost of final solutions: ", average_h / 10
    elif i == 1:
        print("*************")
        print("Board Size: 8")
        print("*************")
        average_h = 0
        for j in range(0, 10):
            b = nqueens.Board(8)
            b.rand()
            print "Initial board:"
예제 #16
0
def main():

    print('**********************************')
    print('BOARD SIZE: 4 DECAY RATE 0.9 TRHESHOLD: 0.000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(4)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = simulatedAnnealing(startingBoard, 0.9, 0.000001)

    print('AVERAGE h VALUE: ', hAvg / 10)

    print('**********************************')
    print('BOARD SIZE: 8 DECAY RATE 0.9 TRHESHOLD: 0.000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(8)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.9, 0.000001)
    print('AVERAGE h VALUE: ', hAvg / 10)

    print('**********************************')
    print('BOARD SIZE: 16 DECAY RATE 0.9 TRHESHOLD: 0.000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(16)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.9, 0.000001)
        print('')
    print('AVERAGE h VALUE: ', hAvg / 10)
    print('**********************************')
    print('BOARD SIZE: 4 DECAY RATE 0.75 TRHESHOLD: 0.0000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(4)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.75, 0.0000001)
    print('AVERAGE h VALUE: ', hAvg / 10)

    #FINISH THE h VAL PRINTING

    print('**********************************')
    print('BOARD SIZE: 8 DECAY RATE 0.75 TRHESHOLD: 0.0000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(8)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.75, 0.0000001)
    print('AVERAGE h VALUE: ', hAvg / 10)

    print('**********************************')
    print('BOARD SIZE: 16 DECAY RATE 0.75 TRHESHOLD: 0.0000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(16)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.75, 0.0000001)
    print('AVERAGE h VALUE: ', hAvg / 10)

    print('**********************************')
    print('BOARD SIZE: 4 DECAY RATE 0.5 TRHESHOLD: 0.00000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(4)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.5, 0.00000001)
    print('AVERAGE h VALUE: ', hAvg / 10)
    print('**********************************')
    print('BOARD SIZE: 8 DECAY RATE 0.75 TRHESHOLD: 0.0000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(8)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.5, 0.00000001)
    print('AVERAGE h VALUE: ', hAvg / 10)
    print('**********************************')
    print('BOARD SIZE: 16 DECAY RATE 0.75 TRHESHOLD: 0.0000001')
    print('**********************************')
    hAvg = 0
    for x in range(10):
        print('RUN', x)
        startingBoard = nqueens.Board(16)
        startingBoard.rand()
        startingBoard.printBoard()
        print('INITIAL h VALUE', nqueens.numAttackingQueens(startingBoard))
        print('')
        hAvg = hAvg + simulatedAnnealing(startingBoard, 0.5, 0.00000001)
    print('AVERAGE h VALUE: ', hAvg / 10)
예제 #17
0
def main():
    T = INITIAL_T  # temperature variable is set to it's initial value
    decay_rate = [
        .9, .75, .5
    ]  #test values for decay rate. values #provided by Sterling McLeod
    threshold = [
        0.000001, 0.0000001, 0.00000001
    ]  #test values for threshold T must reach to terminate the loop. values #provided by Sterling McLeod
    board_size = [4, 8, 16]  # testor board sizes #provided by instructor
    time = 0  #todo: do we actualy need this variable?
    board = None
    waittime = 0
    sum_fin_h_vals = 0  #will hold the sum of all final h values
    num_iterations = 10  #the number of times each threshold-decay-rate pairing is tested
    suc_states = None
    h_vals = []  #temp testor variable holder
    fin_board = None

    for x in range(len(board_size)):
        print("_" * 51, "\n\nFor board size ", board_size[x], "\n_", "_" * 50,
              "\n")
        for y in range(len(threshold)):
            testVals = [threshold[y],
                        decay_rate[y]]  #stores this iteration's testing values
            print("_" * 21, "\n\nThreshold: ", testVals[0], "\nDecay Rate: ",
                  testVals[1], "\n_", "_" * 20, "\n")
            sum_fin_h_vals = 0  #resets the sum of final h values variable
            for z in range(num_iterations):
                if z == 2:
                    print(
                        "Please keep in mind this may take a momment to execute"
                    )
                #print("i looped. ", x, ", ", y, ", " ,  z)#temp: testor #note: this loop has been proven to work the correct number of times
                print("run #: ", z)
                T = INITIAL_T  # temperature variable is set to it's initial value
                time = 0  #resets time
                board = nqueens.Board(board_size[x])
                board.rand()
                board.h = nqueens.numAttackingQueens(board)
                print("intitial state: ", "\t\t\t\t\t\t\t\tintial h-value: ",
                      board.h)
                print(board.printBoard())
                #print("h-val after printboard ", board.h)#temp testor

                while (T >= testVals[0] and board.h != 0):
                    suc_states = nqueens.getSuccessorStates(board)
                    #print("suc states", suc_states)#temp testor
                    for i in suc_states:
                        temp_h = nqueens.numAttackingQueens(i)
                        if (temp_h < board.h):
                            board.h = temp_h
                            fin_board = i

                #     h_vals.append(board.h)#temp testing mechanism
                # print("h-val during suc_states loop ", h_vals)#temp testor
                # print("h after the sucstates loop", board.h)

                    T = get_ft(T, testVals[1])  #decrements T
                    time += 1  #increments time

                # print("\t\t T = ", T, "at time ", time, "h val is ",  board.h)#temp testor
                # while(input("************************************please Press enter to continue***************************************************") != "" ):
                #     waittime += 1

                sum_fin_h_vals += board.h
                print("final state: ", "\t\t\t\t\t\t\t\tfinal h-value: ",
                      board.h)
                print(fin_board.printBoard())
                while (input(
                        "**************************please Press enter to continue****************************"
                ) != ""):
                    waittime += 1  #just a placeholder so the syntax will work
            avg_time = (sum_fin_h_vals / num_iterations)
            print(
                "_" * 21, "\n\nAverage h-value for decay rate", testVals[1],
                " and threshold ", testVals[0], " is : ", avg_time, "\n_",
                "_" * 20, "\n\n", "*" * 20
            )  #todo: make this display the threshold and decay rate info too
            while (input(
                    "**********************please Press enter to continue*****************************"
            ) != ""):
                waittime += 1  #just a placeholder so the syntax will work

    print("done")  #temp testor
예제 #18
0
 def test_diagonal_fail3(self):
     test = nqueens.Board(6)
     test.place_queen(2, 2)
     test.place_queen(4, 0)
     self.assertEqual(test.verify_board(), 2,
                      'Looking for 2 Queen misplaced')