Example #1
0
    def run(self, benchmark):
        """Execute the algorithm

        Argument
        --------
        benchmark: str
        """
        if self.__flags.seed is None:
            algorithm = pg.sga(gen=self.__flags.generations,
                               cr=self.__flags.cr,
                               m=self.__flags.m,
                               param_m=self.__flags.param_m,
                               param_s=self.__flags.param_s,
                               crossover=self.__flags.crossover,
                               mutation=self.__flags.mutation,
                               selection=self.__flags.selection)
        else:
            algorithm = pg.sga(gen=self.__flags.generations,
                               cr=self.__flags.cr,
                               m=self.__flags.m,
                               param_m=self.__flags.param_m,
                               param_s=self.__flags.param_s,
                               crossover=self.__flags.crossover,
                               mutation=self.__flags.mutation,
                               selection=self.__flags.selection,
                               seed=self.__flags.seed)

        # Execute
        super().exec(algorithm, benchmark)
def solver(dimension, lower_bound, upper_bound, optim, bias, popsize):
    global algo
    global pop
    global niter
    global log
    global curve
    prob = pg.problem(
        rastrigin_prob(dimension, lower_bound, upper_bound, optim, bias))
    algo = pg.algorithm(
        pg.sga(gen=2000,
               cr=0.9,
               eta_c=2.0,
               m=0.02,
               param_m=1.0,
               param_s=2,
               crossover='binomial',
               mutation='gaussian',
               selection='truncated'))
    algo.set_verbosity(1)
    pop = pg.population(prob, popsize)
    pop = algo.evolve(pop)
    log = algo.extract(pg.sga).get_log()
    curve = [x[2] for x in log]
    niter = log[-1][0]
    return prob, algo, pop, log, niter, curve
Example #3
0
def run_problem(prob, uda=pg.sga(gen=100), population_size=100, verbosity=1):
    # Create algorithm to solve problem with
    algo = pg.algorithm(uda=uda)
    algo.set_verbosity(verbosity)

    # population of problems
    pop = pg.population(prob=prob, size=population_size)

    # solve problem
    pop = algo.evolve(pop)
    return pop
def main(untrained):
    pop = None
    prob = ConvNetSizeProblem(2, untrained=untrained)
    pg.set_global_rng_seed(94874)
    prob = pg.problem(prob)
    algo = pg.algorithm(pg.sga(1, m=0.2, mutation='uniform'))
    pop = pg.population(prob, pop_size)
    algo.set_verbosity(1)
    for g in range(gens):
        f = pop.get_f()
        x = pop.get_x()
        for i in range(len(x)):
            logger.info(str(g) + ';' + str(x[i]) + ';' + str(f[i][0]))
        pop = algo.evolve(pop)
Example #5
0
def archipelago():
    print('interferometer sga archipelago')
    uda = pg.sga(gen=50000)
    # instantiate an unconnected archipelago
    archi = pg.archipelago(t=pg.topologies.unconnected())
    t = time()
    for _ in range(8):
        alg = pg.algorithm(uda)
        #alg.set_verbosity(1)
        prob = pg.problem(udp)
        pop = pg.population(prob, 20)
        isl = pg.island(algo=alg, pop=pop)
        archi.push_back(isl)

    archi.evolve()
    archi.wait_check()
    print(f'archi: {time() - t:0.3f}s')
 def compute_black_box_optimization(self, x: CArray) -> CArray:
     self.problem.init_starting_point(x)
     seed = self.problem.seed if self.problem.seed is not None else 0
     algorithm = pygmo.algorithm(
         pygmo.sga(gen=self.problem.iterations, seed=seed))
     pygmo_problem = pygmo.problem(self.problem)
     pygmo_population = pygmo.population(pygmo_problem,
                                         size=self.problem.population_size,
                                         seed=self.problem.seed)
     minimization_results = algorithm.evolve(pygmo_population)
     best_x = minimization_results.get_x()[minimization_results.best_idx()]
     evolved_problem = minimization_results.problem.extract(
         type(self.problem))
     confidences, fitness = evolved_problem.export_internal_results()
     self.confidences_ = confidences
     self.fitness = fitness
     return CArray(best_x)
Example #7
0
def main(untrained):
    with Environment() as e:
        pop = None
        prob = ConvNetSizeProblem(2, distributed=True, untrained=untrained)
        pg.set_global_rng_seed(45714)
        prob = pg.problem(prob)
        algo = pg.algorithm(pg.sga(1, m=0.2, mutation='uniform'))
        pop = pg.population(prob, pop_size)
        algo.set_verbosity(1)
        for g in range(gens):
            f = pop.get_f()
            x = pop.get_x()
            for i in range(len(x)):
                e.log(
                    str(g) + ';' + np.array2string(
                        x[i], precision=2, separator=',', suppress_small=True)
                    + ';' + str(f[i][0]))
            pop = algo.evolve(pop)
	def compute_black_box_optimization(self, x: CArray) -> CArray:
		self.problem.init_starting_point(x)
		seed = self.problem.seed if self.problem.seed is not None else 0
		# algorithm = pygmo.algorithm(pygmo.sga(gen=self.problem.iterations, seed=seed, crossover='exponential', mutation='gaussian'))
		algorithm = pygmo.algorithm(pygmo.sga(gen=self.problem.iterations, seed=seed))
		algorithm.set_verbosity(1)
		start_t = time.time()
		pygmo_problem = pygmo.problem(self.problem)
		pygmo_population = pygmo.population(pygmo_problem, size=self.problem.population_size, seed=self.problem.seed)
		minimization_results = algorithm.evolve(pygmo_population)
		end_t = time.time()
		evolved_problem = minimization_results.problem.extract(type(self.problem))
		confidences, fitness, sizes = evolved_problem.export_internal_results()
		self.confidences_ = confidences
		self.fitness_ = fitness
		self.sizes_ = sizes
		self.evolved_problem_ = evolved_problem
		self.elapsed_time_ = end_t - start_t
		best_t = minimization_results.champion_x
		return CArray(best_t)
def rosenbrock_2d(x):

    generations = x[0]
    prob = pg.problem(pg.rosenbrock(2))

    # 2 - Instantiate a pagmo algorithm
    algo = pg.algorithm(pg.sga(gen=generations, cr = .90, eta_c = 1., m = 0.02, param_m = 1., param_s = 2, crossover = "exponential", mutation = "polynomial", selection = "tournament", seed = 3))

    # 3 - Instantiate an archipelago with 16 islands having each 20 individuals
    archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20)

    # 4 - Run the evolution in parallel on the 16 separate islands 10 times.
    archi.evolve(10)

    # 5 - Wait for the evolutions to be finished
    archi.wait()

    # 6 - Print the fitness of the best solution in each island
    res = [isl.get_population().champion_f for isl in archi]
    import pdb; pdb.set_trace();
    return res
Example #10
0
def BGA_train(xMin, yMin, xMaj, yMaj, model_old, lr, epoch, batch_size,
              pop_size, gen_max, cx, mx):
    # define the problem
    print('define the problem')
    prob = pg.problem(
        BPFL_fc_imb(xMin, xMaj, yMin, yMaj, model_old, lr, epoch, batch_size))
    # population initialization
    print('population initialization')
    pop_init = pg.population(prob=prob, size=pop_size)
    # genetic algorithm construction
    print('genetic algorithm construction')
    gaalgo = pg.algorithm(
        pg.sga(gen=gen_max,
               cr=cx,
               m=mx,
               param_s=2,
               crossover="single",
               mutation="uniform",
               selection="truncated"))
    # run ga and get the best population
    print('run ga and get the best population')
    pop_new = gaalgo.evolve(pop_init).get_x()
    return pop_new
Example #11
0
def main():
    parser = argparse.ArgumentParser(description="Make a squad from scratch")
    # General parameters
    parser.add_argument(
        "--budget", help="budget, in 0.1 millions", type=int, default=1000
    )
    parser.add_argument("--season", help="season, in format e.g. 1819")
    parser.add_argument("--gw_start", help="gameweek to start from", type=int)
    parser.add_argument(
        "--num_gw", help="how many gameweeks to consider", type=int, default=3
    )
    parser.add_argument(
        "--algorithm",
        help="Which optimization algorithm to use - 'normal' or 'genetic'",
        type=str,
        default="normal",
    )
    # parameters for "normal" optimization
    parser.add_argument(
        "--num_iterations",
        help="number of iterations (normal algorithm only)",
        type=int,
        default=10,
    )
    # parameters for "pygmo" optimization
    parser.add_argument(
        "--num_generations",
        help="number of generations (genetic only)",
        type=int,
        default=100,
    )
    parser.add_argument(
        "--population_size",
        help="number of candidate solutions per generation (genetic only)",
        type=int,
        default=100,
    )
    parser.add_argument(
        "--no_subs",
        help="Don't include points contribution from substitutes (genetic only)",
        action="store_true",
    )
    parser.add_argument(
        "--include_zero",
        help="Include players with zero predicted points (genetic only)",
        action="store_true",
    )
    parser.add_argument(
        "--gw_weight_type",
        help="'constant' to treat all gameweeks equally, or 'linear' to reduce weight of gameweeks with time (genetic only)",
        type=str,
        default="linear",
    )

    args = parser.parse_args()
    if args.season:
        season = args.season
    else:
        season = get_current_season()
    budget = args.budget
    if args.gw_start:
        gw_start = args.gw_start
    else:
        gw_start = NEXT_GAMEWEEK
    gw_range = list(range(gw_start, min(38, gw_start + args.num_gw)))
    tag = get_latest_prediction_tag(season)

    if args.algorithm == "normal":
        from airsenal.framework.optimization_utils import make_new_squad

        num_iterations = args.num_iterations
        best_squad = make_new_squad(args.budget, num_iterations, tag, gw_range, season)

    elif args.algorithm == "genetic":
        from airsenal.framework.optimization_pygmo import make_new_squad

        num_generations = args.num_generations
        population_size = args.population_size
        remove_zero = not args.include_zero
        gw_weight_type = args.gw_weight_type
        uda = pg.sga(gen=num_generations)
        if args.no_subs:
            sub_weights = {"GK": 0, "Outfield": (0, 0, 0)}
        else:
            sub_weights = {"GK": 0.01, "Outfield": (0.4, 0.1, 0.02)}

        best_squad = make_new_squad(
            gw_range,
            tag,
            budget=budget,
            season=season,
            remove_zero=remove_zero,
            sub_weights=sub_weights,
            uda=uda,
            population_size=population_size,
            gw_weight_type=gw_weight_type,
        )
    else:
        raise ValueError("'algorithm' must be 'normal' or 'genetic'")

    points = best_squad.get_expected_points(gw_start, tag)

    print("---------------------")
    print("Best expected points for gameweek {}: {}".format(gw_start, points))
    print("---------------------")
    print(best_squad)
Example #12
0
    def _setOptimizer(self, optimizer):

        if callable(optimizer) == True:

            # User-defined optimizer function

            pass

        elif isinstance(optimizer, str):

            # Pre-defined optimizer

            if optimizer == 'ga':

                gen = self.optimization_configuration['number_of_generations']

                cr = self.optimization_configuration['crossover_rate']

                mr = self.optimization_configuration['mutation_rate']

                elitism = self.optimization_configuration['elitism']

                ind = self.optimization_configuration['number_of_individuals']

                #=================================================================

                crossover_type = self.optimization_configuration[
                    'crossover_type']

                mutation_type = self.optimization_configuration[
                    'mutation_type']

                selection_type = self.optimization_configuration[
                    'selection_type']

                selection_param = self.optimization_configuration[
                    'selection_param']

                #=================================================================

                self._pagmo_selected_algorithm = pg.sga

                self._pagmo_selected_algorithm_log_columns = [
                    'Gen', 'Fevals', 'Best', 'Improvement'
                ]

                algo = pg.algorithm(
                    pg.sga(gen=gen,
                           cr=cr,
                           eta_c=1.,
                           m=mr,
                           param_m=1.,
                           param_s=selection_param,
                           crossover=crossover_type,
                           mutation=mutation_type,
                           selection=selection_type))

                #isl = pg.island(algo, self.optimization_problem, ind)

                self.optimization_mechanism = algo

                return ('ga')

            if optimizer == 'sade':

                gen = self.optimization_configuration['number_of_generations']

                de_ftol = self.optimization_configuration['ftol_de']

                de_xtol = self.optimization_configuration['xtol_de']

                ind = self.optimization_configuration['number_of_individuals']

                #==================================================================

                self._pagmo_selected_algorithm = pg.de1220

                self._pagmo_selected_algorithm_log_columns = [
                    'Gen', 'Fevals', 'Best', 'F', 'CR', 'Variant', 'dx', 'df'
                ]

                algo = pg.algorithm(
                    pg.de1220(gen=gen, ftol=de_ftol, xtol=de_xtol))

                #isl = pg.island(algo, self.optimization_problem, ind)

                self.optimization_mechanism = algo

                return ('sade')

            if optimizer == 'de':

                gen = self.optimization_configuration['number_of_generations']

                cr = self.optimization_configuration['crossover_rate']

                f_w = self.optimization_configuration['scale_factor']

                de_variant = self.optimization_configuration['variant_de']

                de_ftol = self.optimization_configuration['ftol_de']

                de_xtol = self.optimization_configuration['xtol_de']

                ind = self.optimization_configuration['number_of_individuals']

                #==================================================================

                self._pagmo_selected_algorithm = pg.de

                self._pagmo_selected_algorithm_log_columns = [
                    'Gen', 'Fevals', 'Best', 'F', 'CR', 'dx', 'df'
                ]

                algo = pg.algorithm(
                    pg.de(gen=gen,
                          F=f_w,
                          CR=cr,
                          variant=de_variant,
                          ftol=de_ftol,
                          xtol=de_xtol))

                #isl = pg.island(algo, self.optimization_problem, ind)

                self.optimization_mechanism = algo

                return ('de')

            if optimizer == 'pso':

                gen = self.optimization_configuration['number_of_generations']

                omega = self.optimization_configuration['omega_pso']

                eta1 = self.optimization_configuration['eta1_pso']

                eta2 = self.optimization_configuration['eta2_pso']

                vcoeff = self.optimization_configuration['max_v_pso']

                pso_variant = self.optimization_configuration['variant_pso']

                neighb_type = self.optimization_configuration[
                    'neighborhood_type_pso']

                neighb_param = self.optimization_configuration[
                    'neighborhood_param_pso']

                ind = self.optimization_configuration['number_of_individuals']

                #==================================================================

                self._pagmo_selected_algorithm = pg.pso

                self._pagmo_selected_algorithm_log_columns = [
                    'Gen', 'Fevals', 'gbest', 'Mean Vel.', 'Mean lbest',
                    'Avg. Dist.'
                ]

                algo = pg.algorithm(
                    pg.pso(gen=gen,
                           omega=omega,
                           eta1=eta1,
                           eta2=eta2,
                           max_vel=vcoeff,
                           variant=pso_variant))

                #isl = pg.island(algo, self.optimization_problem, ind)

                self.optimization_mechanism = algo

                return ('pso')

        else:

            raise UnexpectedValueError("(decorated function, str)")
Example #13
0
sub_weights = {"GK": 0.01, "Outfield": (0.4, 0.1, 0.02)}

# Can choose not to optimize full squad by changing the number of players per position.
# In that case, to create valid squads during the optimization, the empty slots are
# filled with dummy players. Each dummy player has a cost of 'dummy_sub_cost'.
players_per_position = TOTAL_PER_POSITION
dummy_sub_cost = 45

# Optimisation algorithm
# For the list of algorithms available in pygmo see here:
# https://esa.github.io/pygmo2/overview.html#list-of-algorithms
# Time it will take to run normally controlled by:
#  - "gen" argument of algorithm - no. of generations (no. of times population
#    is evolved)
#  - "population_size" - no. of candidate solutions in each generatino
uda = pg.sga(gen=100)  # ("User Defined Algorithm")
population_size = 100

# -------------------------
#     RUN OPTIMIZATION
# -------------------------
gw_range = list(range(gw_start, min(38, gw_start + num_gw)))

team = make_new_squad(
    gw_range,
    tag,
    uda=uda,
    population_size=population_size,
    sub_weights=sub_weights,
    budget=budget,
    players_per_position=players_per_position,
Example #14
0
    def findPowersRR(self,
                     objectiveFunction="averageThr",
                     sgaGenerations=100,
                     numberOfThreads=11,
                     numOfIndividuals=10,
                     evolveTimes=10,
                     method="global",
                     x_arg=None,
                     y_arg=None,
                     expectedSignalLoss_arg=None):
        if method == "local":
            if x_arg == None:
                x = self.parent.constraintAreaMaxX / 2
            else:
                x = x_arg
            if y_arg == None:
                y = self.parent.constraintAreaMaxY / 2
            else:
                y = y_arg
            if expectedSignalLoss_arg == None:
                maxDistance = min(self.parent.constraintAreaMaxX / 2,
                                  self.parent.constraintAreaMaxY / 2)
            else:
                maxDistance = returnDistanceFromSNR(expectedSignalLoss_arg)
            localBsVector = []
            for bs in self.parent.bs:
                if math.sqrt((bs.x - x)**2 + (bs.y - y)**2) < maxDistance:
                    row = []
                    row.append(int(bs.ID))
                    row.append(math.sqrt((bs.x - x)**2 + (bs.y - y)**2))
                    localBsVector.append(row)
            localBsVector = np.asarray(localBsVector)
        if objectiveFunction == "averageThr":
            if method == "local":
                localListBS = []
                for i in range(len(localBsVector)):
                    localListBS.append(localBsVector[i, 0])
                prob = pg.problem(
                    local_maximalThroughputProblemRR(
                        dim=len(localBsVector),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower,
                        localListBS=localListBS))

            if method == "global":
                prob = pg.problem(
                    maximalThroughputProblemRR(
                        dim=len(self.parent.bs),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower))

        if objectiveFunction == "medianThr":
            if method == "local":
                localListBS = []
                for i in range(len(localBsVector)):
                    localListBS.append(localBsVector[i, 0])
                prob = pg.problem(
                    local_maximalMedianThrProblemRR(
                        dim=len(localBsVector),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower,
                        localListBS=localListBS))

            if method == "global":
                prob = pg.problem(
                    maximalMedianThrProblemRR(
                        dim=len(self.parent.bs),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower))

        if objectiveFunction == "minIQRthr":
            if method == "local":
                localListBS = []
                for i in range(len(localBsVector)):
                    localListBS.append(localBsVector[i, 0])
                prob = pg.problem(
                    local_minInterQuartileRangeroblemRR(
                        dim=len(localBsVector),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower,
                        localListBS=localListBS))

            if method == "global":
                prob = pg.problem(
                    minInterQuartileRangeroblemRR(
                        dim=len(self.parent.bs),
                        networkInstance=self.parent,
                        lowerTxLimit=self.parent.minTxPower,
                        upperTxLimit=self.parent.maxTxPower))

        prob.siec = copy.deepcopy(self.parent)
        # algo = algorithm.sga(gen=sgaGenerations)
        algo = pg.algorithm(pg.sga(gen=sgaGenerations))
        # archi = archipelago(algo, prob, numberOfThreads, numOfIndividuals, topology = topology.barabasi_albert())
        # archi.evolve(evolveTimes)
        # archi.join()
        population = pg.population(prob, numOfIndividuals)
        population = algo.evolve(population)

        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        # for island in archi:
        #     if theBestCostF > island.population.champion.f[0]:
        #         theBestCostF = island.population.champion.f[0]
        #         islandNumber = islandCounter
        #     islandCounter = islandCounter + 1

        if method == "global":
            for i in range(len(self.parent.bs)):
                self.parent.bs[i].outsidePower = population.champion_x[i]
        if method == "local":
            for i in range(len(localListBS)):
                # self.parent.bs[int(prob.bsList[i])].outsidePower = archi[islandNumber].population.champion.x[i]
                self.parent.bs[int(
                    localListBS[i])].outsidePower = population.champion_x[i]
            return len(localBsVector)
Example #15
0
import pygmo as pg
"""
Okay, so the lesson learned here is: not sure :-D
"""
if __name__ == "__main__":
    prob = pg.problem(pg.rosenbrock(dim=5))
    pool_creator = pg.mp_island()
    # pool_creator.resize_pool(1)
    pool_creator.init_pool(1)
    island = pg.island(udi=pool_creator,
                       algo=pg.sga(gen=200),
                       pop=pg.population(prob, 200))
    island.evolve()
    island.wait()
    print("island: ***** \n", island)
Example #16
0
    def get_bounds(self):
        #  return ([stage[0].TWR*0.75,stage[1].TWR*0.75,stage[2].TWR*0.75,stage[3].TWR*0.75,stage[0].DV_Ratio*0.9,stage[1].DV_Ratio*0.9,stage[2].DV_Ratio*0.9,stage[3].DV_Ratio*0.9],
        #         [stage[0].TWR*1.25,stage[1].TWR*1.25,stage[2].TWR*1.25,stage[3].TWR*1.25,stage[0].DV_Ratio*1.05,stage[1].DV_Ratio*1.05,stage[2].DV_Ratio*1.05,stage[3].DV_Ratio*1.05])
        return ([
            stage[0].DV_Ratio * 0.9, stage[1].DV_Ratio * 0.9,
            stage[2].DV_Ratio * 0.9
        ], [
            stage[0].DV_Ratio * 1.05, stage[1].DV_Ratio * 1.05,
            stage[2].DV_Ratio * 1.05
        ])


if general.Design_optimization:
    prob = pg.problem(Design_Optimization())
    algo = pg.algorithm(pg.sga(
        gen=1))  #Choose of heuristic algorithm and number of generations
    pop = pg.population(prob, 125)  #Choose number of individuals
    pop = algo.evolve(pop)  #Evolve the population
    y = pop.champion_x  #Extract the best DV division solution to minimize mass

    #print(y)

    DV_sum = 0
    for i in range(0, len(stage)):
        #    stage[i].TWR=y[i]
        stage[i].DV_Ratio = y[i]
        DV_sum = DV_sum + stage[i].DV_Ratio

    rocket, stage = Rocket_build(mission, rocket, stage, general)
    Information(mission, rocket, stage)
    print(y)
def make_new_squad_pygmo(
    gw_range,
    tag,
    budget=1000,
    players_per_position=TOTAL_PER_POSITION,
    season=CURRENT_SEASON,
    verbose=1,
    bench_boost_gw=None,
    triple_captain_gw=None,
    remove_zero=True,  # don't consider players with predicted pts of zero
    sub_weights={"GK": 0.01, "Outfield": (0.4, 0.1, 0.02)},
    dummy_sub_cost=45,
    uda=pg.sga(gen=100),
    population_size=100,
    **kwargs,
):
    """Optimize a full initial squad using any PyGMO-compatible algorithm.

    Parameters
    ----------
    gw_range : list
        Gameweeks to optimize squad for
    tag : str
        Points prediction tag to use
    budget : int, optional
        Total budget for squad times 10,  by default 1000
    players_per_position : dict
        No. of players to optimize in each position, by default
        airsenal.framework.squad.TOTAL_PER_POSITION
    season : str
        Season to optimize for, by default airsenal.framework.utils.CURRENT_SEASON
    verbose : int
        Verbosity of optimization algorithm, by default 1
    bench_boost_gw : int
        Gameweek to play benfh boost, by default None
    triple_captain_gw : int
        Gameweek to play triple captaiin, by default None,
    remove_zero : bool
        If True don't consider players with predicted pts of zero, by default True
    sub_weights : dict
        Weighting to give to substitutes in optimization, by default
        {"GK": 0.01, "Outfield": (0.4, 0.1, 0.02)},
    dummy_sub_cost : int, optional
        If not optimizing a full squad the price of each player that is not being
        optimized. For example, if you are optimizing 12 out of 15 players, the
        effective budget for optimizinig the squad will be
        budget - (15 -12) * dummy_sub_cost, by default 45
    uda : class, optional
        PyGMO compatible algorithm class, by default pg.sga(gen=100)
    population_size : int, optional
        Number of candidate solutions in each generation of the optimization,
        by default 100

    Returns
    -------
    airsenal.framework.squad.Squad
        The optimized squad
    """
    # Build problem
    opt_squad = SquadOpt(
        gw_range,
        tag,
        budget=budget,
        players_per_position=players_per_position,
        dummy_sub_cost=dummy_sub_cost,
        season=season,
        bench_boost_gw=bench_boost_gw,
        triple_captain_gw=triple_captain_gw,
        remove_zero=remove_zero,  # don't consider players with predicted pts of zero
        sub_weights=sub_weights,
    )
    prob = pg.problem(opt_squad)

    # Create algorithm to solve problem with
    algo = pg.algorithm(uda=uda)
    algo.set_verbosity(verbose)

    # population of problems
    pop = pg.population(prob=prob, size=population_size)

    # solve problem
    pop = algo.evolve(pop)
    if verbose > 0:
        print("Best score:", -pop.champion_f[0], "pts")

    # construct optimal squad
    squad = Squad(budget=opt_squad.budget, season=season)
    for idx in pop.champion_x:
        if verbose > 0:
            print(
                opt_squad.players[int(idx)].position(season),
                opt_squad.players[int(idx)].name,
                opt_squad.players[int(idx)].team(season, 1),
                opt_squad.players[int(idx)].price(season, 1) / 10,
            )
        squad.add_player(
            opt_squad.players[int(idx)].player_id,
            gameweek=opt_squad.start_gw,
        )

    # fill empty slots with dummy players (if chosen not to optimise full squad)
    for pos in opt_squad.positions:
        if opt_squad.dummy_per_position[pos] > 0:
            for _ in range(opt_squad.dummy_per_position[pos]):
                dp = DummyPlayer(
                    opt_squad.gw_range,
                    opt_squad.tag,
                    pos,
                    price=opt_squad.dummy_sub_cost,
                )
                squad.add_player(dp)
                if verbose > 0:
                    print(dp.position, dp.name, dp.purchase_price / 10)
    if verbose > 0:
        print(f"£{squad.budget/10}m in the bank")

    return squad
Example #18
0
def main():
    seed = args.seed
    np.random.seed(seed)
    cudnn.benchmark = True
    torch.manual_seed(seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(seed)
    timestamp = str(utils.get_unix_timestamp())
    utils.makedirs(args.save)
    path = os.path.join(args.save, timestamp)
    utils.create_exp_dir(path, scripts_to_save=glob.glob('../*.py'))
    logger = utils.get_logger(args.save, timestamp, file_type='txt')
    utils.makedirs(os.path.join(path, 'logs'))
    logger.info("time = %s, args = %s", str(utils.get_unix_timestamp()), args)

    input_shape = [
        11, 9, 3
    ]  # MANUALLY SET NUMBER OF CHANNELS (11) ACCORDING TO PRETRAINING

    os.system('cp -f ../pretrain-weights.pt {}'.format(
        os.path.join(path, 'weights.pt')))
    utils.makedirs(os.path.join(path, 'scripts'))
    os.system('cp -f ./for-copy/parse-ga.py {}'.format(
        os.path.join(path, 'scripts', 'parse-ga.py')))
    os.system('cp -f ./for-copy/parse-ga.py {}'.format(
        os.path.join(path, 'scripts', 'parse-log.py')))
    os.system('cp -f ./for-copy/parse_data.py {}'.format(
        os.path.join(path, 'scripts', 'parse_data.py')))
    os.system('cp -f ./for-copy/optimization-plots.sh {}'.format(
        os.path.join(path, 'scripts', '1_optimization-plots.sh')))

    # PyTorch
    criterion = nn.CrossEntropyLoss()
    criterion = criterion.to(device)
    model = Network(input_shape, args.num_drones, criterion, path)
    model = model.to(device)
    utils.load(model, os.path.join(path, 'weights.pt'))
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # PyGMO
    prob = pg.problem(genetic_algo.Flocking(path, timestamp, model))
    pop = pg.population(prob, size=10, seed=24601)
    algo = pg.algorithm(
        pg.sga(gen=1,
               cr=.90,
               m=0.02,
               param_s=3,
               crossover="single",
               mutation="uniform",
               selection="truncated"))
    algo.set_verbosity(1)

    for i in range(29):
        logger.info(
            "time = %s gen = %d \n champ_f = %s \n champ_x = %s \n f_s = %s \n x_s = %s \n id_s = %s",
            str(utils.get_unix_timestamp()), i + 1,
            str(np.array(pop.champion_f).tolist()),
            str(np.array(pop.champion_x).tolist()),
            str(np.array(pop.get_f()).tolist()),
            str(np.array(pop.get_x()).tolist()),
            str(np.array(pop.get_ID()).tolist()))
        pop = algo.evolve(pop)
        model.online_update(path, genetic_algo.TS_LIST[-100:], input_shape,
                            criterion, optimizer, logger, i)
        utils.save(model, os.path.join(path, 'weights.pt'))
Example #19
0
    def __call__(self, function):

        scanner_options = {
            'sade':
            dict(gen=self.gen,
                 variant=self.variant,
                 variant_adptv=self.variant_adptv,
                 ftol=self.ftol,
                 xtol=self.xtol,
                 memory=self.memory,
                 seed=self.seed),
            'gaco':
            dict(gen=self.gen,
                 ker=self.ker,
                 q=self.q,
                 oracle=self.oracle,
                 acc=self.acc,
                 threshold=self.threshold,
                 n_gen_mark=self.n_gen_mark,
                 impstop=self.impstop,
                 evalstop=self.evalstop,
                 focus=self.focus,
                 memory=self.memory,
                 seed=self.seed),
            'maco':
            dict(gen=self.gen,
                 ker=self.ker,
                 q=self.q,
                 threshold=self.threshold,
                 n_gen_mark=self.n_gen_mark,
                 evalstop=self.evalstop,
                 focus=self.focus,
                 memory=self.memory,
                 seed=self.seed),
            'gwo':
            dict(gen=self.gen, seed=self.seed),
            'bee_colony':
            dict(gen=self.gen, limit=self.limit, seed=self.seed),
            'de':
            dict(gen=self.gen,
                 F=self.F,
                 CR=self.CR,
                 variant=self.variant,
                 ftol=self.ftol,
                 xtol=self.xtol,
                 seed=self.seed),
            'sea':
            dict(gen=self.gen, seed=self.seed),
            'sga':
            dict(gen=self.gen,
                 cr=self.cr,
                 eta_c=self.eta_c,
                 m=self.m,
                 param_m=self.param_m,
                 param_s=self.param_s,
                 crossover=self.crossover,
                 mutation=self.mutation,
                 selection=self.selection,
                 seed=self.seed),
            'de1220':
            dict(gen=self.gen,
                 allowed_variants=self.allowed_variants,
                 variant_adptv=self.variant_adptv,
                 ftol=self.ftol,
                 xtol=self.xtol,
                 memory=self.memory,
                 seed=self.seed),
            'cmaes':
            dict(gen=self.gen,
                 cc=self.cc,
                 cs=self.cs,
                 c1=self.c1,
                 cmu=self.cmu,
                 sigma0=self.sigma0,
                 ftol=self.ftol,
                 xtol=self.xtol,
                 memory=self.memory,
                 force_bounds=self.force_bounds,
                 seed=self.seed),
            'moead':
            dict(gen=self.gen,
                 weight_generation=self.weight_generation,
                 decomposition=self.decomposition,
                 neighbours=self.neighbours,
                 CR=self.CR,
                 F=self.F,
                 eta_m=self.eta_m,
                 realb=self.realb,
                 limit=self.limit,
                 preserve_diversity=self.preserve_diversity,
                 seed=self.seed),
            'compass_search':
            dict(max_fevals=self.max_fevals,
                 start_range=self.start_range,
                 stop_range=self.stop_range,
                 reduction_coeff=self.reduction_coeff),
            'simulated_annealing':
            dict(Ts=self.Ts,
                 Tf=self.Tf,
                 n_T_adj=self.n_T_adj,
                 n_range_adj=self.n_range_adj,
                 bin_size=self.bin_size,
                 start_range=self.start_range,
                 seed=self.seed),
            'pso':
            dict(gen=self.gen,
                 omega=self.omega,
                 eta1=self.eta1,
                 eta2=self.eta2,
                 max_vel=self.max_vel,
                 variant=self.variant,
                 neighb_type=self.neighb_type,
                 neighb_param=self.neighb_param,
                 memory=self.memory,
                 seed=self.seed),
            'pso_gen':
            dict(gen=self.gen,
                 omega=self.omega,
                 eta1=self.eta1,
                 eta2=self.eta2,
                 max_vel=self.max_vel,
                 variant=self.variant,
                 neighb_type=self.neighb_type,
                 neighb_param=self.neighb_param,
                 memory=self.memory,
                 seed=self.seed),
            'nsga2':
            dict(gen=self.gen,
                 cr=self.cr,
                 eta_c=self.eta_c,
                 m=self.m,
                 eta_m=self.eta_m,
                 seed=self.seed),
            'nspso':
            dict(gen=self.gen,
                 omega=self.omega,
                 c1=self.c1,
                 c2=self.c2,
                 chi=self.chi,
                 v_coeff=self.v_coeff,
                 leader_selection_range=self.leader_selection_range,
                 diversity_mechanism=self.diversity_mechanism,
                 memory=self.memory,
                 seed=self.seed),
            'mbh':
            dict(algo=self.algo,
                 stop=self.stop,
                 perturb=self.perturb,
                 seed=self.seed),
            'cstrs_self_adaptive':
            dict(iters=self.iters, algo=self.algo, seed=self.seed),
            'ihs':
            dict(gen=self.gen,
                 phmcr=self.phmcr,
                 ppar_min=self.ppar_min,
                 ppar_max=self.ppar_max,
                 bw_min=self.bw_min,
                 bw_max=self.bw_max,
                 seed=self.seed),
            'xnes':
            dict(gen=self.gen,
                 eta_mu=self.eta_mu,
                 eta_sigma=self.eta_sigma,
                 eta_b=self.eta_b,
                 sigma0=self.sigma0,
                 ftol=self.ftol,
                 xtol=self.xtol,
                 memory=self.memory,
                 force_bounds=self.force_bounds,
                 seed=self.seed)
        }

        if self.log_data:
            xl = []
            yl = []

        log_data = self.log_data

        #
        class interf_function:
            def __init__(self, dim):
                self.dim = dim

            def fitness(self, x):
                x = np.expand_dims(x, axis=0)
                y = function(x)
                # x = x[0]
                y = y.tolist()
                if log_data:
                    xl.append(x)
                    yl.append(y)
                # print (x, y[0])
                return y[0]

            if function.is_differentiable():

                def gradient(self, x):
                    x = np.expand_dims(x, axis=0)
                    g = function(x)
                    g = g.tolist()
                    return g[0]

            def get_bounds(self):
                lb = []
                ub = []
                bounds = function.get_ranges()
                # warning
                # check for infinities
                for i in range(len(bounds)):
                    lb.append(bounds[i, 0])
                    ub.append(bounds[i, 1])
                r = (np.array(lb), np.array(ub))
                return r

        # I need to call pygmo functions directly
        prob = pg.problem(interf_function(function))

        # print (prob.get_thread_safety())

        if self.scanner == "sade":
            # I need a dictionary with algorithms and options
            algo = pg.algorithm(pg.sade(**scanner_options[self.scanner]))
        elif self.scanner == "gaco":
            algo = pg.algorithm(pg.gaco(**scanner_options[self.scanner]))
        # elif self.scanner == "maco": # is not implemented though in webpage
        #                               looks it is
        # algo = pg.algorithm(pg.maco(**scanner_options[self.scanner]))
        elif self.scanner == "gwo":
            algo = pg.algorithm(pg.gwo(**scanner_options[self.scanner]))
        elif self.scanner == "bee_colony":
            algo = pg.algorithm(pg.bee_colony(**scanner_options[self.scanner]))
        elif self.scanner == "de":
            algo = pg.algorithm(pg.de(**scanner_options[self.scanner]))
        elif self.scanner == "sea":
            algo = pg.algorithm(pg.sea(**scanner_options[self.scanner]))
        elif self.scanner == "sga":
            algo = pg.algorithm(pg.sga(**scanner_options[self.scanner]))
        elif self.scanner == "de1220":
            algo = pg.algorithm(pg.de1220(**scanner_options[self.scanner]))
        elif self.scanner == "cmaes":
            algo = pg.algorithm(pg.cmaes(**scanner_options[self.scanner]))
        # elif self.scanner == "moead": #multiobjective algorithm
        #  algo = pg.algorithm(pg.moead(**scanner_options[self.scanner]))
        elif self.scanner == "compass_search":
            algo = pg.algorithm(
                pg.compass_search(**scanner_options[self.scanner]))
        elif self.scanner == 'simulated_annealing':
            algo = pg.algorithm(
                pg.simulated_annealing(**scanner_options[self.scanner]))
        elif self.scanner == 'pso':
            algo = pg.algorithm(pg.pso(**scanner_options[self.scanner]))
        elif self.scanner == 'pso_gen':
            algo = pg.algorithm(pg.pso_gen(**scanner_options[self.scanner]))
        # elif self.scanner == 'nsga2': #multiobjective algorithm
        #  algo = pg.algorithm(pg.nsga2(**scanner_options[self.scanner]))
        # elif self.scanner == 'nspso': is not implemented though in webpage
        #                               looks it is
        #  algo = pg.algorithm(pg.nspso(**scanner_options[self.scanner]))
        elif self.scanner == 'mbh':
            if scanner_options[self.scanner]['algo'] == 'de':
                algo = pg.algorithm(
                    pg.mbh(pg.algorithm(pg.de(**scanner_options['de']))))
        # elif self.scanner == 'ihs': #does not work
        #  algo = pg.algorithm(ihs(**scanner_options[self.scanner]))
        # elif self.scanner == 'xnes': #does not work
        #  algo = pg.algorithm(xnes(**scanner_options[self.scanner]))
        # uda = algo.extract(xnes)
        else:
            print(
                'The ' + self.scanner + ' algorithm is not implemented. The '
                'list of algorithms available is', algorithms)
            sys.exit()

        # add verbosing flag
        if self.verbose > 1:
            algo.set_verbosity(self.verbose)

        pop = pg.population(prob, self.size)

        if self.verbose > 9:
            print('prob', prob)

        opt = algo.evolve(pop)

        if self.verbose > 9:
            print('algo', algo)

        # best_x = np.expand_dims(opt.champion_x, axis=0)
        # best_fitness = np.expand_dims(opt.get_f()[opt.best_idx()], axis=0)
        best_x = np.expand_dims(opt.champion_x, axis=0)
        best_fitness = np.expand_dims(opt.champion_f, axis=0)

        if self.verbose > 0:
            print('best fit:', best_x, best_fitness)

        if self.log_data:
            x = np.squeeze(xl, axis=(1, ))
            y = np.squeeze(yl, axis=(2, ))

        if self.log_data:
            return (x, y)
        else:
            return (best_x, best_fitness)
Example #20
0
def main():
    parser = argparse.ArgumentParser(description="Make a squad from scratch")
    # General parameters
    parser.add_argument(
        "--budget", help="budget, in 0.1 millions", type=int, default=1000
    )
    parser.add_argument("--season", help="season, in format e.g. 1819")
    parser.add_argument("--gw_start", help="gameweek to start from", type=int)
    parser.add_argument(
        "--num_gw", help="how many gameweeks to consider", type=int, default=3
    )
    parser.add_argument(
        "--algorithm",
        help="Which optimization algorithm to use - 'normal' or 'genetic'",
        type=str,
        default="genetic",
    )
    # parameters for "normal" optimization
    parser.add_argument(
        "--num_iterations",
        help="number of iterations (normal algorithm only)",
        type=int,
        default=10,
    )
    # parameters for "pygmo" optimization
    parser.add_argument(
        "--num_generations",
        help="number of generations (genetic only)",
        type=int,
        default=100,
    )
    parser.add_argument(
        "--population_size",
        help="number of candidate solutions per generation (genetic only)",
        type=int,
        default=100,
    )
    parser.add_argument(
        "--no_subs",
        help="Don't include points contribution from substitutes (genetic only)",
        action="store_true",
    )
    parser.add_argument(
        "--include_zero",
        help="Include players with zero predicted points (genetic only)",
        action="store_true",
    )
    parser.add_argument(
        "--verbose",
        help="Print details on optimsation progress",
        action="store_true",
    )
    parser.add_argument(
        "--fpl_team_id",
        help="ID for your FPL team",
        type=int,
    )
    args = parser.parse_args()
    season = args.season or get_current_season()
    budget = args.budget
    gw_start = args.gw_start or NEXT_GAMEWEEK
    gw_range = list(range(gw_start, min(38, gw_start + args.num_gw)))
    tag = get_latest_prediction_tag(season)
    if not check_tag_valid(tag, gw_range, season=season):
        print(
            "ERROR: Database does not contain predictions",
            "for all the specified optimsation gameweeks.\n",
            "Please run 'airsenal_run_prediction' first with the",
            "same input gameweeks and season you specified here.",
        )
        sys.exit(1)
    algorithm = args.algorithm
    num_iterations = args.num_iterations
    num_generations = args.num_generations
    population_size = args.population_size
    remove_zero = not args.include_zero
    verbose = args.verbose
    if args.no_subs:
        sub_weights = {"GK": 0, "Outfield": (0, 0, 0)}
    else:
        sub_weights = {"GK": 0.01, "Outfield": (0.4, 0.1, 0.02)}
    if algorithm == "genetic":
        try:
            import pygmo as pg

            uda = pg.sga(gen=num_generations)
        except ModuleNotFoundError as e:
            print(e)
            print("Defaulting to algorithm=normal instead")
            algorithm = "normal"
            uda = None
    else:
        uda = None

    best_squad = make_new_squad(
        gw_range,
        tag,
        budget=budget,
        season=season,
        algorithm=algorithm,
        remove_zero=remove_zero,
        sub_weights=sub_weights,
        uda=uda,
        population_size=population_size,
        num_iterations=num_iterations,
        verbose=verbose,
    )
    if best_squad is None:
        raise RuntimeError(
            "best_squad is None: make_new_squad failed to generate a valid team or "
            "something went wrong with the squad expected points calculation."
        )

    points = best_squad.get_expected_points(gw_start, tag)
    print("---------------------")
    print("Best expected points for gameweek {}: {}".format(gw_start, points))
    print("---------------------")
    print(best_squad)

    fpl_team_id = args.fpl_team_id or fetcher.FPL_TEAM_ID
    fill_initial_suggestion_table(
        best_squad,
        fpl_team_id,
        tag,
        season=season,
        gameweek=gw_start,
    )
def sga(objective_function,
        gen=200,
        cr=0.9,
        eta_c=1.0,
        m=0.02,
        param_m=1.0,
        param_s=2,
        crossover='exponential',
        mutation='polynomial',
        selection='tournament',
        pop_size=15):
    """
    Parameters
    - gen (int) – number of generations.
    - cr (float) – crossover probability.
    - eta_c (float) – distribution index for sbx crossover. This parameter is inactive if other types of crossover 
    are selected.
    - m (float) – mutation probability.
    - param_m (float) – distribution index (polynomial mutation), gaussian width (gaussian mutation) or 
    inactive (uniform mutation)
    - param_s (float) – the number of best individuals to use in “truncated” selection or the size of the 
    tournament in tournament selection.
    - crossover (str) – the crossover strategy. One of exponential, binomial, single or sbx
    - mutation (str) – the mutation strategy. One of gaussian, polynomial or uniform.
    - selection (str) – the selection strategy. One of tournament, “truncated”.

    """
    logs = []
    problem = pg.problem(objective_function)
    algorithm = pg.algorithm(
        pg.sga(gen=gen,
               cr=cr,
               eta_c=eta_c,
               m=m,
               param_m=param_m,
               param_s=param_s,
               crossover=crossover,
               mutation=mutation,
               selection=selection))
    algorithm.set_verbosity(50)
    population = pg.population(prob=problem, size=pop_size)
    solution = algorithm.evolve(population)
    """
    get_logs output is a list of tuples with the following structure:
    - Gen (int), generation number
    - Fevals (int), number of functions evaluation made
    - Best (float), the best fitness function currently in the population
    - dx (float), the norm of the distance to the population mean of the mutant vectors
    - df (float), the population flatness evaluated as the distance between the fitness of the best and of the worst individual
    - sigma (float), the current step-size
    """

    logs = np.array(algorithm.extract(pg.sga).get_log())[:, (
        1, 2)]  # taking only function evaluations and best fitness

    algo_ = algorithm.get_name()
    function_ = objective_function.get_name()

    return {
        'champion solution': solution.champion_f,
        'champion coordinates': solution.champion_x,
        'log': logs,
        'algorithm': algo_,
        'problem': function_
    }
import pygmo as pg
from pygmo import sga

# Maksimum generations of a run
MAXGEN = 1000
# Mutation probability
pm = 0.5
# Crossover probability
cm = 0.3

# User defined algorithm (UDA)
algo = pg.algorithm(sga(gen=MAXGEN, cr=cm, m=pm, selection='truncated'))