Ejemplo n.º 1
0
def _fit_model(epochs):
    # try:
    #     global model
    #     global training_input
    #     global training_output
    #
    #     log.info('Fitting model')
    #
    #     model.fit(training_input, training_output, epochs=epochs)    #Not currently working
    #
    #
    #     log.info('\tModel fitted\n')
    #     return True
    #
    # except:
    #     log.error('\tUnknown error in NeuralNetwork._fit_model\n')
    #     return False

    global model
    global training_input
    global training_output

    print(model.summary())

    log.info('Fitting model')

    model.fit(training_input, training_output,
              epochs=epochs)  #Not currently working

    log.info('\tModel fitted\n')
    return True
Ejemplo n.º 2
0
    def save_to_db(self, url, r):
        while True:
            if r.ready():
                result = r.result
                log.info('{:30} 查询结果: {}'.format(url, result.__repr__()))

                # 更新数据库数据

                break
Ejemplo n.º 3
0
def file_import(file_path, db=None):
    log.info('正在向数据库导入 {} 文件'.format(file_path))
    with OpenMysqlConn(db) as cursor:
        with open(file_path, 'r') as file_hand:
            for sql in file_hand.read().split(';'):
                if sql == '\n':
                    continue
                log.info('执行SQL: {}'.format(sql))
                cursor.execute(sql)
Ejemplo n.º 4
0
def _quit(code=0):
    """Cleanly closes the game"""
    # In error codes, 0 = clean close, no errors
    # 1 = closed with errors, very bad
    if code == 1:
        log.critical("Game quiting with errors!")
    else:
        log.info("Game quiting...")
    pygame.display.quit()
    pygame.quit()
    exit(code)
Ejemplo n.º 5
0
def run():
    all_thd = []
    for _ in range(THREAD_NUM):
        log.info('正在启动线程 {}'.format(_))
        t = Spider()
        all_thd.append(t)
        t.daemon = True
        t.start()

    for t in all_thd:
        t.join()
Ejemplo n.º 6
0
 def wrapper(self, count=0, *args, **kwargs):
     try:
         return fun(self, *args, **kwargs)
     except requests.exceptions.ConnectionError:
         log.error('{} 不存在'.format(self._url))
     except Exception as e:
         if count < retry_num:
             count += 1
             log.info("{}: 第{}次重试".format(self._url, count))
             time.sleep(1)
             return wrapper(self, count, *args, **kwargs)
         else:
             raise Exception(e)
Ejemplo n.º 7
0
def _add_input_layer():
    try:
        global model

        log.info('Adding input layer')

        model.add(keras.layers.Flatten(input_shape=(42)))

        log.info('\tInput layer added\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._add_input_layer\n')
        return False
def _create_model():
    try:
        global model

        log.info('Creating network model')

        model = Sequential()

        log.info('\tNetwork model created\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._create_model\n')
        return False
Ejemplo n.º 9
0
def _get_AI_move(playField):

    field = _flatten_field(playField)
    moves = AI.predict(
        field)  #Should return array eg: [0.1, 0.1, 0.3, 0.8, 0.4, 0.2, 0.1]

    best_move = np.argmax(
        moves)  #Returns location of highest val, only first occurrence

    while not _validate_move(playField, best_move):
        moves[best_move] = 0.0
        best_move = np.argmax(moves)

    log.info("AI selected move at {}".format(best_move))
    return best_move
Ejemplo n.º 10
0
def _compile_model():
    try:
        global model

        log.info('Network compiling')

        model.compile(optimizer='adam',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])

        log.info('\tNetwork compiled\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._compile_model\n')
        return False
def _evaluate_model():
    try:
        log.info('Evaluating model')

        global model
        global test_input  # Test data doesn't exist, not strictly required
        global test_output  # Unless you want to make 1,000 more data sets

        accuracy = model.evaluate(test_input, test_output)

        log.info('\tModel evaluated at {}% loss\n'.format(accuracy))
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._evaluate_model\n')
        return False
Ejemplo n.º 12
0
def exportPlay(column):
    """
    Converts play to an array and exports this data to a text file
    :param column: The players move
    :return:
    """

    global GatherMove
    GatherMove = False  # Ensures that only plays after exported boards are recorded

    dataForExport = []  # Prepare a list for the data to be dumped into
    """
    #Intended AI output would be list of probabilities indicating best move
    #e.g: [0.1, 0.3, 0.5, 0.7, 0.5, 0.2, 0.1]
    #Would indicate best move to be 4th column
    """

    for i in range(0, 7):
        if column == i:
            dataForExport.append(
                1.0)  #For move at col 2 would result in [0,0,1,0,0,0,0]
        else:
            dataForExport.append(0.0)

    # EXPORTING CODE #
    if not os.path.isdir("trainingData"):
        # Verify the desired folder exists, if not, create it
        log.debug("Training Data folder missing... creating")
        os.mkdir("trainingData")

    fileNum = 0
    try:
        while True:
            # this loop makes sure it wont overwrite an existing file, then writes
            filename = "trainingData/ExportedMove{}.txt".format(fileNum)
            if os.path.isfile(filename):
                fileNum += 1
            else:
                f = open(filename, "w")
                f.write(str(dataForExport))
                f.close()
                log.info("Exported current state to {}".format(filename))
                return True
    except Exception as e:
        # Error handling ^-^
        log.error("Failed to export game state: {}".format(e))
        return False
Ejemplo n.º 13
0
def _add_output_layer(size):
    # try:
    global model

    log.info('Adding output layer')
    model.add(keras.layers.Flatten())

    # model.add(keras.layers.Dense(size, activation=tf.nn.softmax, output_size=6))
    model.add(keras.layers.Dense(size, activation=tf.nn.softmax))
    # todo: ValueError: Error when checking target: expected dense_4 to have shape (1,), but got array with shape (6,)

    log.info('\tOutput layer added\n')
    return True

    # except:
    log.error('\tUnknown error in NeuralNetwork._add_output_layer\n')
    return False
Ejemplo n.º 14
0
def _add_hidden_layers(layers, nodes):
    try:
        global model

        log.info('Adding {} hidden layers'.format(layers))

        for _ in range(layers):
            model.add(
                keras.layers.Dense(nodes,
                                   input_shape=(41, ),
                                   activation=tf.nn.relu))

        log.info('\tHidden layers aadded\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._add_hidden_layers\n')
        return False
Ejemplo n.º 15
0
def _add_hidden_layers(layers, nodes):
    # try:
    global model

    log.info('Adding {} hidden layers'.format(layers))

    for _ in range(layers):
        # model.add(keras.layers.Dense(nodes, activation=tf.nn.relu, output_size=42))
        model.add(keras.layers.Dense(nodes, activation=tf.nn.relu))

        # model.add(keras.layers.flatten())

    log.info('\tHidden layers aadded\n')
    return True

    # except:
    log.error('\tUnknown error in NeuralNetwork._add_hidden_layers\n')
    return False
Ejemplo n.º 16
0
 def get_result(self, url, r):
     while True:
         if r.ready():
             try:
                 result = r.result
                 log.info('{:30} 查询共获取到: {}'.format(url, len(result)))
                 for new_url in result:
                     self.conn.lpush('task', new_url)
                     self.conn.lpush('cms_task', new_url)
                 conn = POOL.connection()
                 cursor = conn.cursor()
                 _sql = 'INSERT INTO Domain (domain) VALUES (%s);'
                 cursor.executemany(_sql, result)
                 conn.commit()
                 conn.close()
             finally:
                 break
         time.sleep(1)
def _fit_model(epochs):

    # try:
    #     global model
    #     global training_input
    #     global training_output
    #
    #     log.info('Fitting model')
    #
    #     model.fit(training_input, training_output, epochs=epochs)    #Not currently working
    #
    #
    #     log.info('\tModel fitted\n')
    #     return True
    #
    # except:
    #     log.error('\tUnknown error in NeuralNetwork._fit_model\n')
    #     return False

    # global model
    # global training_input
    # global training_output
    #
    # # print(model.summary())
    #
    # log.info('Fitting model')
    # model.fit(training_input, training_output, epochs=epochs)    # Not currently working
    #
    #
    # log.info('\tModel fitted\n')
    try:
        log.info('Fitting model')
        global model
        model.fit(training_input,
                  training_output,
                  epochs=epochs,
                  batch_size=128)  # Trains neural network

        log.debug("\tModel fitted\n")
        return True
    except:
        log.error("\tFailed to fit model\n")

        return False
Ejemplo n.º 18
0
def _load_model(name):
    try:
        global model

        log.info('Loading model "{}"'.format(name))

        name = name + '.model'

        try:
            new_model = keras.models.load_model(name)
            log.info('\tLoaded model "{}"\n'.format(name))
            return True

        except:
            log.error('\tModel "{}" failed to load\n'.format(name))

    except:
        log.error('\tUnknown error in NeuralNetwork._load_model\n')
        return False
def _save_model(name):
    try:
        global model

        log.info('Saving model')

        name = name + '.model'

        try:
            model.save(name)

            log.info('\tModel saved as {}'.format(name))

        except:
            log.error('\tFailed to save model as {}'.format(name))
            return False

    except:
        log.error('\tUnknown error in NeuralNetwork._save_model\n')
        return False
Ejemplo n.º 20
0
def _get_AI_move(playField):
    """
    Gets the AIs next move
    :param playField:
    :return: int of move
    """

    global AI

    if not AI:
        log.error(
            "No AI found"
        )  # Ensures that an AI exists, does happen earlier as well, just making sure
        nn._construct("AI")
        nn._save_model("AI")
        log.info("AI constructed")
        AI = True

    field = _flatten_field(playField)
    print(np.array(field).shape)
    moves = nn._predict(
        field)  # Should return array eg: [0.1, 0.1, 0.3, 0.8, 0.4, 0.2, 0.1]

    best_move = np.argmax(
        moves)  # Returns location of highest val, only first occurrence

    while not _validate_move(playField,
                             best_move):  # Ensures return value is valid
        moves[best_move] = 0.0
        if np.sum(moves) == 0:
            for i in range(8):
                if _validate_move(
                        playField,
                        i):  # If AI fails, just uses next available slot
                    return i
            log.error("AI failed to return usable value")
            return False
        best_move = np.argmax(moves)

    log.info("AI selected move at {}".format(best_move))
    return best_move
Ejemplo n.º 21
0
def _get_data():
    try:
        global training_input
        global training_output

        training_input = []
        training_output = []

        log.info('Fetching training data')

        try:
            number_of_files = int(len(os.listdir('trainingData')))
        except:
            log.error('Directory trainingData does not exist')
            return False

        for file_num in range(0, int(number_of_files / 2)):

            state_file = 'trainingData/ExportedState{}.txt'.format(file_num)
            move_file = 'trainingData/ExportedMove{}.txt'.format(file_num)

            state_data = df._format_array_v2(state_file)
            move_data = df._format_array_v2(move_file)

            if not state_data:
                log.error('Failed to load board state data')
                return False

            if not move_data:
                log.error('Failed to load move data')
                return False

            state_data = np.array(state_data)
            move_data = np.array(move_data)

            training_input.append(state_data)
            training_output.append(move_data)

        training_input = np.array(training_input)
        training_output = np.array(training_output)

        log.info('\tData fetched')
        log.info('\t\tTraining_input length: {}\tShape: {}'.format(
            len(training_input), training_input.shape))
        log.info('\t\tTraining_out length: {}\tShape: {}\n'.format(
            len(training_output), training_output.shape))
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._get_data\n')
        return False
def _compile_model():

    try:
        log.info('Compiling model')
        global model

        # log.info('Network compiling')
        #
        # # model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
        # model.compile(optimizer='adam')

        model.compile(optimizer='rmsprop',
                      loss='binary_crossentropy',
                      metrics=['accuracy'])  # trains it using tensoroflow

        log.info('\tModel compiled\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._compile_model\n')
        return False
Ejemplo n.º 23
0
def flattenAndExport(playfield):
    """
    Flattens the 2D array into a 1D array and exports this data to a text file
    :param playfield: The game's playfield
    :return:
    """

    global GatherMove
    GatherMove = True  # The play made at this point to be recorded, for AI target data

    dataForExport = []  # Prepare a list for the data to be dumped into
    for row in playfield:
        for item in row:
            dataForExport.append(
                item)  # dump each item one by one into this new list

    # EXPORTING CODE #
    if not os.path.isdir("trainingData"):
        # Verify the desired folder exists, if not, create it
        log.debug("Training Data folder missing... creating")
        os.mkdir("trainingData")

    fileNum = 0
    try:
        while True:
            # this loop makes sure it wont overwrite an existing file, then writes
            filename = "trainingData/ExportedState{}.txt".format(fileNum)
            if os.path.isfile(filename):
                fileNum += 1
            else:
                f = open(filename, "w")
                f.write(str(dataForExport))
                f.close()
                log.info("Exported current state to {}".format(filename))
                return True
    except Exception as e:
        # Error handling ^-^
        log.error("Failed to export game state: {}".format(e))
        return False
def _load_model(name):
    try:
        global new_model
        global has_new_model

        log.info('Loading model "{}"'.format(name))

        name = name + '.model'

        try:
            new_model = keras.models.load_model(name)
            log.info('\tLoaded model "{}"\n'.format(name))
            has_new_model = True
            # keras.plot_model(new_model, to_file='Model.png')    # Would be a nice idea
            return True

        except:
            log.error('\tModel "{}" failed to load\n'.format(name))

    except:
        log.error('\tUnknown error in NeuralNetwork._load_model\n')
        return False
Ejemplo n.º 25
0
def _add_input_layer():
    # try:
    global model

    log.info('Adding input layer')

    # model.add(keras.layers.Flatten(input_shape=(None, 42), output_size=42))

    # model.add(keras.layers.Flatten)
    model.add(keras.layers.Flatten(input_shape=(
        1000,
        41,
    )))
    # model.add(keras.layers.Input( shape=(1000, 41, )))
    # model.add(keras.layers.Flatten(input_dim=41))

    log.info('\tInput layer added\n')
    return True

    # except:
    log.error('\tUnknown error in NeuralNetwork._add_input_layer\n')
    return False
def _add_hidden_layers(layers, nodes):
    try:
        global model

        log.info('Adding {} hidden layers'.format(layers))

        # for _ in range(layers):
        #     # model.add(keras.layers.Dense(nodes, activation=tf.nn.relu, output_size=42))
        #     model.add(keras.layers.Dense(nodes, activation=tf.nn.relu))
        #     model.add(keras.layers.Dropout(0.01))    # Prevents over-fitting the model while training (memorisation)
        #     # model.add(keras.layers.Flatten())

        for _ in range(layers):
            model.add(Dense(nodes, activation='relu'))
            model.add(Dropout(0.5))

        log.info('\tHidden layers aadded\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._add_hidden_layers\n')
        return False
def _add_output_layer(size):
    try:

        log.info('Adding output layer')
        global model

        # log.info('Adding output layer')
        # print(model.summary())
        # model.add(keras.layers.Flatten())    # todo: Currently cuts off here
        # # todo: Flatten layer expecten min_ndim=3, got ndim=2
        #
        # # model.add(keras.layers.Dense(size, activation=tf.nn.softmax, output_size=6))
        # model.add(keras.layers.Dense(size, activation=tf.nn.softmax))
        # # todo: ValueError: Error when checking target: expected dense_4 to have shape (1,), but got array with shape (6,)    Sorted?

        model.add(Dense(size, activation='sigmoid'))  # output layer

        log.info('\tOutput layer added\n')
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._add_output_layer\n')
        return False
def _add_input_layer():
    # try:
    global model

    log.info('Adding input layer')

    # # model.add(keras.layers.Flatten(input_shape=(None, 42), output_size=42))
    #
    # # model.add(keras.layers.Flatten)
    # model.add(keras.layers.Flatten(input_shape=(1000, 41, 1)))
    # # model.add(keras.layers.Input( shape=(1000, 41, )))
    # # model.add(keras.layers.Flatten(input_dim=41))

    model.add(Dense(
        32, activation='relu',
        input_shape=(42, )))  # relu activiation layer, better perfomance.
    model.add(Dropout(0.5))

    log.info('\tInput layer added\n')
    return True

    # except:
    log.error('\tUnknown error in NeuralNetwork._add_input_layer\n')
    return False
Ejemplo n.º 29
0
def _get_data():
    try:
        global training_input
        global training_output

        training_input = []
        training_output = []

        log.info('Fetching training data')

        number_of_files = int(len(os.listdir('trainingData')))

        for file_num in range(0, int(number_of_files / 2)):

            state_file = 'trainingData/ExportedState{}.txt'.format(file_num)
            move_file = 'trainingData/ExportedMove{}.txt'.format(file_num)

            f = open(state_file, "r")
            data = list(f.read())
            # data = np.load(file)
            data = df._format_array_v2(state_file)
            data = np.array(data)
            training_input.append(data)

            f = open(move_file, "r")
            data = list(f.read())
            # data = np.load(file)
            data = df._format_array_v2(move_file)
            data = np.array(data)
            training_output.append(data)

        training_input = np.array(training_input)
        training_output = np.array(training_output)

        log.info('\tData fetched')
        log.info('\t\tTraining_input length: {}'.format(len(training_input)))
        log.info('\t\tTraining_out length: {}\n'.format(len(training_output)))
        return True

    except:
        log.error('\tUnknown error in NeuralNetwork._get_data\n')
        return False
Ejemplo n.º 30
0
def start_game():
    """
    Initialises the game and begins the main loop

    :return: None
    """
    log.info("Initialising game...")
    playField = _create_playField(
        ROW_COUNT, COLUMN_COUNT
    )  # Creates a playfield of size designated at the top of this file
    log.info("Rendering playfield...")
    if not TestMode:
        renderer(playField)  # Draw the User Interface
        pygame.display.update()  # Refresh the screen so drawing can be seen
    log.info("Ready!")
    _game_loop(playField)  # Start the game loop