Beispiel #1
0
def classificador():
    global classifier
    global txErro
    global real
    #global classifierInit

    classifierInit = list()
    message = request.get_json(silent=True)
    #nSolution = np.asarray(message["solucoes"]) #passa o numero de exemplos na posicao 0
    #nObj = np.asarray(message["objetivos"]) #passa o numero de variaveis na posicao 0
    processar = message["processar"]
    nObj = np.asarray(message["objetivos"])

    print("classificador:", processar)
    if processar == "SVM":
        classifierInit = SVR(kernel='rbf', C=1e3, gamma=0.1, tol=0.01)
    elif processar == "TREE":
        for i in range(nObj[0]):
            classifierInit.append(
                DecisionTreeRegressor(max_depth=500,
                                      min_samples_split=2,
                                      random_state=0,
                                      criterion='mse'))
    elif processar == "RAMDOMFOREST":
        for i in range(nObj[0]):
            classifierInit.append(
                RandomForestRegressor(n_estimators=200,
                                      max_depth=None,
                                      min_samples_split=2,
                                      random_state=0,
                                      criterion='mse'))
    elif processar == "MLP":
        classifierInit = MLPRegressor(hidden_layer_sizes=(10, ), max_iter=1000)
    elif processar == "NO-SURROGATE":
        classifierInit = None

    classifier = classifierInit

    return json.dumps({"retorno": []})
Beispiel #2
0
def classificador():
    global classifier
    global txErro
    global real
    global classifier_name
    global population_size
    global not_first_run
    global classifier_num_of_epochs
    global classifier_avr_opt
    global classifier_timestep
    global history_nSolution

    try:
        not_first_run
    except NameError:
        archive_old_mse()
        not_first_run = True

    classifier_num_of_epochs = None

    classifierInit = list()
    message = request.get_json(silent=True)
    #nSolution = np.asarray(message["solucoes"]) #passa o numero de exemplos na posicao 0
    #nObj = np.asarray(message["objetivos"]) #passa o numero de variaveis na posicao 0

    # Message id doesn't make sense because it's the same
    # object used in other cases.
    classifier_name = message['algoritmo']
    tagProblem = message['processar']
    nObj = np.asarray(message['objetivos'])
    if nObj[0] == 3:
        population_size = 92
    else:
        population_size = 764

    if classifier_name.startswith("SVM"):
        classifierInit = SVR(kernel='rbf', C=1e3, gamma=0.1, tol=0.01)
    elif classifier_name.startswith("TREE"):
        for i in range(nObj[0]):
            classifierInit.append(
                DecisionTreeRegressor(max_depth=500,
                                      min_samples_split=2,
                                      random_state=0,
                                      criterion='mse'))
    elif classifier_name.startswith("RAMDOMFOREST"):
        for i in range(nObj[0]):
            classifierInit.append(
                RandomForestRegressor(n_estimators=200,
                                      max_depth=None,
                                      min_samples_split=2,
                                      random_state=0,
                                      criterion='mse'))
    elif classifier_name.startswith("MLP"):
        classifierInit = MLPRegressor(hidden_layer_sizes=(10, ), max_iter=1000)
    elif classifier_name.startswith(
            "NO-SURROGATE") or classifier_name.startswith("RANDOM"):
        classifierInit = None
    elif classifier_name.startswith("LSTM") or classifier_name.startswith(
            "RNN"):
        history_nSolution = []

        # Clear any session, in case it's not the first one.
        keras.backend.clear_session()

        # Clear GPU memory
        cuda.select_device(0)
        cuda.close()

        # Clear classifiers if they exist
        if 'classifier' in globals():
            del classifier
        gc.collect()

        # Parametrization
        if tagProblem.startswith('WFG'):
            if population_size == 92:
                amntVarDir = 14
            else:
                amntVarDir = 19
        elif tagProblem.startswith('DTLZ'):
            if population_size == 92:
                amntVarDir = 12
            else:
                amntVarDir = 19
        else:
            # Invalid dimension
            amntVarDir = -1

        info = dict()
        model_name = classifier_name.split('_')[0]
        for arg in classifier_name.split('_'):
            if '=' in arg:
                key, value = arg.split('=')
                try:
                    info[key] = int(value)
                except:
                    info[key] = value

        amnt_hidden_nodes = info['amntNodesHidden']
        if amnt_hidden_nodes.startswith('PROP'):
            multi = float(amnt_hidden_nodes.split('-')[1]) / 100
            EPS = 0.0001
            amnt_hidden_nodes = int(amntVarDir * multi + EPS)

        classifier_num_of_epochs = info['amntEpochs']
        classifier_timestep = info['ts']
        if ('FIXED-2' in classifier_name):
            output_labels = 1
            if ('avr' in classifier_name):
                classifier_avr_opt = True
                input_shape = (classifier_timestep, 1)
            else:
                classifier_avr_opt = False
                input_shape = (classifier_timestep, amntVarDir)
        else:
            output_labels = population_size
            if ('avr' in classifier_name):
                classifier_avr_opt = True
                input_shape = (classifier_timestep, population_size)
            else:
                classifier_avr_opt = False
                input_shape = (classifier_timestep,
                               population_size * amntVarDir)

        for i in range(nObj[0]):
            classifierInit.append(
                NeuralNetwork(model_name, input_shape, output_labels,
                              amnt_hidden_nodes))
    classifier = classifierInit

    return json.dumps({"retorno": []})