예제 #1
0
    def __init__(self,
                 lower_bound,
                 upper_bound,
                 swarm_size_main_population,
                 swarm_size_auxiliary_population,
                 fitness_function,
                 swarm_confidence=2.0):
        self._random = np.random.RandomState()
        self._number_of_dimensions = number_of_dimensions
        self._lower_bound = lower_bound
        self._upper_bound = upper_bound
        self._number_of_generation_swarm = number_of_generation_swarm
        self._swarm_size_main_population = swarm_size_main_population
        self._swarm_size_auxiliary_population = swarm_size_auxiliary_population

        #Swarm hyper-parameters
        self._swarm_confidence = swarm_confidence

        self._fitness_function = fitness_function

        # Create the main swarm responsible to explore the function
        self._swarm = Swarm(number_of_creatures_main_population=self.
                            _swarm_size_main_population,
                            number_of_creatures_auxiliary_population=self.
                            _swarm_size_auxiliary_population,
                            lower_bound=self._lower_bound,
                            upper_bound=self._upper_bound,
                            random=self._random)

        self._list_real_evaluation_position = []
        self._list_real_evaluation_fitness = []
예제 #2
0
    def __init__(self, lower_bound, upper_bound, number_of_dimensions, number_of_real_evaluation, swarm_size,
                 number_of_generation_swarm, fitness_function, inertia_factor=0.5, self_confidence=1.5,
                 swarm_confidence=1.5, sense_of_adventure=1.5):
        self._number_of_real_evaluation = number_of_real_evaluation
        self._random = np.random.RandomState()
        self._number_of_dimensions = number_of_dimensions
        self._lower_bound = lower_bound
        self._upper_bound = upper_bound
        self._number_of_generation_swarm = number_of_generation_swarm

        self._fitness_before_real_evalutation = []

        #Swarm hyper-parameters
        self._inertia_factor = inertia_factor
        self._self_confidence = self_confidence
        self._swarm_confidence = swarm_confidence
        self._sense_of_adventure = sense_of_adventure

        # We remove two for the swarm size.
        # Because the first step of the algorithm is to add two creatures which cover the space
        # in the most efficient manner.
        self._swarm_size = swarm_size-2

        self._fitness_function = fitness_function

        self._regressor = FunctionEstimator(get_EI=True)

        # Create the main swarm responsible to explore the function
        self._swarm = Swarm(swarm_size=self._swarm_size, number_of_dimensions=self._number_of_dimensions,
                            lower_bound=self._lower_bound, upper_bound=self._upper_bound, random=self._random)

        self._list_real_evaluation_position = []
        self._list_real_evaluation_fitness = []

        self._bound_distance = self.create_bound_distance()
예제 #3
0
 def get(self):
     if not self.current_user:
         self.redirect("/login/")
     else:
         swarm=Swarm()
         swarm_uid=swarm.get_user_id_by_email(self.get_email())
         arrays=swarm.get_reserved_array_by_user_id(swarm_uid)
         self.render("swarm_arrays.html",arrays=arrays)
예제 #4
0
 def __init__(self, problemFile, paramFile, toBe=False):
     self._problem = Problem(problemFile)
     self._dimParticle = len(self._problem.getVertices())
     self._noPopulation, self._sizeOfNeighborhood, self._w, self._c1, self._c2 = 0, 0, 0, 0, 0
     self.loadParameters(paramFile)
     self._population = Swarm(self._noPopulation, self._dimParticle,
                              self._problem)
     self._neighborhoods = self.selectNeighbors()
     self._toBeDrawn = toBe
예제 #5
0
    def __init__(self, problem):
        self.problem = problem
        self.swarm = Swarm(int(self.problem.params["swarmSize"]),
                           int(self.problem.params["nrDims"]),
                           int(self.problem.params["vmin"]),
                           int(self.problem.params["vmax"]))
        self.totalIterations = int(self.problem.params["totalIterations"])
        self.w = float(self.problem.params["w"])
        self.c1 = float(self.problem.params["c1"])
        self.c2 = float(self.problem.params["c2"])

        self.run()
        self.showStatistics()
        self.showEvalDiagram()
예제 #6
0
def logPSOOutput():
    if not inputDir == None:
        samples = readSamples(inp=inputDir)
    else:
        samples = [(inputSample, 1)]
    with open('Malware_Samples_PSO_Results.csv', 'w') as f:
        f.write(
            'Sample,Sample_Length,Number_of_Initial_Mappings,BaselineCofidence,BaselineFitness,Prediction_Before_PSO, Confidence_After_PSO,Fitness_After_PSO,Prediction_After_PSO,Iteration,Number_of_Required_Changes,Number_Of_Queries\n'
        )
    i = 0
    for samplePath, _ in samples:
        sample = readSample(samplePath)
        print(samplePath)
        length = (len(sample))
        try:
            swarm = Swarm(numOfParticles, randomMutations, maxQueries, sample,
                          C1, C2, earlyTermination)
        except:
            print("\n############################################\n")
            print("Radare2 failed to analyze sample %s. Skipping...\n" %
                  (samplePath))
            print("\n############################################\n")
            continue
        try:
            numberOfPotentialMappings = len(swarm.replacements)
            baselineConfidence = swarm.calculateBaselineConfidence()
            if baselineConfidence < 0.5:
                continue
            print("Searching Optimum Advresarial Example... %s\n" % (i))
            swarm.initializeSwarmAndParticles()
            print('Number of Potential Changes %s\n' %
                  (numberOfPotentialMappings))
            print('Model Prediction Before PSO= %s\n' %
                  (0 if baselineConfidence < 0.5 else 1))
            print('Baseline Confidence= %s\n' % (str(baselineConfidence)))
            _, _, iterations, numberOfQueries = swarm.searchOptimum()
            modelConfidence = baselineConfidence - swarm.bestFitness
            print('Model Confindence After PSO %s' % (modelConfidence))
            print('Best Fitness Score= %s' % (swarm.bestFitness))
            finalPosition = swarm.patchTempFile(swarm.bestPosition)
            predAfter = get_probs(swarm.targetModel, finalPosition)
            predAfter = 0 if predAfter < 0.5 else 1
            numberOfChanges = swarm.numberOfChanges()
            print('Model Prediction After PSO= %s' % (str(predAfter)))
            print('Required number of changes (L1)= %s' % (numberOfChanges))
            with open('Malware_Samples_PSO_Results.csv', 'a') as f:
                f.write(
                    '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' %
                    (samplePath, str(length), str(numberOfPotentialMappings),
                     str(baselineConfidence), str(1 - baselineConfidence),
                     str(0 if baselineConfidence < 0.5 else 1),
                     str(modelConfidence), str(
                         swarm.bestFitness), str(predAfter), str(iterations),
                     str(numberOfChanges), str(numberOfQueries)))
            i = i + 1
            os.system("mv " + str(samplePath) + " dataset/tested")
        except:
            os.system("mv " + str(samplePath) + " dataset/failed")
예제 #7
0
class Iteration:
    def __init__(self):
        self._swarm = Swarm(10)
        self._w = 0.15
        self._c1 = 0.15
        self._c2 = 0.15

    def run(self):
        bestParticle = self._swarm.getBestParticle()
        for p in self._swarm.getSwarm():
            p.setBestGlobal(bestParticle)

        for p in self._swarm.getSwarm():
            p.update(bestParticle, self._w, self._c1, self._c2)
            p.evaluate()

    def getSwarm(self):
        return self._swarm.getSwarm()
예제 #8
0
    def create_swarm(self, socket, args):
        """Initialize a new swarm in this node"""
        swarm_id = args.swarmid

        if swarm_id in self._swarms:
            logging.warn(
                "Trying to add same swarm twice! Swarm: {}".format(swarm_id))
            return None

        self._swarms[swarm_id] = Swarm(socket, args)

        return self._swarms[swarm_id]
예제 #9
0
def setup():
    fullScreen()
    size(200, 200)
    global pop_size, interfaceHeight, interfaceWidth, inertia, c1, c2, lowerB, xlim, ylim
    pop_size = 5

    inertia = 0.9
    c1 = 0.2
    c2 = 0.1
    interfaceHeight = height
    interfaceWidth = width * 0.22
    xlim, lowerB, ylim = width, interfaceWidth, height

    global img
    global img1

    img = loadImage("grass.jpg")
    img1 = loadImage("grass1.jpg")

    global swarm
    swarm = Swarm(pop_size, lowerB, xlim, ylim, inertia, c1, c2)
    swarm.init_population()
def run(target_error, n_particles, c1, c2, w, n_iterations):
    fit = []
    search_space = Swarm(1, target_error, n_particles, c1, c2, w)
    particles_vector = [Particle() for _ in range(search_space.n_particles)]
    search_space.particles = particles_vector
    #search_space.print_particles()

    iteration = 0
    while (iteration < n_iterations):
        search_space.set_pbest()
        search_space.set_gbest()

        if (abs(search_space.gbest_value - search_space.target) <=
                search_space.target_error):
            break
        search_space.move_particles()
        fit.append(search_space.gbest_value)
        iteration += 1
    return (search_space.gbest_position, iteration, fit)
예제 #11
0
class World(object):
    '''
        lower_bound and upper_bound refer to the min and max of the function to optimize respectively.
        number_of_genes correspond to the number of dimensions of the function to optimize.
        population_size Number of creature in a population
        number_of_generation correspond to the number of the main loop the algorithm will do
    '''
    def __init__(self,
                 lower_bound,
                 upper_bound,
                 swarm_size_main_population,
                 swarm_size_auxiliary_population,
                 fitness_function,
                 swarm_confidence=2.0):
        self._random = np.random.RandomState()
        self._number_of_dimensions = number_of_dimensions
        self._lower_bound = lower_bound
        self._upper_bound = upper_bound
        self._number_of_generation_swarm = number_of_generation_swarm
        self._swarm_size_main_population = swarm_size_main_population
        self._swarm_size_auxiliary_population = swarm_size_auxiliary_population

        #Swarm hyper-parameters
        self._swarm_confidence = swarm_confidence

        self._fitness_function = fitness_function

        # Create the main swarm responsible to explore the function
        self._swarm = Swarm(number_of_creatures_main_population=self.
                            _swarm_size_main_population,
                            number_of_creatures_auxiliary_population=self.
                            _swarm_size_auxiliary_population,
                            lower_bound=self._lower_bound,
                            upper_bound=self._upper_bound,
                            random=self._random)

        self._list_real_evaluation_position = []
        self._list_real_evaluation_fitness = []

    def run_world(self, max_evals):
        nmbr_evals, best_creature_fitness, best_creature_position = self._swarm.run_swarm(
            max_iter=self._number_of_generation_swarm,
            fitness_function=self._fitness_function,
            max_evals=max_evals)
        print "FINISHED"
        print "BEST POSITION FOUND"
        print best_creature_fitness
        print best_creature_position
        return nmbr_evals, best_creature_fitness, best_creature_position
예제 #12
0
def train2(table=train_18, max_x=15, pop=300, gen=500):
    swarm_app_strat_1 = Swarm(min_x=0,
                              max_x=max_x,
                              n=2,
                              population=pop,
                              generations=gen,
                              table=table,
                              flag_test=1,
                              plt_name='strat_3')
    swarm_app_strat_2 = Swarm(min_x=0,
                              max_x=max_x,
                              n=2,
                              population=pop,
                              generations=gen,
                              table=table,
                              flag_test=2,
                              plt_name='strat_4')
    swarm_app_strat_1.start()
    swarm_app_strat_2.start()
예제 #13
0
def Optimize(inFilePtr,
             outFilePtr,
             convFact,
             constraint,
             points,
             zeroInd,
             lossFunctionChoice=0):
    dist = 1.0 / (constraint[:, 2]**convFact)
    constraint = np.insert(constraint, 3, dist, axis=1)

    swarm = Swarm(constraint,
                  len(points),
                  randVal=randRange,
                  swarmSize=swarmSize,
                  zeroInd=zeroInd,
                  lossFunc=lossFunctionChoice)

    ittFin = One_Move(ittCount, swarm, constraint, len(points), threshold,
                      outFilePtr, convFact)
    return (stats.pearsonr(swarm.gBest[2], constraint[:, 3])[0],
            stats.spearmanr(swarm.gBest[2], constraint[:, 3])[0],
            lossFunction(constraint[:, 3],
                         swarm.gBest[2]), ittFin, swarm.id, swarm)
예제 #14
0
def draw():
    delay(100)
    global r, g, b, pop_size, swarm, interfaceHeight, interfaceWidth, c1, c2, inertia, lowerB, xlim, ylim

    backgroundObj = Background(r, b, g, height, width + 100, pop_size, inertia,
                               c1, c2, img, img1)
    backgroundObj.generateShade()
    backgroundObj.generateBubbles()
    interface = backgroundObj.generateInterface(interfaceWidth,
                                                interfaceHeight)
    fill(255, 127, 0)
    ellipse(swarm.foodSource.x, swarm.foodSource.y, 30, 30)
    #Code starts after u press 32. to hault the simulation press the mouse. to redraw press a key. to continue release the mouse
    for i in range(len(interface)):
        if interface[i] == True:
            if i == 0:
                pop_size += 1
                interface[i] = False
            if i == 1:
                if pop_size > 3:
                    pop_size -= 1
                interface[i] = False
            if i == 2:
                print('helloo')
                x = inertia + 0.1
                if x < 1:
                    inertia += 0.1
                interface[i] = False
            if i == 3:
                print('kekek')
                x = inertia - 0.1
                if x >= 0.1:
                    inertia -= 0.1

                interface[i] = False
            if i == 4:
                x = c1 + 0.1
                if x < 1:
                    c1 += 0.1
                interface[i] = False
            if i == 5:
                x = c1 - 0.1
                if x >= 0.1:
                    #swarm.inertia+=1
                    c1 -= 0.1
                interface[i] = False
            if i == 6:
                x = c2 + 0.1
                if x < 1:
                    #swarm.inertia+=1
                    c2 += 0.1
                interface[i] = False
            if i == 7:
                x = c2 - 0.1
                if x >= 0.1:
                    #swarm.inertia+=1
                    c2 -= 0.1
                interface[i] = False
            if i == 8:
                swarm = Swarm(pop_size, lowerB, xlim, ylim, inertia, c1, c2)
                swarm.init_population()
                interface[i] = False

            if i == 9:
                interface[i] = False
                print('l')
                noLoop()

    if keyCode == 32:
        swarm.PSO(100)
    if keyCode == ENTER:
        noLoop()
예제 #15
0
def main():
    swarm = Swarm(config.ALPHA, config.ABSORPTION)
    update_brightness(swarm.fireflies)
    swarm.update_attractiveness()
    utils.description(swarm.fireflies)

    swarm.__str__()

    t = 0

    while t < config.MAX_GENERATION:
        for i, firefly in enumerate(swarm.fireflies):

            other_firefly = swarm.most_attractive[i]

            if other_firefly is not firefly and other_firefly.attractiveness > firefly.attractiveness:
                swarm.move(firefly, other_firefly)
            elif other_firefly.attractiveness == firefly.attractiveness:
                swarm.move_randomly(other_firefly)

            update_brightness(swarm.fireflies)
            swarm.update_attractiveness()

        t += 1

    print()
    swarm.__str__()
예제 #16
0
        #for percent_mask in [0.2, 0.4, 0.6, 0.8]:
        #for percent_mask in [0.5]:
        for movecity in [0.2, 0.4, 0.6, 0.8]:

            #tu zmieniamy
            params = {
                'num_of_cities': 15,
                'size_cities': size_cities,
                'perc_lockdown': 0.4, #Kuba
                'perc_mask': 0.7,#percent_mask,#percent_mask, #Ola 0.7
                'perc_out': 0.5,#percent_out, ##prawdopodobienstwo wyjscia z zamknietego miasta #Ola 0.5
                'random_move': 0.7, #Kuba
                'move_city':movecity#movecity#0.3 #Ola
            }

            automat = Swarm(m, iteration, aa, bb, pop, show_visualisation=False, **params) #True
            numbers = automat.simulation()
            print(numbers)
            Ill_list.append(numbers.iloc[:, 0])
            Rest_list.append(numbers.iloc[:, 1])
            Dead_list.append(numbers.iloc[:, 2])
            Healthy_list.append(numbers.iloc[:, 3])


            #labele_list.append(str(params['perc_out']))
            #labele_list.append(str(params['perc_mask']))
            labele_list.append(str(params['move_city']))
            visualize_simulation(numbers, iteration, 'iter_' + str(iteration) + '_size_cities_' + str(size_cities))

    #Ill
    for i in range(len(Ill_list)):
예제 #17
0
import math
from Swarm import Swarm


def ackley(inpt):
    return -20 * math.exp(-0.2 * math.sqrt(0.5 * (
        (inpt[0]**2) + (inpt[1]**2)))) - (math.exp(
            0.5 * (math.cos(2 * math.pi * inpt[0]) +
                   math.cos(2 * math.pi * inpt[1])))) + math.exp(1) + 20


def booth(inpt):
    x = inpt[0]
    y = inpt[1]
    return ((x + 2 * y - 7)**2) + (2 * x + y - 5)**2


opt = Swarm(100, [1, 1], 3, booth, 2)

for i in range(100):
    opt.iterate([0.9, 0.9], [0.4, 0.4], [0.2, 0.2])
    opt.visualise()

print(opt.swarmsBestPos)
print(ackley(opt.swarmsBestPos))
예제 #18
0
from Swarm import Swarm
import Helper
import time

# objectives 2D - shape
# obj = Polygon([(0, 0), (0, 0.5), (0.5, 0.5), (0.5, 0)])
# obj = Polygon([(0.5, 0), (0.5, 0.5), (1, 0.5), (1, 0)])
obj = Polygon([(0.25, 0), (0.25, 0.5), (0.75, 0.5), (0.75, 0)])
# obj = Polygon([(0, 0.5), (0.5, 1), (1, 0.5), (0.5, 0)])
# obj = Point(0.5, 0.5).buffer(0.5)

if __name__ == '__main__':

    start_time = time.time()
    print 'Start PSO Algorithm...'
    swarm = Swarm(obj)
    result = swarm.solve()
    print '... Done PSO Algorithm'

    print 'Maximum similarity:', result.get_objective_function()
    best_process = list(set(result.get_position()))
    best_process  = filter(lambda a: a != 24, best_process)

    print 'Best process: Start', '->',
    for i in range(len(best_process)):
        if best_process[i] < Helper.MAX_ACTION:
            print 'Fold crease', best_process[i] + 1, '->',
    print 'End'

    Helper.draw_output(best_process,obj)
예제 #19
0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Worker')

    while True:
        resp = (s.recv(1024)).decode('utf-8')
        #print(resp)
        if resp == '1':
            print('Beginning Optimizaiton')
            start = (s.recv(1024)).decode('utf-8')
            start = start.split(' ')
            for i in range(len(start)):
                start[i] = int(start[i])
            print("beginning search at: ")
            print(start)
            opt = Swarm(1000, start, (max(start) * 0.2), function, len(start))
            for i in range(1000):
                opt.iterate([0.9, 0.9], [0.4, 0.4], [0.2, 0.2])
            #time.sleep(5) # make sure that the server is ready to recieve info again
            toSend = ''
            for num in opt.swarmsBestPos:
                toSend += str(num).strip() + ' '
            toSend += str(opt.swarmsBestVal)
            s.sendall(toSend.encode('utf-8'))
            print('Sent: ' + toSend)
            print("Found minimum at: ")
            print(opt.swarmsBestPos)
        if not resp:
            break
예제 #20
0
from Swarm import Swarm
import matplotlib.pyplot as plt

c1 = 2.05
c2 = 2.05
popSize = 64

swarm1 = Swarm(popSize, c1, c2)
swarm2 = Swarm(popSize, c1, c2)
swarm3 = Swarm(popSize, c1, c2)

swarm1.runInerWeight()
swarm2.runInerWeight()
swarm3.runInerWeight()

print(swarm1.bestSolValue, swarm1.bestSol)
print(swarm2.bestSolValue, swarm2.bestSol)
print(swarm3.bestSolValue, swarm3.bestSol)

gens = [i for i in range(1, swarm1.iter + 1)]

fig, (a1, a2) = plt.subplots(2)
a1.plot(gens, swarm1.bestPerGen, 'r-', gens, swarm2.bestPerGen, 'g-', gens,
        swarm3.bestPerGen, 'b-')
a2.plot(gens, swarm1.averagePerGen, 'r-', gens, swarm2.averagePerGen, 'g-',
        gens, swarm3.averagePerGen, 'b-')

#plt.plot(gens, swarm1.bestPerGen, 'r-', gens, swarm2.bestPerGen, 'g-', gens, swarm3.bestPerGen, 'b-')
#a1.ylabel("Fitness of best particle in generation")
#a2.ylabel("Average fitness of population")
plt.xlabel("Generation #")
예제 #21
0
 def __init__(self):
     self._swarm = Swarm(10)
     self._w = 0.15
     self._c1 = 0.15
     self._c2 = 0.15
예제 #22
0
class SwarmProcess(object):
    '''
    lower_bound and upper_bound refer to the min and max of the function to optimize respectively.
    number_of_genes correspond to the number of dimensions of the function to optimize.
    population_size Number of creature in a population
    number_of_generation correspond to the number of the main loop the algorithm will do
    '''
    def __init__(self, lower_bound, upper_bound, number_of_dimensions, number_of_real_evaluation, swarm_size,
                 number_of_generation_swarm, fitness_function, inertia_factor=0.5, self_confidence=1.5,
                 swarm_confidence=1.5, sense_of_adventure=1.5):
        self._number_of_real_evaluation = number_of_real_evaluation
        self._random = np.random.RandomState()
        self._number_of_dimensions = number_of_dimensions
        self._lower_bound = lower_bound
        self._upper_bound = upper_bound
        self._number_of_generation_swarm = number_of_generation_swarm

        self._fitness_before_real_evalutation = []

        #Swarm hyper-parameters
        self._inertia_factor = inertia_factor
        self._self_confidence = self_confidence
        self._swarm_confidence = swarm_confidence
        self._sense_of_adventure = sense_of_adventure

        # We remove two for the swarm size.
        # Because the first step of the algorithm is to add two creatures which cover the space
        # in the most efficient manner.
        self._swarm_size = swarm_size-2

        self._fitness_function = fitness_function

        self._regressor = FunctionEstimator(get_EI=True)

        # Create the main swarm responsible to explore the function
        self._swarm = Swarm(swarm_size=self._swarm_size, number_of_dimensions=self._number_of_dimensions,
                            lower_bound=self._lower_bound, upper_bound=self._upper_bound, random=self._random)

        self._list_real_evaluation_position = []
        self._list_real_evaluation_fitness = []

        self._bound_distance = self.create_bound_distance()

    def create_bound_distance(self):
        bound_distance = []
        for i in range(len(self._lower_bound)):
            bound_distance.append(self._upper_bound[i] - self._lower_bound[i])
            bound_distance = np.array(bound_distance)
        return bound_distance

    def run_swarm_process(self):
        # First we find the combination of creature that cover the space the more thoroughly.
        # To achieve that, we use KMEANS with k=2 on the list of creature position.
        kmeans = KMeans(n_clusters=2)

        swarm_positions = self._swarm.get_list_position()  # Get the list of point in the space for KMeans
        # Normalize the dimension of each point so the point chosen is irrelevant of the possible different unities
        # of each dimensions

        normalized_swarm_position = np.array(swarm_positions) / np.array(self._bound_distance)
        kmeans.fit(normalized_swarm_position)  # Train KMeans
        centers = kmeans.cluster_centers_  # Get the centers
        centers *= self._bound_distance  # Go back to the original dimension
        print "Centers: ", centers

        # Add two new creatures with their position corresponding to the centers of kmeans.
        creature0_position = centers[0]
        creature0_fitness = FitnessFunction.calculate_fitness(self._fitness_function, creature0_position,
                                                              self._number_of_dimensions)
        self._number_of_real_evaluation -= 1  # Did a real evaluation

        creature1_position = centers[1]
        creature1_fitness = FitnessFunction.calculate_fitness(self._fitness_function, creature1_position,
                                                              self._number_of_dimensions)
        self._number_of_real_evaluation -= 1  # Did a real evaluation

        self._swarm.add_creature_to_swarm(position=creature0_position)
        self._swarm.add_creature_to_swarm(position=creature1_position)

        # Add the creatures position and fitness to the list of position and fitness evaluated
        self._list_real_evaluation_position.append(creature0_position)
        self._list_real_evaluation_fitness.append(creature0_fitness)
        self._list_real_evaluation_position.append(creature1_position)
        self._list_real_evaluation_fitness.append(creature1_fitness)

        # Train the regressor
        self._regressor.train_regressor(self._list_real_evaluation_position, self._list_real_evaluation_fitness)

        # From here, we alternate between exploration and exploitation randomly based on an heuristic except for the
        # Very first pass where we for the algorithm to be in exploration mode for one more evaluation
        # (3 evaluations total)
        self.exploration()
        self._number_of_real_evaluation -= 1  # Did a real evaluation

        # Now that we have three points evaluated, we are ready to start the algorithm for the requested amount of real
        # evaluations. Or until the user stop the program
        for generation in range(self._number_of_real_evaluation):
            print self._list_real_evaluation_position
            print self._list_real_evaluation_fitness
            print self._fitness_before_real_evalutation
            # Decide if we explore or exploite.
            exploitation_threshold = max(0.2, 1/math.sqrt((generation+2)/2))
            exploitation_threshold = 0.0
            if self._random.rand() < exploitation_threshold:
                best_creature_ever = self.exploration()

                # TODO once the exploitation algorithm is finish. Remove the if/else and move this block after
                # We are almost done with this generation, get the real value of the point of interest found
                new_point_to_add_fitness = FitnessFunction.calculate_fitness(self._fitness_function,
                                                                             best_creature_ever.get_position(),
                                                                             self._number_of_dimensions)
                # Finish the generation by adding the new creature to the list and updating the regressor
                self._list_real_evaluation_position.append(best_creature_ever.get_position())
                self._list_real_evaluation_fitness.append(new_point_to_add_fitness)

                self._fitness_before_real_evalutation.append(self._regressor.get_estimation_fitness_value(best_creature_ever.get_position())[0])
                choose_kernel = False
                if len(self._list_real_evaluation_fitness) % 10 == 0:
                    choose_kernel = True
                self._regressor.train_regressor(self._list_real_evaluation_position,
                                                self._list_real_evaluation_fitness, choose_kernel=choose_kernel)
                print "Smallest point found: ", new_point_to_add_fitness, "Fitness found by the PSO:", \
                    best_creature_ever.get_fitness(), " At position: ", best_creature_ever.get_position()
                # Reset swarm fitness
                self._swarm.reset_swarm()
            else:
                best_creature_ever = self.exploitation()


        index = self._list_real_evaluation_fitness.index(min(self._list_real_evaluation_fitness))
        print "Smallest point found: ", self._list_real_evaluation_fitness[index], " At position: ", \
            self._list_real_evaluation_position[index]

    def exploration(self):
        print "EXPLORATION"
        # We want to get EI
        self._regressor.set_EI_bool(True)
        # We want to get the curiosity
        self._swarm.set_curiosity(True)
        # Make sure that every creature has been evaluated
        best_fitness = min(self._list_real_evaluation_fitness)
        print "BEST CURRENT FITNESS", best_fitness
        self._swarm.evaluate_fitness_swarm(fitness_function=self._regressor, best_real_function_value=best_fitness)

        # run swarm optimization with number of iterations.
        best_creature_ever = self._swarm.run_swarm_optimization(max_iterations=self._number_of_generation_swarm,
                                                                function_to_optimize=self._regressor,
                                                                inertia_factor=self._inertia_factor,
                                                                self_confidence=self._self_confidence,
                                                                swarm_confidence=self._swarm_confidence,
                                                                sense_of_adventure=self._sense_of_adventure,
                                                                best_real_function_value=best_fitness,
                                                                list_position_with_real_fitness=
                                                                self._list_real_evaluation_position)
        return best_creature_ever

    def exploitation(self):
        print "EXPLOITATION"
        # Finish exploration by updating the regressor
        # We don't want to get EI
        self._regressor.set_EI_bool(False)

        seed_position = []
        fitness_seed_position = []
        # Determine the number of parallel optimization to perform on already evaluated position
        if len(self._list_real_evaluation_position) <= 10:
            # Number of parallel optimization = number of real evaluation.
            seed_position.extend(self._list_real_evaluation_position)
            fitness_seed_position.extend(self._list_real_evaluation_fitness)
        else:
            # Create 10 parallel optimization with seed on the 10 best points found.
            # Get the 3 best positions and chose randomly 7 other points
            # Start by partially sorting the array to get the best 3 points
            index_partially_sorted = np.argpartition(a=self._list_real_evaluation_fitness, kth=3)[0:3]
            seed_position = self._list_real_evaluation_position[index_partially_sorted]
            fitness_seed_position = self._list_real_evaluation_fitness[index_partially_sorted]

            # Add randomly 7 positions different from the 3 positions already selected
            index_range = range(len(self._list_real_evaluation_fitness))
            # remove the already selected values
            for i in index_partially_sorted:
                index_range.remove(i)
            choices = np.random.choice(index_range, 7, replace=False)
            seed_position.extend(self._list_real_evaluation_position[choices])
            fitness_seed_position.extend(self._list_real_evaluation_position[choices])

        # Reduce the number of creatures per swarms so that the sum of all the creatures in all of the swarms
        # Are equivalent to when we are in exploration mode.
        size_of_swarm = int(self._swarm_size / len(seed_position))
        swarms = []
        limit = math.pi/10.0
        normalized_list_real_evaluation = np.array(self._list_real_evaluation_position)/np.array(self._bound_distance)
        fitness_list = np.array(self._list_real_evaluation_fitness)
        # Calculate the std of the fitness
        fitness_std = np.std(fitness_list)
        # If the std is very small (close to machine error), then the fitness information is basically uniform.
        # This mean it's useless to try to exploite this area. Launch and exploration phase instead.
        if fitness_std < np.finfo(float).eps*10:
            return self.exploration()


        # Get the sorted list from biggest value to smallest of the position fitness.
        sorted_fitness_list = np.flipud(np.sort(fitness_list))
        for position, fitness_position in zip(seed_position, fitness_seed_position):
            # We need to set the upper and lower bound in function of the seed point for this swarm and the variance
            # of the gaussian process.
            normalized_position = np.array(np.array(position)/self._bound_distance)
            '''
            # old way
            difference_between_position_and_points_to_avoid = normalized_list_real_evaluation - normalized_position
            distance_normalized_position_other_points = np.apply_along_axis(
                np.linalg.norm, axis=1, arr=difference_between_position_and_points_to_avoid)

            closest_point = np.argmin(distance_normalized_position_other_points)
            closesness_factor = 0.5

            difference_vector = closest_point-position

            if fitness_position == sorted_fitness_list[0]:
                mean = abs(sorted_fitness_list[0]-sorted_fitness_list[1])
            else:
                mean = 1.0
            '''

            # Bounding box is defined as when the std of the gaussian process is equal or smaller to pi/10.0*fitness_std
            # around the current position considered. However, since we cannot know analytically the std of the GP,
            # we have to use another way to actually define the measurement of the bounding box.
            # To do so, we check the closest known position with a real fitness to the creature we're working on right
            # now (i.e. identified by the name position). We then trace a segment between those two points and check
            # in the middle of this segment if the std of the GP is above or below our threshold.
            # If it's below: take half the original segment and call it L. We add L to the current segment and verify
            # the GP std. We keep increasing the segment by L until the GP std become bigger than our threshold. This is
            # our new boundary
            # If it's above: We divide by sqrt(2) the length of the segment and we check again. We keep repeating that
            # process until we get a value that is below the researched threshold. This is our new bounding box.

            # First, find the closest point by calculating the distance between the current position and all the others
            # with known fitness and get the min
            index = 0
            min_distance = float('Inf')
            min_distance_index = 0
            print normalized_list_real_evaluation
            print normalized_position
            for point in normalized_list_real_evaluation:
                if np.array_equal(normalized_position, point) == False:
                    distance = np.linalg.norm(normalized_position-point)
                    if distance < min_distance:
                        min_distance = distance
                        min_distance_index = index
                index += 1

            closest_point = normalized_list_real_evaluation[min_distance_index]
            print normalized_list_real_evaluation
            # Now that we have the closest point, calculate the middle point between the closest point and the
            # considered position
            middle_point = (normalized_position+closest_point)/2.
            # Get the variance in the GP of this point
            prediction, gp_std = self._regressor.get_estimation_fitness_value(middle_point)
            max_std = (2.0*math.pi)/10.
            if gp_std > max_std:
                # get a smaller and smaller segment until we find a point that is within our boundary
                new_segment = np.array(middle_point-normalized_position)
                shrinking_factor = 1.0
                while gp_std > max_std:
                    point_to_evaluate = np.array(normalized_position) + (shrinking_factor*new_segment)
                    prediction, gp_std = self._regressor.get_estimation_fitness_value(point_to_evaluate *
                                                                                      np.array(self._bound_distance))
                    shrinking_factor /= math.sqrt(2)
            else:
                # get a bigger and bigger segment that grow with the length of the middle point each time
                # calculate the segment to add each time
                segment_to_add = (np.array(middle_point)-np.array(normalized_position))/2.0
                new_segment = np.array(middle_point)
                while gp_std < max_std:
                    new_segment += np.array(segment_to_add)
                    prediction, gp_std = self._regressor.get_estimation_fitness_value(new_segment *
                                                                                      np.array(self._bound_distance))

            # Now that the new segment is calculated. We simply get the distance between the position we're interested
            # in and this point we just found (new_segment)
            distance_bound = np.linalg.norm(new_segment-normalized_position)

            # Take care of the curse of dimensionality
            distance_bound /= math.pow(math.sqrt(2), self._number_of_dimensions-1)

            # Unormalize
            distance_bound *= self._bound_distance

            # We create an array with the same dimension as the function dimension. We have to provide an upper bound
            # and a lower bound which will both have a distance of the norm we just calculated (distance_bound)
            upper_bound_exploitation = position + (distance_bound * np.ones(self._number_of_dimensions))
            lower_bound_exploitation = position - (distance_bound * np.ones(self._number_of_dimensions))

            swarm_to_add = Swarm(swarm_size=size_of_swarm, number_of_dimensions=self._number_of_dimensions,
                                 lower_bound=lower_bound_exploitation, upper_bound=upper_bound_exploitation,
                                 random=self._random)
            swarms.append(swarm_to_add)


        self._regressor.train_regressor(self._list_real_evaluation_position, self._list_real_evaluation_fitness)
        return 0.0
예제 #23
0
class Controller:
    def __init__(self, problem):
        self.problem = problem
        self.params = {}
        self.load_parameters()
        self.swarm = Swarm(self.params["swarmSize"],
                           len(self.problem.data.set))

    def load_parameters(self):
        try:
            f = open("params.in", "r")
            for x in filter(None, f.read().split('\n')):
                (param, value) = x.split(' =')
                value = int(value)
                self.params[param] = value
        except Exception as e:
            print("Exception Controller::loadParameters(fileName): " + str(e))

    def iteration(self):
        for i in range(self.params["swarmSize"]):
            current = self.swarm.v[i]
            p = self.swarm.get_best_neighbour()
            current.update(p, i)
            current.evaluate(self.problem)

    def run(self):
        for i in range(self.params["iterations"]):
            print(i)
            self.iteration()
        return self.swarm.get_best_particles(1)[0]

    def print_run(self):
        best = self.run()
        first, second = [], []
        for i in range(len(best.position)):
            if best.position[i] == 0:
                first.append(i + 1)
            else:
                second.append(i + 1)
        print("first: {}\nsecond: {}".format(first, second))

    def run_with_plot(self):
        best_particles = []
        num_iterations = 0
        while num_iterations < self.params["iterations"]:
            best_particles.append(
                self.swarm.get_best_particles(1)[0].best_fitness)
            self.iteration()
            num_iterations += 1
        return best_particles

    def print_plot(self):
        results = self.run_with_plot()
        matplotlib.pyplot.plot(range(1, len(results) + 1), results)
        matplotlib.pyplot.show()
        best = self.swarm.get_best_particles(1)[0]
        first, second = [], []
        for i in range(len(best.position)):
            if best.position[i] == 0:
                first.append(i + 1)
            else:
                second.append(i + 1)
        print("first: {}\nsecond: {}".format(first, second))

    def run_statistics(self):
        best_runs = []
        for run in range(self.params["runs"]):
            print("Running %d" % (run + 1))
            best_runs.append(self.run().best_fitness)
        with open("statistics.data", "a") as g:
            g.write("Average fitness: {}\n".format(statistics.mean(best_runs)))
            g.write("Standard deviation of fitness: {}\n\n".format(
                statistics.stdev(best_runs)))
예제 #24
0
 def init_swarms(number_swarms: int, function: Callable[[np.ndarray], float]):
     return [Swarm(Const.N_PARTICLES, function) for _ in range(number_swarms)]
예제 #25
0
 def __init__(self, problem):
     self.problem = problem
     self.params = {}
     self.load_parameters()
     self.swarm = Swarm(self.params["swarmSize"],
                        len(self.problem.data.set))
예제 #26
0
class Controller:
    def __init__(self, problemFile, paramFile, toBe=False):
        self._problem = Problem(problemFile)
        self._dimParticle = len(self._problem.getVertices())
        self._noPopulation, self._sizeOfNeighborhood, self._w, self._c1, self._c2 = 0, 0, 0, 0, 0
        self.loadParameters(paramFile)
        self._population = Swarm(self._noPopulation, self._dimParticle,
                                 self._problem)
        self._neighborhoods = self.selectNeighbors()
        self._toBeDrawn = toBe

    def getPopulation(self):
        return self._population

    def setPopulation(self):
        return self._population

    def selectNeighbors(self):
        pop = self._population
        nSize = self._sizeOfNeighborhood

        if (nSize > len(pop)):
            nSize = len(pop)

        # Attention if nSize==len(pop) this selection is not a propper one
        # use a different approach (like surfle to form a permutation)
        neighbors = []
        for i in range(len(pop)):
            localNeighbor = []
            for j in range(nSize):
                x = randint(0, len(pop) - 1)
                while (x in localNeighbor):
                    x = randint(0, len(pop) - 1)
                localNeighbor.append(x)
            neighbors.append(localNeighbor.copy())
        return neighbors

    def sigmoidFunction(self, x):
        return 1 / (1 + exp(-x))

    def iteration(self):
        pop, neighbors, w, c1, c2 = self._population.getListOfParticles(
        ), self._neighborhoods, self._w, self._c1, self._c2

        bestNeighbors = []
        # determine the best neighbor for each particle
        for i in range(len(pop)):
            bestNeighbors.append(neighbors[i][0])
            for j in range(1, len(neighbors[i])):
                if (pop[bestNeighbors[i]].fitness >
                        pop[neighbors[i][j]].fitness):
                    bestNeighbors[i] = neighbors[i][j]

        # update the velocity for each particle
        for i in range(len(pop)):
            for j in range(len(pop[0].velocity)):
                newVelocity = w * pop[i].velocity[j]
                newVelocity = newVelocity + c1 * random() * (
                    pop[bestNeighbors[i]].position[j] - pop[i].position[j])
                newVelocity = newVelocity + c2 * random() * (
                    pop[i].bestPosition[j] - pop[i].position[j])
                pop[i].velocity[j] = newVelocity

        # update the position for each particle
        for i in range(len(pop)):
            newposition = []
            for j in range(len(pop[i].velocity)):
                if (random() >= self.sigmoidFunction(pop[i].velocity[j])):
                    newposition.append(1)
                else:
                    newposition.append(2)
            pop[i].position = newposition
        self._population.setListOfParticles(pop)
        return self._population

    def runAlgorithm(self):

        for i in range(self._noPopulation):
            self._population = self.iteration()

        best = 0
        fitnesses = []
        P = self._population.getListOfParticles()
        for i in range(0, len(self._population)):
            fitnesses.append(P[i].fitness)
            if (P[i].fitness < P[best].fitness):
                best = i

        if self._toBeDrawn:
            pyp.plot(fitnesses)
            pyp.show()

        print('The best particle has: \n' + str(P[best]) + 'Fitness = ' +
              str(P[best].fitness) + '\n')

    def loadParameters(self, paramFile):
        f = open(paramFile)
        line = f.readline().split()
        self._noPopulation, self._sizeOfNeighborhood, self._w, self._c1, self._c2 = \
            int(line[0]), int(line[1]), float(line[2]), float(line[3]), float(line[4])
        f.close()
예제 #27
0
    def exploitation(self):
        print "EXPLOITATION"
        # Finish exploration by updating the regressor
        # We don't want to get EI
        self._regressor.set_EI_bool(False)

        seed_position = []
        fitness_seed_position = []
        # Determine the number of parallel optimization to perform on already evaluated position
        if len(self._list_real_evaluation_position) <= 10:
            # Number of parallel optimization = number of real evaluation.
            seed_position.extend(self._list_real_evaluation_position)
            fitness_seed_position.extend(self._list_real_evaluation_fitness)
        else:
            # Create 10 parallel optimization with seed on the 10 best points found.
            # Get the 3 best positions and chose randomly 7 other points
            # Start by partially sorting the array to get the best 3 points
            index_partially_sorted = np.argpartition(a=self._list_real_evaluation_fitness, kth=3)[0:3]
            seed_position = self._list_real_evaluation_position[index_partially_sorted]
            fitness_seed_position = self._list_real_evaluation_fitness[index_partially_sorted]

            # Add randomly 7 positions different from the 3 positions already selected
            index_range = range(len(self._list_real_evaluation_fitness))
            # remove the already selected values
            for i in index_partially_sorted:
                index_range.remove(i)
            choices = np.random.choice(index_range, 7, replace=False)
            seed_position.extend(self._list_real_evaluation_position[choices])
            fitness_seed_position.extend(self._list_real_evaluation_position[choices])

        # Reduce the number of creatures per swarms so that the sum of all the creatures in all of the swarms
        # Are equivalent to when we are in exploration mode.
        size_of_swarm = int(self._swarm_size / len(seed_position))
        swarms = []
        limit = math.pi/10.0
        normalized_list_real_evaluation = np.array(self._list_real_evaluation_position)/np.array(self._bound_distance)
        fitness_list = np.array(self._list_real_evaluation_fitness)
        # Calculate the std of the fitness
        fitness_std = np.std(fitness_list)
        # If the std is very small (close to machine error), then the fitness information is basically uniform.
        # This mean it's useless to try to exploite this area. Launch and exploration phase instead.
        if fitness_std < np.finfo(float).eps*10:
            return self.exploration()


        # Get the sorted list from biggest value to smallest of the position fitness.
        sorted_fitness_list = np.flipud(np.sort(fitness_list))
        for position, fitness_position in zip(seed_position, fitness_seed_position):
            # We need to set the upper and lower bound in function of the seed point for this swarm and the variance
            # of the gaussian process.
            normalized_position = np.array(np.array(position)/self._bound_distance)
            '''
            # old way
            difference_between_position_and_points_to_avoid = normalized_list_real_evaluation - normalized_position
            distance_normalized_position_other_points = np.apply_along_axis(
                np.linalg.norm, axis=1, arr=difference_between_position_and_points_to_avoid)

            closest_point = np.argmin(distance_normalized_position_other_points)
            closesness_factor = 0.5

            difference_vector = closest_point-position

            if fitness_position == sorted_fitness_list[0]:
                mean = abs(sorted_fitness_list[0]-sorted_fitness_list[1])
            else:
                mean = 1.0
            '''

            # Bounding box is defined as when the std of the gaussian process is equal or smaller to pi/10.0*fitness_std
            # around the current position considered. However, since we cannot know analytically the std of the GP,
            # we have to use another way to actually define the measurement of the bounding box.
            # To do so, we check the closest known position with a real fitness to the creature we're working on right
            # now (i.e. identified by the name position). We then trace a segment between those two points and check
            # in the middle of this segment if the std of the GP is above or below our threshold.
            # If it's below: take half the original segment and call it L. We add L to the current segment and verify
            # the GP std. We keep increasing the segment by L until the GP std become bigger than our threshold. This is
            # our new boundary
            # If it's above: We divide by sqrt(2) the length of the segment and we check again. We keep repeating that
            # process until we get a value that is below the researched threshold. This is our new bounding box.

            # First, find the closest point by calculating the distance between the current position and all the others
            # with known fitness and get the min
            index = 0
            min_distance = float('Inf')
            min_distance_index = 0
            print normalized_list_real_evaluation
            print normalized_position
            for point in normalized_list_real_evaluation:
                if np.array_equal(normalized_position, point) == False:
                    distance = np.linalg.norm(normalized_position-point)
                    if distance < min_distance:
                        min_distance = distance
                        min_distance_index = index
                index += 1

            closest_point = normalized_list_real_evaluation[min_distance_index]
            print normalized_list_real_evaluation
            # Now that we have the closest point, calculate the middle point between the closest point and the
            # considered position
            middle_point = (normalized_position+closest_point)/2.
            # Get the variance in the GP of this point
            prediction, gp_std = self._regressor.get_estimation_fitness_value(middle_point)
            max_std = (2.0*math.pi)/10.
            if gp_std > max_std:
                # get a smaller and smaller segment until we find a point that is within our boundary
                new_segment = np.array(middle_point-normalized_position)
                shrinking_factor = 1.0
                while gp_std > max_std:
                    point_to_evaluate = np.array(normalized_position) + (shrinking_factor*new_segment)
                    prediction, gp_std = self._regressor.get_estimation_fitness_value(point_to_evaluate *
                                                                                      np.array(self._bound_distance))
                    shrinking_factor /= math.sqrt(2)
            else:
                # get a bigger and bigger segment that grow with the length of the middle point each time
                # calculate the segment to add each time
                segment_to_add = (np.array(middle_point)-np.array(normalized_position))/2.0
                new_segment = np.array(middle_point)
                while gp_std < max_std:
                    new_segment += np.array(segment_to_add)
                    prediction, gp_std = self._regressor.get_estimation_fitness_value(new_segment *
                                                                                      np.array(self._bound_distance))

            # Now that the new segment is calculated. We simply get the distance between the position we're interested
            # in and this point we just found (new_segment)
            distance_bound = np.linalg.norm(new_segment-normalized_position)

            # Take care of the curse of dimensionality
            distance_bound /= math.pow(math.sqrt(2), self._number_of_dimensions-1)

            # Unormalize
            distance_bound *= self._bound_distance

            # We create an array with the same dimension as the function dimension. We have to provide an upper bound
            # and a lower bound which will both have a distance of the norm we just calculated (distance_bound)
            upper_bound_exploitation = position + (distance_bound * np.ones(self._number_of_dimensions))
            lower_bound_exploitation = position - (distance_bound * np.ones(self._number_of_dimensions))

            swarm_to_add = Swarm(swarm_size=size_of_swarm, number_of_dimensions=self._number_of_dimensions,
                                 lower_bound=lower_bound_exploitation, upper_bound=upper_bound_exploitation,
                                 random=self._random)
            swarms.append(swarm_to_add)


        self._regressor.train_regressor(self._list_real_evaluation_position, self._list_real_evaluation_fitness)
        return 0.0
예제 #28
0

if __name__ == "__main__":
    name = "Power2"
    numberOfSwarm = 50

    firstObjective = Objective("X2", [0], X2)
    secondObjective = Objective("X2_reverse", [1], X2_reverse)
    objectiveFunction = [firstObjective, secondObjective]

    numOfFeature = 2
    featureRange = (
        (-100, 100),
        (-100, 100),
    )
    numberOfIter = 150
    q = 2

    swarm = Swarm(name=name,
                  numberOfSwarm=numberOfSwarm,
                  objectiveFunction=objectiveFunction,
                  numOfFeature=numOfFeature,
                  featureRange=featureRange,
                  numberOfIter=numberOfIter,
                  q=q)

    swarm.initialize()
    swarm.optimize()
    swarm.chartHistories()
    print(swarm.archive[0])
예제 #29
0
#!/usr/bin/python

from Demo import Demo
from Swarm import Swarm
from Taxi import Taxi

swarm= Swarm(count=24)
demo= Demo(swarm)
while demo.is_open:
	demo.draw(Taxi(swarm))
	if not demo.paused:
		swarm.move(demo.step)

예제 #30
0
from Swarm import Swarm
import time
import logging

# Configure a logging level
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

swarm = Swarm()

init_time = time.time()

swarm.start_mission([0, 1])

# Test of sending commands
# You can comment this block
for i in range(500):
    swarm.simultaneous_control_egalitarian_actions([0, 1], "speed?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "battery?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "time?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "height?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "temp?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "attitude?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "baro?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "acceleration?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "tof?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "wifi?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "sdk?")
    swarm.simultaneous_control_egalitarian_actions([0, 1], "sn?")

# Example
swarm.simultaneous_control_egalitarian_actions([0, 1], "speed 50")
예제 #31
0
def train_60_func(table=train_60, max_x=15, pop=300, gen=500):
    swarm_app_start_md = Swarm(min_x=0,
                               max_x=max_x,
                               n=2,
                               population=pop,
                               generations=gen,
                               table=table,
                               flag_choice=1,
                               plt_name='strat_60_md')
    swarm_app_start_md.start()
    swarm_app_start_mmre = Swarm(min_x=0,
                                 max_x=max_x,
                                 n=2,
                                 population=pop,
                                 generations=gen,
                                 table=table,
                                 flag_choice=2,
                                 plt_name='strat_60_mmre')
    swarm_app_start_mmre.start()
    swarm_app_start_rms = Swarm(min_x=0,
                                max_x=max_x,
                                n=2,
                                population=pop,
                                generations=gen,
                                table=table,
                                flag_choice=3,
                                plt_name='strat_60_rms')
    swarm_app_start_rms.start()
    swarm_app_start_ed = Swarm(min_x=0,
                               max_x=max_x,
                               n=2,
                               population=pop,
                               generations=gen,
                               table=table,
                               flag_choice=4,
                               plt_name='strat_60_ed')
    swarm_app_start_ed.start()
예제 #32
0
from Swarm import Swarm
import matplotlib.pyplot as plt

c1 = 1.4944
c2 = 1.4944
popSize = 64

swarm1 = Swarm(popSize, c1, c2)
swarm2 = Swarm(popSize, c1, c2)
swarm3 = Swarm(popSize, c1, c2)

swarm1.runBasic()
swarm2.runBasic()
swarm3.runBasic()

print(swarm1.bestSolValue, swarm1.bestSol)
print(swarm2.bestSolValue, swarm2.bestSol)
print(swarm3.bestSolValue, swarm3.bestSol)

gens = [i for i in range(1, swarm1.iter+1)]

fig, (a1, a2) = plt.subplots(2)
a1.plot(gens, swarm1.bestPerGen, 'r-', gens, swarm2.bestPerGen, 'g-', gens, swarm3.bestPerGen, 'b-')
a2.plot(gens, swarm1.averagePerGen, 'r-', gens, swarm2.averagePerGen, 'g-', gens, swarm3.averagePerGen, 'b-')

#plt.plot(gens, swarm1.bestPerGen, 'r-', gens, swarm2.bestPerGen, 'g-', gens, swarm3.bestPerGen, 'b-')
#a1.ylabel("Fitness of best particle in generation")
#a2.ylabel("Average fitness of population")
plt.xlabel("Generation #")
#plt.title("Best solution per generation vs. Generation #")
plt.show()