Question

Write a python code to implement optimization using Particle Swarm Optimization.

Write a python code to implement optimization using Particle Swarm Optimization.

Homework Answers

Answer #1

#program

import random
import numpy as np

W = 0.5
c1 = 0.8
c2 = 0.9

n_iterations = int(input("Inform the number of iterations: "))
target_error = float(input("Inform the target error: "))
n_particles = int(input("Inform the number of particles: "))

class Particle():
def __init__(self):
self.position = np.array([(-1) ** (bool(random.getrandbits(1))) * random.random()*50, (-1)**(bool(random.getrandbits(1))) * random.random()*50])
self.pbest_position = self.position
self.pbest_value = float('inf')
self.velocity = np.array([0,0])

def __str__(self):
print("I am at ", self.position, " meu pbest is ", self.pbest_position)
  
def move(self):
self.position = self.position + self.velocity


class Space():

def __init__(self, target, target_error, n_particles):
self.target = target
self.target_error = target_error
self.n_particles = n_particles
self.particles = []
self.gbest_value = float('inf')
self.gbest_position = np.array([random.random()*50, random.random()*50])

def print_particles(self):
for particle in self.particles:
particle.__str__()

def fitness(self, particle):
return particle.position[0] ** 2 + particle.position[1] ** 2 + 1

def set_pbest(self):
for particle in self.particles:
fitness_cadidate = self.fitness(particle)
if(particle.pbest_value > fitness_cadidate):
particle.pbest_value = fitness_cadidate
particle.pbest_position = particle.position
  

def set_gbest(self):
for particle in self.particles:
best_fitness_cadidate = self.fitness(particle)
if(self.gbest_value > best_fitness_cadidate):
self.gbest_value = best_fitness_cadidate
self.gbest_position = particle.position

def move_particles(self):
for particle in self.particles:
global W
new_velocity = (W*particle.velocity) + (c1*random.random()) * (particle.pbest_position - particle.position) + \
(random.random()*c2) * (self.gbest_position - particle.position)
particle.velocity = new_velocity
particle.move()
  

search_space = Space(1, target_error, n_particles)
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()
iteration += 1
  
print("The best solution is: ", search_space.gbest_position, " in n_iterations: ", iteration)

#output

#code

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write JAVA code of Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman Problem (TSP): find...
write JAVA code of Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman Problem (TSP): find shortest path to visit 26 cities.
compare between Genetic Algorithms ( GA) and Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman...
compare between Genetic Algorithms ( GA) and Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman Problem (TSP): find shortest path to visit 26 cities. write java code for both , then run the to compare between them in terms of time complexcity and optimality (show the optimal path found by each algorithm).
please write a LONG basic piece of code in Python language that focuses on using Variables,...
please write a LONG basic piece of code in Python language that focuses on using Variables, please do not make the code short! thank you!
PYTHON Write some code to read in a sequence of 10 doubles from the keyboard using...
PYTHON Write some code to read in a sequence of 10 doubles from the keyboard using loops, determine the position of the smallest number in that sequence.
3. For this problem, you should describe the algorithm using a flowchart and then write Python...
3. For this problem, you should describe the algorithm using a flowchart and then write Python code to implement the algorithm. You will write the Python code in a Python program file and then run it to obtain the output. Include in your HW document a photo of your flowchart and a screenshot of your code and the output in IDLE you get when you run the program. Algorithm: Ask the user to enter the user’s first name, the number...
Write a python code to print (any given file) file as a csv format by using...
Write a python code to print (any given file) file as a csv format by using MRJob only. code must be able to run stand-alone MRJob application. For submission to your work: Your final hand should be a single file name BDM.py that takes exactly one arguments for the input path. output will be handled through redirection. Sample run: python BDM.py sample.csv > output.csv
Using your programming language python, write a program that will ask for: - Probability of A...
Using your programming language python, write a program that will ask for: - Probability of A - Probability of B - Probability of B given A and will then output the probability of A given B using Bayes Theorem. You can prompt for the input anyway you like (command line, GUI, website, etc), and return the result any way you like (command line, GUI, website, etc). You may not use any libraries that implement Bayes for you - you need...
By using Python code answer the following parts: A) Write a program to ask the user...
By using Python code answer the following parts: A) Write a program to ask the user to input their name and then output the type and length of the input. B) Write a program to output the last 11 letters of 'Introduction to Indiana'. C) Write a program to output the 3rd to the 11th letters of 'Introduction to Indiana'. D) Write a program to ask the user to input a float number and output the rounded value of it...
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
Write python code for the queue class buuilt with a linked list using: First- returns first...
Write python code for the queue class buuilt with a linked list using: First- returns first value in queue, PrintQ-returns whole queue as list, RemoveValue-removes specific value from queue, RemoveIndex-removes value from queue by index
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT