Пример #1
0
def grid_search_cv(pipeline, chain_names, chain_hyperparameter_space, dataset_name, verbose=0):
            
    tuned_parameters = chain_hyperparameter_space.get_grid_parameters()
    
    if verbose > 0:
        print("Parameters : values to test")
        for k in tuned_parameters:
          print("  --", k, ":", tuned_parameters[k])                              
        
    """ 
        Grid search from scikit-learn:
    
        GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, 
        n_jobs=1, iid=True, refit=True, cv=None, verbose=0, 
        pre_dispatch=‘2*n_jobs’, error_score=’raise’, return_train_score=’warn’)
        
        param_grid : dict or list of dictionaries
        Dictionary with parameters names (string) as keys and lists of parameter
        settings to try as values, or a list of such dictionaries, in which case
        the grids spanned by each dictionary in the list are explored. This 
        enables searching over any sequence of parameter settings.        
    """    
    
    model = grid_search.GridSearch(pipeline, chain_names, tuned_parameters,
                                   dataset_name, verbose=verbose)
                                          
    return model 
Пример #2
0
    def test_4(self):
        """
        Chromosome: sequence of clients separed by an 'X' when a new vehicle is assigned
        Selection: roulette-wheel
        Crossover operator: simple random crossover
        Mutation operator: simple random mutation
        Elitism is enabled
        Termination criteria: number of generations = 100
        Parameters:
            population_size: 97
            reproduction rate: 0.25
            crossover rate: 0.625
            mutation rate: 0.125
        """
        fname = './input/A-n80-k10.vrp'
        nodes, capacity, distances, demand = self.load_test(fname)

        individual_factory = cvrp.CVRPIndividualFactory(
            nodes, capacity, distances, demand, individual_type='corrected')
        termination_criteria = ga.NumberOfGenerationsTerminationCriteria(
            number_of_generations=100)
        solver = ga.GeneticAlgorithm(individual_factory,
                                     population_size=97,
                                     reproduction=0.25,
                                     crossover=0.625,
                                     mutation=0.125,
                                     elitism=True,
                                     termination_criteria=termination_criteria)

        if self.grid_search:
            params = {
                "population_size":
                numpy.logspace(3, 12, base=2, num=6, dtype=int),
                "operators_rate":
                filter(
                    lambda x: sum(x) == 1.0,
                    itertools.product(numpy.arange(.125, 0.875, .125),
                                      repeat=3)),
                "elitism": [True],
                "termination_criteria": [
                    ga.NumberOfGenerationsTerminationCriteria(
                        number_of_generations=100)
                ]
            }
            grid = grid_search.GridSearch(solver, params)
            grid.search(0.0)
            grid_scores = grid.get_grid_scores()

            fname = './results/correction_operator/A-n80-k10.vrp.grid.csv'
            grid_scores.to_csv(fname, sep=',', index=False)
            sys.stdout.write(
                "Finished. Results are at: ./results/correction_operator/A-n80-k10.vrp.grid.csv\n"
            )
        else:
            sys.stdout.write(
                "Starting test_4: CORRECTION + SIMPLE RANDOM OPERATORS, ELITISM ENABLED\n"
            )
            sys.stdout.write("Input: ./tests/vrp/A-n80-k10.vrp\n")

            solver.init_population()
            solver.evolve()
            info = solver.get_generation_info()
            fname = './results/correction_operator/A-n80-k10.vrp.csv'
            info.to_csv(fname, sep=',', index=False)

            plt.plot(info['generation'],
                     info['min'],
                     "r",
                     label="melhor",
                     linewidth=2)
            plt.plot(info['generation'],
                     info['mean'],
                     "b",
                     label="media",
                     linewidth=2)
            plt.plot(info['generation'], info['std'], "k.", label="desvio")

            legend = plt.legend(loc='lower right', numpoints=1)
            plt.xlabel("geracoes")
            plt.ylabel("fitness")
            plt.yscale('log')
            plt.show()

            sys.stdout.write(
                "Finished. Results are at: ./results/correction_operator/A-n80-k10.vrp.csv\n"
            )
        assert True