def _gen_swarm(self, lo, up, size=0):
     for _ in range(size):
         self.particles.append(
             Particle(
                 random_vector(lo, up), random_vector(-abs(up - lo), abs(up - lo))
             )
         )
Beispiel #2
0
 def _gen_swarm(self, lo, up, size=0):
     for _ in range(size):
         x = random_vector(lo, up)
         self.particles.append(
             Particle(x, random_vector(-abs(up - lo), abs(up - lo))))
         if calculate_yang(x) < calculate_yang(
                 self.best_swarm_x) or self.best_swarm_x == -1:
             self.best_swarm_x = calculate_yang(x)
 def _gen_swarm(self, lo, up, e, size=0):
     for _ in range(size):
         x = random_vector(lo, up)
         self.particles.append(
             Particle(x, random_vector(-abs(up - lo), abs(up - lo))))
         x_val = calculate_yang(x, e)
         if x_val < self.best_swarm_val or self.best_swarm_x == -1:
             self.best_swarm_x = x
             self.best_swarm_val = x_val
 def update_velocity(self, omega, fip, fig, g):
     r = random_vector(0, 1, 2)
     r2 = random_vector(0, 1, 2)
     self.velocity = [
         omega * self.velocity[i]
         + fip * r[0]*r2[1] * (self.best_x[i] - self.x[i])
         + fig * r[1]*r2[0] * (g[i] - self.x[i])
         for i in range(len(self.velocity)-2)
     ]+[omega * self.velocity[i]
         + fip * r[0]*r2[0] * (self.best_x[i] - self.x[i])
         + fig * r[1]*r2[1] * (g[i] - self.x[i])
         for i in range(len(self.velocity)-2,len(self.velocity))]
Beispiel #5
0
    def _gen_swarm(self, lo, up, e, size=0):
        for i in range(size):
            if i == 0:
                x = random_vector(lo, up)
            else:
                x = random_vector(
                    -abs(max(self.particles[0].x) - min(self.particles[0].x)),
                    abs(max(self.particles[0].x) - min(self.particles[0].x)))

            self.particles.append(
                Particle(x, random_vector(-abs(up - lo), abs(up - lo))))
            x_val = calculate_yang(x, e)
            if x_val < self.best_swarm_val or self.best_swarm_x == -1:
                self.best_swarm_x = x
                self.best_swarm_val = x_val
 def update_velocity(self, omega, fip, fig, g):
     new_velocity = []
     r = random_vector(0, 1, 2)
     for i in range(len(self.velocity)):
         new_velocity.append(omega * self.velocity[i] + fip * r[0] *
                             (self.best_x[i] - self.x[i]) + fig * r[1] *
                             (g[i] - self.x[i]))
Beispiel #7
0
def RFD(pruning_rate_per_layer, is_first, is_last):
    ran_feat_1, ran_feat_2 = utils.random_vector(L1, L2)
    mask_1 = utils.mask_vec(ran_feat_1, pruning_rate_per_layer)
    mask_2 = utils.mask_vec(ran_feat_2, pruning_rate_per_layer)
    L1_rd_prev = tf.cond(is_first, lambda: L1_prev * mask_1, lambda: L1_prev)
    L1_rd = tf.nn.relu(L1_rd_prev + B1)
    L2_rd = tf.cond(is_last,
                    lambda: tf.nn.relu(tf.matmul(L1_rd, W2) * mask_2 + B2),
                    lambda: tf.nn.relu(tf.matmul(L1_rd, W2) + B2))
    model_rd = tf.matmul(L2_rd, W3) + B3
    return model_rd
Beispiel #8
0
def create_balls():
    magic_ball = Ball(BALL_POS, 50)
    magic_ball.speed = BALL_SPEED * random_vector()

    result = [magic_ball]
    for x in (-1, 0, 1):
        for y in (-1, 0, 1):
            if x == 0 and y == 0:
                continue
            result.append(Ball(BALL_POS + Vec2d(150 * x, 150 * y), random.randint(10, 40)))

    return result
Beispiel #9
0
def main(argv):
    #get arguments
    args = parser.parse_args()
    if args.problem != None:
        problem = args.problem
    else:
        problem = "OneMax"
    if args.size != None:
        size = int(args.size)
    else:
        size = 100
    if args.algorithm != None:
        algorithm = eval(args.algorithm)
    else:
        algorithm = ["OPOEA"]
    if args.it != None:
        it = int(args.it)
    else:
        it = 1
    if args.steps != None:
        steps = int(args.steps)
    else:
        steps = int(size)
    if args.save != None:
        save = bool(args.save)
    else:
        save = False
    if args.test != None:
        test = bool(args.test)
    else:
        test = False
    if args.extra_name != None:
        extra_name = str(args.extra_name)
    else:
        extra_name = ""
    if args.random_seed != None:
        random_seed = int(args.random_seed)
        if it > 1:
            print("Error: it must be equal to 1 when random_seed is specified")
            return 1
    else:
        random_seed = None
    crossover_choice = args.crossover_choice
    max_evaluation = int(args.max_evaluation)

    #global ioh_output
    ioh_output = args.ioh_output

    #DEBUG
    #print("args.ioh_output: " + str(args.ioh_output))
    #print("ioh_output: " + str(ioh_output))

    if 'LL_static' in algorithm:
        LL_static_lambda1 = int(args.LL_static_lambda1)
        LL_static_lambda2 = int(args.LL_static_lambda2)
        LL_static_lda = int(args.LL_static_lda)
        LL_static_crossOverBias = float(args.LL_static_crossOverBias)
    if 'LL_static_02' in algorithm:
        LL_static_02_lda = int(args.LL_static_02_lda)
        LL_static_02_alpha = int(args.LL_static_02_alpha)
        LL_static_02_beta = int(args.LL_static_02_beta)
    if 'LL_dynamic_01' in algorithm:
        LL_dynamic_01_a = float(args.LL_dynamic_01_a)
        LL_dynamic_01_b = float(args.LL_dynamic_01_b)
    if 'LL_dynamic_02' in algorithm:
        LL_dynamic_02_alpha = float(args.LL_dynamic_02_alpha)
        LL_dynamic_02_beta = float(args.LL_dynamic_02_beta)
        LL_dynamic_02_gamma = float(args.LL_dynamic_02_gamma)
        LL_dynamic_02_a = float(args.LL_dynamic_02_a)
        LL_dynamic_02_b = float(args.LL_dynamic_02_b)

    results = {}
    for alg in algorithm:
        results[alg] = record.Record()

    #import algorithms

    #mytest() #DEBUG

    if ioh_output is not None:
        if os.path.isfile(ioh_output):
            os.remove(ioh_output)
            with open(ioh_output, 'wt') as f:
                f.write('')

    for i in range(it):
        #for i in tqdm(range(it)):
        print('Run number ' + str(i))

        #set the instance for given problem
        instance = eval('evaluate.setup_' + problem + '(size, problem)')

        if random_seed is not None:
            np.random.seed(random_seed)

        #initial solution
        initial_solution = Solution(utils.random_vector(size), None, None, 0)
        initial_solution.value = getattr(evaluate, problem)(initial_solution,
                                                            instance, True)

        #Solve the problem
        for alg in algorithm:
            algFunc = getattr(algorithms, alg)

            if alg == 'LL_static':
                experiment = algFunc(initial_solution, instance,
                                     LL_static_lambda1, LL_static_lambda2,
                                     LL_static_lda, LL_static_crossOverBias,
                                     crossover_choice, max_evaluation,
                                     ioh_output)
            elif alg == 'LL_static_02':
                experiment = algFunc(initial_solution, instance,
                                     LL_static_02_lda, LL_static_02_alpha,
                                     LL_static_02_beta, crossover_choice,
                                     max_evaluation, ioh_output)
            elif alg == 'LL_dynamic_01':
                experiment = algFunc(initial_solution, instance,
                                     LL_dynamic_01_a, LL_dynamic_01_b,
                                     crossover_choice, max_evaluation,
                                     ioh_output)
            elif alg == 'LL_dynamic_02':
                experiment = algFunc(initial_solution, instance,
                                     LL_dynamic_02_alpha, LL_dynamic_02_beta,
                                     LL_dynamic_02_gamma, LL_dynamic_02_a,
                                     LL_dynamic_02_b, crossover_choice,
                                     max_evaluation, ioh_output)
            else:
                experiment = getattr(algorithms, alg)(initial_solution,
                                                      instance)
            #print(alg)
            print(experiment[0])
            results[alg].insert_expriment(experiment)

    list_of_records = [results[alg] for alg in algorithm]
    if save:
        for (l, alg) in zip(list_of_records, algorithm):
            l.print_to_file(problem, size, alg, steps, extra_name)

    if test:
        for (l, alg) in zip(list_of_records, algorithm):
            l.plot_average(steps)
        utils.compare(list_of_records, steps, algorithm)

    if it > 1:
        print("\n------------------- Summarised results ------------------")
        for alg in algorithm:
            lsVals = [e[0] for e in results[alg].list_of_results]
            print(alg + ', mean: ' + str(results[alg].mean_opt_time))
            print(alg + ', min: ' + str(np.min(lsVals)))
            print(alg + ', max: ' + str(np.max(lsVals)))

    return 0
def main():
    t = 1
    x, e = random_vector(0, 5), random_vector(0, 1)
    swarm(t, x, e)
def main():
    t = 10
    x, e = random_vector(0, 5), random_vector(0, 1)
 def update_velocity(self, omega, fip, fig):
     new_velocity = []
     r = random_vector(0, 1, 2)
     for i in range(len(self.velocity)):
         new_velocity.append(omega * self.velocity[i] + fip * r[0] * ())
Beispiel #13
0
 def update_velocity(self, omega, fip, fig):
     new_velocity = []
     r = random_vector(0, 1, 2)
     for v in self.velocity:
         new_velocity.append(omega*v + )
Beispiel #14
0
def main():
    t = 6
    x, e = random_vector(-5, 5), random_vector(0, 1)
    print(x, e)
    swarm(t, x, e)
 def _gen_swarm(self, size=0, lo, up):
     for _ in range(size):
         self.particles.append(Particle(random_vector(lo, up)))