Exemplo n.º 1
0
example = 'd4 d5 g3 g6 b3 Bg7 Bb2 Nf6 Bg2 O-O Nf3 Nc6 O-O e6 Ne5 Nxe5 dxe5 Nd7 f4 c6 e4 Qb6+ Bd4 Qc7 exd5 cxd5 Nc3 b6 Nb5 Qd8 Nd6 Ba6 c4 dxc4 bxc4 Rb8 Qa4 Bc8 Qxa7 Bb7 Bxb7 Rxb7 Qxb7 f6 Bxb6 Nxb6 Rab1 Nxc4 Nxc4 Qd4+ Kg2 Qxc4 a3 fxe5 fxe5 Bxe5 Rxf8+ Kxf8 Rf1+ Kg8 Qf7+ Kh8 Qf8#'

#trying to stream line and tidy up the awful process of reading moves contained in run_gamestring
import gs as gs
from stats import analyze as an
from stats import validCoords
from models import forest
import numpy as np
fi = forest(pikl='data/forestmodelDepth20.pkl')


def MO(move):
    print(move)
    places = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    rows = {'1': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7}
    pieces = ['N', 'K', 'B', 'R', 'Q']
    cols = {'a': 7, 'b': 6, 'c': 5, 'd': 4, 'e': 3, 'f': 2, 'g': 1, 'h': 0}
    pieces = {'N': 3, 'B': 4, 'Q': 9, 'K': 1000, 'R': 5}
    move = list(move)
    new_move = {'cols': [], 'rows': [], 'piece': [], 'type': '', 'castles': []}

    for key in list(move):

        if key in rows:

            new_move['rows'].append(rows[key])

        elif key in cols:

            new_move['cols'].append(cols[key])
Exemplo n.º 2
0
def input_data(w):
    if w == 0:
        A = np.array([[5, 3]])
        b = np.array([4])
        c = np.array([-3, 4])
    if w == 1:
        A = np.array([[3, 2], [0, 1]])
        b = np.array([4, 3])
        c = np.array([-1, -1])
    if w == 2:  # Expected output is c^{T} x* = -17700 at x* = [650, 1100]
        A = np.array([[1, 0], [0, 1], [1, 1], [4, 2]])
        c = np.array([-12, -9])
        b = np.array([1000, 1500, 1750, 4800])
    if w == 3:  # expected output is c^{T} x* = -88 at x* = [12, 8]
        A = np.array([[2, 1], [2, 3]])
        c = np.array([-4, -5])
        b = np.array([32, 48])
    if w == 4:  #The problem is not feasible
        A = np.array([[1, 1], [-1, -1]])
        c = np.array([2, -3])
        b = np.array([8, -11])
    if w == 5:  # Expected output is c^{T} x* = -8. 5 at x* = [0.5 3.5 0.  0.  0.  2.5]
        A = np.array([[1, 1, 2], [2, 0, 1], [2, 1, 3]])
        c = np.array([-3, -2, -4])
        b = np.array([4, 1, 7])
    if w == 6:  # Expected output is c^{T} x* = -25 at x* = [3, 0, 4, 0, 0]
        A = np.array([[2, 0, -1, 2, -1], [-1, 3, 1, -1, 2]])
        b = np.array([2, 1])
        c = np.array([-3, 1, -4, -1, -2])
    if w == 7:  # Expected output is c^{T} x* = 85 at x* = [7.5, 0, 2.5, 0, 0]
        A = np.array([[-1, 1, -1, 1, 1], [-1, -4, 1, 3, 1]])
        b = np.array([-10, -5])
        c = np.array([9, 16, 7, -3, -1])
    if w == 8:  # Expected output is c^{T} x* = -3 at x* = [2, 1, 0, 0, 0] !!!!
        A = np.array([[1, 2], [2, -1], [0, 1]])
        c = np.array([-1, -1])
        b = np.array([4, 3, 1])
    if w == 9:  # Example Unlimited problem
        A = np.array([[-2, -1, -1, -10], [1, 1, 1, 0]])
        c = np.array([3, 6, 7, 1])
        b = np.array([-100, 1])
    if w == 10:  # Example in cycle, need Bland's rule
        A = np.array([[0.25, -60, -0.04, 9], [0.5, -90, -0.02, 3],
                      [0, 0, 1, 0]])
        c = np.array([-0.75, 150, -0.02, 6])
        b = np.array([0, 0, 1])
    if w == 11:  # Example in cycle, need Bland's rule
        A = np.array([[1, 1], [3, 1], [0, 1]])
        c = np.array([-2, -1])
        b = np.array([5, 9, 4])
    if w == 12:
        A = np.array([[2, 3, 1], [4, 1, 2], [3, 4, 2]])
        c = np.array([-5, -4, -3])
        b = np.array([5, 11, 8])
    if w == 13:
        A = np.array([[-1, 3], [1, 1], [2, -1]])
        c = np.array([-3, -2])
        b = np.array([12, 8, 10])
    if w == 14:  # x* = [3. 0. 0. 2. 0.]. optimal cost -9
        A = np.array([[1, 2, 3], [2, 2, 1]])
        b = np.array([5, 6])
        c = np.array([-3, 4, 5])
    if w == 15:
        A = np.array([[2, -1, 4], [1, 1, 2], [-2, 1, -1]])
        b = np.array([4, 5, -1])
        c = np.array([5, 3, 0])
    if w == 16:  #  x* = [0.2 0.  1.6 0.  0.  4. ]. c* = -5.4
        A = np.array([[2, 1, 1], [1, 2, 3], [2, 2, 1]])
        b = np.array([2, 5, 6])
        c = np.array([-3, -1, -3])
    if w == 17:
        A = np.array([[1, 0], [1, 2], [3, 2]])
        c = np.array([-5, -4])
        b = np.array([4, 10, 16])
    if w == 18:
        A = np.array([[8, 12, 13, 64, 22, 41], [8, 12, 13, 75, 22, 41],
                      [3, 6, 4, 18, 6, 4], [5, 10, 8, 32, 6, 12],
                      [5, 13, 8, 42, 6, 20], [5, 13, 8, 48, 6, 20],
                      [0, 0, 0, 0, 8, 0], [3, 0, 4, 0, 8, 0],
                      [3, 2, 4, 0, 8, 4], [3, 2, 4, 8, 8, 4]])
        c = np.array([100, 600, 1200, 2400, 500, 2000])
        c = -c
        b = np.array([80, 96, 20, 36, 44, 48, 10, 18, 22, 24])
    if w == 19:
        A = np.array([[6, 3], [12, -3]])
        b = np.array([18, 0])
        c = np.array([-1, 0])
    if w == 20:
        A = np.array([[1, 1], [-1, -1]])
        b = np.array([2, -3])
        c = np.array([-8, -11])
    if w == 21:
        c = np.array(
            [600.1, 310.5, 1800, 3850, 18.6, 198.7, 882, 4200, 402.5, 327])
        c = -c
        A = np.array([[20, 5, 100, 200, 2, 4, 60, 150, 80, 40],
                      [20, 7, 130, 280, 2, 8, 110, 210, 100, 40],
                      [60, 3, 50, 100, 4, 2, 20, 40, 6, 12],
                      [60, 8, 70, 200, 4, 6, 40, 70, 16, 20],
                      [60, 13, 70, 250, 4, 10, 60, 90, 20, 24],
                      [60, 13, 70, 280, 4, 10, 70, 105, 22, 28],
                      [5, 2, 20, 100, 2, 5, 10, 60, 0, 0],
                      [45, 14, 80, 180, 6, 10, 40, 100, 20, 0],
                      [55, 14, 80, 200, 6, 10, 50, 140, 30, 40],
                      [65, 14, 80, 220, 6, 10, 50, 180, 30, 50]])
        b = np.array([450, 540, 200, 360, 440, 480, 200, 360, 440, 480])
        A = np.vstack((A, np.identity(10)))
        b1 = np.ones(10)
        b = np.hstack((b, b1))
    if w == 22:
        c = np.array([
            560, 1125, 300, 620, 2100, 431, 68, 328, 47, 122, 322, 196, 41, 25,
            425, 4260, 416, 115, 82, 22, 631, 132, 420, 86, 42, 103, 215, 81,
            91, 26, 49, 420, 316, 72, 71, 49, 108, 116, 90, 738, 1811, 430,
            3060, 215, 58, 296, 620, 418, 47, 81
        ])
        c = -c
        A = np.array(
            [[
                40, 91, 10, 30, 160, 20, 3, 12, 3, 18, 9, 25, 1, 1, 10, 280,
                10, 8, 1, 1, 49, 8, 21, 6, 1, 5, 10, 8, 2, 1, 0, 10, 42, 6, 4,
                8, 0, 10, 1, 40, 86, 11, 120, 8, 3, 32, 28, 13, 2, 4
            ],
             [
                 16, 92, 41, 16, 150, 23, 4, 18, 6, 0, 12, 8, 2, 1, 0, 200,
                 20, 6, 2, 1, 70, 9, 22, 4, 1, 5, 10, 6, 4, 0, 4, 12, 8, 4, 3,
                 0, 10, 0, 6, 28, 93, 9, 30, 22, 0, 36, 45, 13, 2, 2
             ],
             [
                 38, 39, 32, 71, 80, 26, 5, 40, 8, 12, 30, 15, 0, 1, 23, 100,
                 0, 20, 3, 0, 40, 6, 8, 0, 6, 4, 22, 4, 6, 1, 5, 14, 8, 2, 8,
                 0, 20, 0, 0, 6, 12, 6, 80, 13, 6, 22, 14, 0, 1, 2
             ],
             [
                 8, 71, 30, 60, 200, 18, 6, 30, 4, 8, 31, 6, 3, 0, 18, 60,
                 21, 4, 0, 2, 32, 15, 31, 2, 2, 7, 8, 2, 8, 0, 2, 8, 6, 7, 1,
                 0, 0, 20, 8, 14, 20, 2, 40, 6, 1, 14, 20, 12, 0, 1
             ],
             [
                 38, 52, 30, 42, 170, 9, 7, 20, 0, 3, 21, 4, 1, 2, 14, 310, 8,
                 4, 6, 1, 18, 15, 38, 10, 4, 8, 6, 0, 0, 3, 0, 10, 6, 1, 3, 0,
                 3, 5, 4, 0, 30, 12, 16, 18, 3, 16, 22, 30, 4, 0
             ]])
        A = np.vstack((A, np.identity(50)))
        b1 = np.ones(50)
        b = np.hstack(([800, 650, 550, 550, 650], b1))
    if w == 23:
        c = np.array([1, 8, 9, 2, 7, 3, 0, 0])
        A = np.array([[-1, -1, -1, 0, 0, 0, 0, 0], [-1, 0, 0, 1, 1, 0, 0, 0],
                      [0, -1, 0, -1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 1, 0, 0]])
        b = np.array([-1, 0, 0, 1])
    if w == 24:
        b = np.array([-1, -2.5, -3, 1, 2.5, 3])
        A = np.array([[0, -1, -1, 0, 0], [-2, -1, 0, -1, 0],
                      [-4, -1, 0, 0, -1], [0, 1, -1, 0, 0], [2, 1, 0, -1, 0],
                      [4, 1, 0, 0, -1]])
        c = np.array([0, 0, 1, 1, 1])
    if w == 25:
        c = np.array([-3500, -1350, -450, -250, 300, 300])
        A = np.array([[0, 0, 0, 0, 0.1, -0.9], [0, 0, 0, 0, -0.1, 0.9],
                      [0.82, -0.18, 0, 0, 0, 0], [-0.82, 0.18, 0, 0, 0, 0],
                      [0, 0, 0, 0, 1, 0], [1, 1, 0, 0, 0,
                                           0], [1, 0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0],
                      [-1, -1, 0, 0, 0.16, 0], [1, 1, 0, 0, -0.16, 0],
                      [0, 0, -0.5, -0.22, 0.11, 0.11],
                      [0, 0, 0.5, 0.22, -0.11, -0.11]])
        b = np.array([0, 0, 0, 0, 300, 450, 40, 30, 50, 0, 0, 0, 0])
    if w == 26:  # standard form LP problem
        c = np.array([1, 1, 1, 1, 1, 1, 1])
        A = np.array([[-1, 0, 0, -1, -1, -1, -1], [-1, -1, 0, 0, -1, -1, -1],
                      [-1, -1, -1, 0, 0, -1, -1], [-1, -1, -1, -1, 0, 0, -1],
                      [-1, -1, -1, -1, -1, 0, 0], [0, -1, -1, -1, -1, -1, 0],
                      [0, 0, -1, -1, -1, -1, -1]])
        b = np.array([-8, -6, -6, -6, -6, -10, -10])
    if w == 27:
        c = np.array([3, 2])  # Solution b and optimal cost 0
        A = np.array([[1, -2], [1, -1], [2, -1], [1, 0], [2, 1], [1, 1],
                      [1, 2], [0, 1]])
        b = np.array([1, 2, 6, 5, 16, 12, 21, 10])
    if w == 28:
        c = np.array([6, 8, 5, 9])
        A = np.array([[2, 1, 1, 3], [1, 3, 1, 2]])
        b = np.array([5, 3])
    if w == 29:
        (A, b, c) = forest()
    if w == 30:
        (A, b, c) = ssteel()
    if w == 31:
        (A, b, c) = tubprod()
    if w == 32:  # Quick aid
        (A, b, c) = qa()
    if w == 33:  # Bank
        (A, b, c) = onb()
    return A, b, c
Exemplo n.º 3
0
def overnight():
	global OPENING
	global ENDGAME
	global MIDGAME
	costofthisprogrambecomingskynet = 999999999999999999999
	parser = Parser()
	games = parser.read_uci(sys.argv[1])
	scored = parser.parseStockfish(games,sys.argv[2])

	validationgames = parser.read_uci(sys.argv[3])
	validationscored = parser.parseStockfish(validationgames,sys.argv[4])

	training_features = set2(scored)
	training_labels_w = getLabels(scored,WHITE)
	training_labels_b = getLabels(scored,BLACK)

	validation_features = set2(validationscored)
	validation_labels_w = getLabels(validationscored,WHITE)
	validation_labels_b = getLabels(validationscored,BLACK)

	f = open("knnpartition","w")
	try:
		#opening/endgame/midgame for each model
		minscore = 10000
		for i in range(10):
			for j in range(10-i):
				OPENING = float(i)/10
				MIDGAME = float(j)/10
				ENDGAME = float(10 - i - j)/10
				print(OPENING,MIDGAME,ENDGAME)
				training_features = set2(scored)
				validation_features = set2(validationscored)


				y_w = knn(training_features,training_labels_w,validation_features)
				y_b = knn(training_features,training_labels_b,validation_features)


				error_w = meanError(validation_labels_w,y_w)
				error_b = meanError(validation_labels_b,y_b)
				error_t = (error_b+error_w)/2 #mean
				st = "Got score of " + str(error_t) + " with " + str(i) + " " + str(j) + " " + str(10-i-j)
				if error_t < minscore:
					minscore = error_t
					print(st)
				f.write(st+"\n")
		f.close()
	except:
		f.close()

	f = open("lrpartition","w")
	try:
		#opening/endgame/midgame for each model
		minscore = 10000
		for i in range(10):
			for j in range(10-i):
				OPENING = float(i)/10
				MIDGAME = float(j)/10
				ENDGAME = float(10 - i - j)/10
				print(OPENING,MIDGAME,ENDGAME)
				training_features = set2(scored)
				validation_features = set2(validationscored)


				y_w = lr(training_features,training_labels_w,validation_features)
				y_b = lr(training_features,training_labels_b,validation_features)


				error_w = meanError(validation_labels_w,y_w)
				error_b = meanError(validation_labels_b,y_b)
				error_t = (error_b+error_w)/2 #mean
				st = "Got score of " + str(error_t) + " with " + str(i) + " " + str(j) + " " + str(10-i-j)
				if error_t < minscore:
					minscore = error_t
					print(st)
				f.write(st+"\n")
		f.close()
	except:
		f.close()

	f = open("treepartition","w")
	try:
		#opening/endgame/midgame for each model
		minscore = 10000
		for i in range(10):
			for j in range(10-i):
				OPENING = float(i)/10
				MIDGAME = float(j)/10
				ENDGAME = float(10 - i - j)/10
				print(OPENING,MIDGAME,ENDGAME)
				training_features = set2(scored)
				validation_features = set2(validationscored)


				y_w = tree(training_features,training_labels_w,validation_features)
				y_b = tree(training_features,training_labels_b,validation_features)


				error_w = meanError(validation_labels_w,y_w)
				error_b = meanError(validation_labels_b,y_b)
				error_t = (error_b+error_w)/2 #mean
				st = "Got score of " + str(error_t) + " with " + str(i) + " " + str(j) + " " + str(10-i-j)
				if error_t < minscore:
					minscore = error_t
					print(st)
				f.write(st+"\n")
		f.close()
	except:
		f.close()

	f = open("forestpartition","w")
	try:
		#opening/endgame/midgame for each model
		minscore = 10000
		for i in range(10):
			for j in range(10-i):
				OPENING = float(i)/10
				MIDGAME = float(j)/10
				ENDGAME = float(10 - i - j)/10
				print(OPENING,MIDGAME,ENDGAME)
				training_features = set2(scored)
				validation_features = set2(validationscored)


				y_w = forest(training_features,training_labels_w,validation_features)
				y_b = forest(training_features,training_labels_b,validation_features)


				error_w = meanError(validation_labels_w,y_w)
				error_b = meanError(validation_labels_b,y_b)
				error_t = (error_b+error_w)/2 #mean
				st = "Got score of " + str(error_t) + " with " + str(i) + " " + str(j) + " " + str(10-i-j)
				if error_t < minscore:
					minscore = error_t
					print(st)
				f.write(st+"\n")
		f.close()
	except:
		f.close()


	OPENING = 0.4
	MIDGAME = 0.3
	ENDGAME = 0.3
	training_features = set2(scored)
	validation_features = set2(validationscored)

	f = open("knnoverfit","w")
	for i in range(100):
		try:
			y_w = knnO(training_features,training_labels_w,validation_features,i)
			y_b = knnO(training_features,training_labels_b,validation_features,i)


			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i)
			f.write(st+"\n")
		except:
			pass
	f.close()

	f = open("treeoverfit","w")
	for i in range(100):
		try:
			y_w = treeO(training_features,training_labels_w,validation_features,i)
			y_b = treeO(training_features,training_labels_b,validation_features,i)


			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i)
			f.write(st+"\n")
		except:
			pass
	f.close()			

	f = open("forestoverfit","w")
	for i in range(100):
		try:
			y_w = forestO(training_features,training_labels_w,validation_features,i)
			y_b = forestO(training_features,training_labels_b,validation_features,i)

			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i)
			f.write(st+"\n")
		except:
			pass
	f.close()	

	f = open("knnoverfit2","w")
	try:
		i = 500

		y_w = knnO(training_features,training_labels_w,validation_features,i)
		y_b = knnO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 1000

		y_w = knnO(training_features,training_labels_w,validation_features,i)
		y_b = knnO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 3000

		y_w = knnO(training_features,training_labels_w,validation_features,i)
		y_b = knnO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 4500

		y_w = knnO(training_features,training_labels_w,validation_features,i)
		y_b = knnO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")
	except:
		pass
	f.close()	

	f = open("forestoverfit2","w")
	try:
		i = 500

		y_w = forestO(training_features,training_labels_w,validation_features,i)
		y_b = forestO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 1000

		y_w = forestO(training_features,training_labels_w,validation_features,i)
		y_b = forestO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 3000

		y_w = forestO(training_features,training_labels_w,validation_features,i)
		y_b = forestO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 4500

		y_w = forestO(training_features,training_labels_w,validation_features,i)
		y_b = forestO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")
	except:
		pass
	f.close()	

	f = open("treeoverfit2","w")
	try:
		i = 500

		y_w = treeO(training_features,training_labels_w,validation_features,i)
		y_b = treeO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 1000

		y_w = treeO(training_features,training_labels_w,validation_features,i)
		y_b = treeO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 3000

		y_w = treeO(training_features,training_labels_w,validation_features,i)
		y_b = treeO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")

		i = 4500

		y_w = treeO(training_features,training_labels_w,validation_features,i)
		y_b = treeO(training_features,training_labels_b,validation_features,i)


		error_w = meanError(validation_labels_w,y_w)
		error_b = meanError(validation_labels_b,y_b)
		error_t = (error_b+error_w)/2 #mean
		st = "Got score of " + str(error_t) + " with " + str(i)
		f.write(st+"\n")
	except:
		pass
	f.close()	

	i = 101

	f = open("lastoverfits","w")
	while 1:
		i+=2
		try:
			y_w = knnO(training_features,training_labels_w,validation_features,i)
			y_b = knnO(training_features,training_labels_b,validation_features,i)


			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i) + " knn"
			f.write(st+"\n")

			y_w = forestO(training_features,training_labels_w,validation_features,i)
			y_b = forestO(training_features,training_labels_b,validation_features,i)


			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i) + " tree"
			f.write(st+"\n")

			y_w = forestO(training_features,training_labels_w,validation_features,i)
			y_b = forestO(training_features,training_labels_b,validation_features,i)


			error_w = meanError(validation_labels_w,y_w)
			error_b = meanError(validation_labels_b,y_b)
			error_t = (error_b+error_w)/2 #mean
			st = "Got score of " + str(error_t) + " with " + str(i) + " forest"
			f.write(st+"\n")
		except:
			pass
	f.close
Exemplo n.º 4
0
from gs import gamestate
import copy
from models import forest
forestmodel =forest(pikl='data/fullforest.pikl')
from stats import analyze as an
from stats import validCoords
from models import forest
import numpy as np
from simple_search import rootNode

def MO(move):

    places=['a','b','c','d','e','f','g']
    rows={'1':0,'2':1,'3':2,'4':3,'5':4,'6':5,'7':6,'8':7}
    pieces=['N','K','B','R','Q']
    cols = {'a':7, 'b': 6, 'c':5, 'd':4, 'e':3, 'f':2, 'g':1, 'h':0}
    pieces = {'N':3, 'B':4, 'Q':9, 'K':1000, 'R':5}
    move=list(move)
    new_move = {'cols':[], 'rows':[], 'piece':[], 'type':'', 'castles':[]}

    for key in list(move):

        if key in rows:

            new_move['rows'].append(rows[key])

        elif key in cols:

            new_move['cols'].append(cols[key])

        elif key in pieces:
Exemplo n.º 5
0
def knnScoresAll():
	#use knn using scores and other features
	parser = Parser()
	print("PARSING")
	games = parser.read_uci(sys.argv[1])
	scored = parser.parseStockfish(games,sys.argv[2])

	validationgames = parser.read_uci(sys.argv[3])
	validationscored = parser.parseStockfish(validationgames,sys.argv[4])

	print("Building Features/Labels")
	#Now I'm going to run KNN which is going to return y_
	training_features = []
	#finding longest game
	longest = 0
	for i in scored:
		if len(i.turns) > longest:
			longest = len(i.turns)
	for i in validationscored:
		if len(i.turns) > longest:
			longest = len(i.turns)
	#have longest game
	for i in scored:
		game = []
		for j in range(len(i.turns)):
			game.append(i.turns[j].moves[0].rating)
			game.append(i.turns[j].moves[1].rating)
		for k in range(longest):
			if k < j:
				continue
			game.append(0)
			game.append(0)
		game.append(numturns(i)) #number of turns in the game
		game.append(averagescore(i)) #average score
		game.append(numlosingturns(i)) #% of game where white is ahead
		game.append(averageopening(i)) # average score in opening
		game.append(scoreopening(i)) #score at end of opening
		game.append(averagemidgame(i)) #average score in midgame
		game.append(scoremidgame(i)) #score at/near end of midgame
		game.append(averageendgame(i)) #average score in endgame
		game.append(lastscore(i)) #score of last white move at end of the game

		game.append(totalscore(i)) #gets total change in score

		game.append(averagescorechange(i)) #average amount the score changes per move
		game.append(minimumscore(i)) #minimum score in the game
		game.append(maximumscore(i)) #maximum score in the game
		game.append(scoreswitch(i)) #counts number of times who is ahead changes
		training_features.append(game)



	training_labels_w = getLabels(scored,WHITE)
	training_labels_b = getLabels(scored,BLACK)

	validation_features = []

	for i in validationscored:
		game = []
		for j in range(len(i.turns)):
			game.append(i.turns[j].moves[0].rating)
			game.append(i.turns[j].moves[1].rating)
		for k in range(longest):
			if k < j:
				continue
			game.append(0)
			game.append(0)
		game.append(numturns(i)) #number of turns in the game
		game.append(averagescore(i)) #average score
		game.append(numlosingturns(i)) #% of game where white is ahead
		game.append(averageopening(i)) # average score in opening
		game.append(scoreopening(i)) #score at end of opening
		game.append(averagemidgame(i)) #average score in midgame
		game.append(scoremidgame(i)) #score at/near end of midgame
		game.append(averageendgame(i)) #average score in endgame
		game.append(lastscore(i)) #score of last white move at end of the game

		game.append(totalscore(i)) #gets total change in score

		game.append(averagescorechange(i)) #average amount the score changes per move
		game.append(minimumscore(i)) #minimum score in the game
		game.append(maximumscore(i)) #maximum score in the game
		game.append(scoreswitch(i)) #counts number of times who is ahead changes
		validation_features.append(game)
	validation_labels_w = getLabels(validationscored,WHITE)
	validation_labels_b = getLabels(validationscored,BLACK)

	print("Running KNN")
	y_w = forest(training_features,training_labels_w,validation_features)
	y_b = forest(training_features,training_labels_b,validation_features)

	print("Calculating Error")
	error_w = meanError(validation_labels_w,y_w)
	error_b = meanError(validation_labels_b,y_b)
	error_t = (error_b+error_w)/2 #mean

	print(str(error_w) + " White error")
	print(str(error_b) + " Black error")
	print(str(error_t) + " Total error")
	return