Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/aprimoramentos'
Browse files Browse the repository at this point in the history
  • Loading branch information
ddss committed Aug 22, 2015
2 parents 9aea0e0 + 38f55dd commit 2ad7ec9
Show file tree
Hide file tree
Showing 4 changed files with 1,114 additions and 816 deletions.
24 changes: 15 additions & 9 deletions Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
Example of main program to be used with PSO
@author: Daniel
"""
from matplotlib import use
use('Agg')

from PSO import PSO # Importing the PSO class
from Modelo_benchmark import Modelo # Importing the function to be minimized
from time import time

sup = [32,32] # maximum value for the parameters
inf = [-32,-32] # minimum value for the parameters
t1 = time()

sup = [100,100] # maximum value for the parameters
inf = [-100,-100] # minimum value for the parameters

args_model=['ackley'] # In this example, the Modelo class needs another argument: the name of the function to be minimized

# PSO Algorithm executed with decreasing inercia weight from 0.9 to 0.4; acceleration factors constant and equal to default values of 2; 30 particles and 200 iterations.
Otimizacao = PSO(sup,inf,{'busca':'Otimo','algoritmo':'PSO','inercia':'TVIW-linear','aceleracao':'Constante','restricao':True},\
Num_particulas=30,itmax=100,w=[0.9,0.4],args_model=args_model)
Otimizacao = PSO(sup,inf,{'busca':'Otimo','algoritmo':'PSO','inercia':'TVIW-Adaptive-vel','aceleracao':'TVAC','restricao':True,'parada':['itmax']},
Num_particulas=30,itmax=500,w=[1.0,0.1],args_model=args_model,NP=2)
#Otimizacao = PSO(sup,inf,{'busca':'Otimo','algoritmo':'HPSO','gbest':'Particula','parada':'itmax','inercia':'Constante'},\
# Num_particulas=30,itmax=100,w=[0.9,0.4],args_model=args_model)
Otimizacao.Busca(Modelo,printit=True) # Do the search
Otimizacao.Result_txt() # Printing results in the same folder as txt files
Otimizacao.Graficos(Nome_param=[r'$\theta_1$',r'$\theta_2$'],Unid_param=[None,None],FO2a2=True) # Creating performance graphs in the same folder, including the objective function in 3d (FO2a2=True)

Otimizacao.Relatorios(resumos_txt=True) # Printing results in the same folder as txt files
#Otimizacao.Graficos(Nome_param=[r'$\theta_{}$'.format(i) for i in xrange(len(sup))],FO2a2=True) # Creating performance graphs in the same folder, including the objective function in 3d (FO2a2=True)
# To create movie frames uncomment the line below
# Otimizacao.Movie(Nome_param=[r'$\theta_1$',r'$\theta_2$'],tipos=['projecao','funcao']) # Creating movie frames
#Otimizacao.Movie(Nome_param=[r'$\theta_1$',r'$\theta_2$'],Unid_param=['admin','admin']) # Creating movie frames

# Printing the results:
print 'Optimum point', Otimizacao.gbest # optimum point
print 'Best Fitness' , Otimizacao.best_fitness # objective function in the optimum point
print 'Time', time()-t1
77 changes: 40 additions & 37 deletions Modelo_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,49 @@
import sys
from numpy import exp, size, sin, cos, sqrt, pi, sum, matrix


class Modelo(Thread):
result = 0
def __init__(self,param,args_model):

def __init__(self, param, args_model):
Thread.__init__(self)
self.args = param
self.args_model = args_model

def run(self):

#Funções unimodais

if self.args_model[0] == 'exponential':
# Função Exponencial
aux = -exp(-0.5*sum([arg**2 for arg in self.args]))

if self.args_model[0] == 'sphere':
# Função sphere (f2)
aux = sum([arg**2 for arg in self.args])

if self.args_model[0] == 'rosenbrook':
# Função de Rosenbrock
aux = 0
for i in xrange(0,size(self.args)-1):
aux = (100*(self.args[i+1]-self.args[i]**2)**2 + (self.args[i]-1)**2) + aux

#Funções multimodais

if self.args_model[0] == 'shaffer_f6':
# Shaffer's f6
num = sin(sqrt(self.args[0]**2 + self.args[1]**2))**2 - 0.5
den = (1.0 + 0.001*(self.args[0]**2 + self.args[1]**2))**2
aux = 0.5 + num/den

if self.args_model[0] == 'rastrigin':
# Rastrigin
aux = sum([arg**2 - 10*cos(2*pi*arg) + 10 for arg in self.args])

if self.args_model[0] == 'ackley':
# Ackley
D = float(size(self.args))
aux = -20*exp(-0.2*sqrt((1/D)*sum([arg**2 for arg in self.args]))) -exp((1/D)*sum(cos([2*pi*arg for arg in self.args]))) + 20 + exp(1)

self.result = aux

# Funções unimodais

if self.args_model[0] == 'exponential':
# Função Exponencial
aux = -exp(-0.5 * sum([arg ** 2 for arg in self.args]))

if self.args_model[0] == 'sphere':
# Função sphere (f2)
aux = sum([arg ** 2 for arg in self.args])

if self.args_model[0] == 'rosenbrook':
# Função de Rosenbrock
aux = 0
for i in xrange(0, size(self.args) - 1):
aux = (100 * (self.args[i + 1] - self.args[i] ** 2) ** 2 + (self.args[i] - 1) ** 2) + aux

# Funções multimodais

if self.args_model[0] == 'shaffer_f6':
# Shaffer's f6
num = sin(sqrt(self.args[0] ** 2 + self.args[1] ** 2)) ** 2 - 0.5
den = (1.0 + 0.001 * (self.args[0] ** 2 + self.args[1] ** 2)) ** 2
aux = 0.5 + num / den

if self.args_model[0] == 'rastrigin':
# Rastrigin
aux = sum([arg ** 2 - 10 * cos(2 * pi * arg) + 10 for arg in self.args])

if self.args_model[0] == 'ackley':
# Ackley
D = float(size(self.args))
aux = -20 * exp(-0.2 * sqrt((1 / D) * sum([arg ** 2 for arg in self.args]))) - exp(
(1 / D) * sum(cos([2 * pi * arg for arg in self.args]))) + 20 + exp(1)

self.result = aux
Loading

0 comments on commit 2ad7ec9

Please sign in to comment.