0% found this document useful (0 votes)
15 views

LS

The document describes code for implementing a particle swarm optimization (PSO) algorithm. It defines a PSO class that initializes a swarm of particles, evaluates each particle's fitness, updates each particle's velocity and position, and tracks the best global and local positions over iterations until convergence criteria are met.

Uploaded by

Daniel Pussil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

LS

The document describes code for implementing a particle swarm optimization (PSO) algorithm. It defines a PSO class that initializes a swarm of particles, evaluates each particle's fitness, updates each particle's velocity and position, and tracks the best global and local positions over iterations until convergence criteria are met.

Uploaded by

Daniel Pussil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace ConsoleApplication1//Cambiar espacio de nombres


{
class PSO : Algoritmo
{
int swarmsize;
public int velocidadSuperior;
public int velocidadInferior;
public int semilla = 0;
public double bs;
public PSO(int iteraciones, int poblacion, int velInferior, int velSuper
ior)
{
swarmsize = poblacion;
base.iteracciones = iteraciones;
this.velocidadInferior = velInferior;
this.velocidadSuperior = velSuperior;
Console.WriteLine("PSO");
}

public void printv(double[] vect)


{
for (int i = 0; i < vect.Count(); i++)
{
Console.WriteLine("" + vect[i]);
}
}

public override double procesar(Funcion funcion, int interaciones, int s


emilla)
{
// Console.WriteLine("getRandon() "+getRandon(0.0, 5, 5555555));
this.semilla = semilla;
double rst = procesarPSO();
return rst;
}
public double procesarPSO()
{
//Crear e inicializar poblacion
Particula[] particulas = new Particula[swarmsize];
for (int i = 0; i < swarmsize; i++)//
{

Particula part = new Particula(this);


part.inicializar(semilla + i);
particulas[i] = part;
}
Particula best = new Particula(this);
//
for (int it = 0; it < base.iteracciones; it++)
{
//

Console.WriteLine("*******Iteracion : " + it);


for (int i = 0; i < particulas.Length; i++)
{
//Evaluar particula i
double fitness_x = funcion.evaluar(particulas[i].Posicion);
particulas[i].fitness = fitness_x;
//Console.WriteLine(" f: " + fitness_x);
//Actualizar el Mejor Fitness historico;
if (fitness_x < particulas[i].mejorfitness)//Minimizar
{
//clonar
particulas[i].mejorfitness = fitness_x;//Preguntar
particulas[i].mejorposicion = (double[])particulas[i].Po

sicion.Clone();//
particulas[i].mejorposicion = particulas[i].ClonarPos(pa
rticulas[i].Posicion);
}
//Mejor PArticula
double fitness_best = funcion.evaluar(best.Posicion);
best.fitness = fitness_best;
if (fitness_best == 0 || particulas[i].fitness < fitness_bes
t)
{
// best = particulas[i].Clone(particulas[i]);
best = particulas[i].Clone();
}
//
//

Console.WriteLine("{" + i + "}");
particulas[i].printf();

}
//Console.WriteLine("Best: {" + it + "}");
// best.printf();
//Tweek
const double epsiolon = 1.0;
double alf = 0.7;
double beta = 2.5;
double gama = 2.5;
double delta = 2.5;

for (int i = 0; i < particulas.Length; i++)


{
for (int j = 0; j < particulas[i].velocidad.Length; j++)
{
double b = getRandon(0.0, beta, semilla + i);
// double c = getRandon(0.0, gama, 1);
double d = getRandon(0.0, delta, semilla + i - 5);//Mejo
ro los resultados (88)
//
Console.WriteLine("
j : " + j + "b: " + b + " d: " + d);
double velocida = (alf * particulas[i].velocidad[j]) + b
* (particulas[i].mejorposicion[j] - particulas[i].Posicion[j]) + d * (best.mejo
rposicion[j] - particulas[i].Posicion[j]);//social + cognitivo
//ajustar limites de Velocida
if (velocida < velocidadInferior)
{
velocida = velocidadInferior;
}
if (velocida > velocidadSuperior)
{
velocida = velocidadSuperior;
}
// Console.WriteLine("vel: " + velocida);
particulas[i].velocidad[j] = velocida; //nueva velocidad
}
}
//Mutar
for (int i = 0; i < particulas.Length; i++)
{
for (int j = 0; j < particulas[i].velocidad.Length; j++)
{
double pos = 0.0;
pos = particulas[i].Posicion[j] + (epsiolon * particulas
[i].velocidad[j]);//la nueva posicion
//Ajustar Limites de posicion
if (pos < base.funcion.linf)
{
pos = base.funcion.linf;
}
if (pos > base.funcion.lsup)
{
pos = base.funcion.lsup;

}
particulas[i].Posicion[j] = pos;
}
}
}
// Console.WriteLine("**** Mejor Minimo: " + best.fitness + " EL me
jor best historico " + best.mejorfitness);
return best.fitness;
}
//public void printPoblacion(Particula[] lst)
//{
//
for (int i = 0; i < lst.Length; i++)
//
{
//
Console.WriteLine(" " + lst[i].mejorfitness + " , " + lst[i].f
itness);
//
for (int j = 0; j < lst[i].Posicion.Length; j++)
//
{
//
Console.WriteLine("
" + lst[i].Posicion[j] + " , " + ls
t[i].velocidad[j]);
//
}
//
//}

public override double procesar(Funcion funcion, int interaciones, int s


emilla, int ntweak)
{
throw new NotImplementedException();
}
public double getRandon(double inferior, double superior, int semilla)
{
Random rnd = new Random(semilla);
double x = inferior + (superior - inferior) * rnd.NextDouble();
return x;
}
public double getRandon1(double inferior, double superior)
{
Random rnd = new Random();
double x = rnd.NextDouble() * (superior - inferior) + inferior; // c
reates a number between 1 and 12
return x;
}
}
}

You might also like