TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Сlass Simplex

Abstract: Сlass Simplex. Pesquise 862.000+ trabalhos acadêmicos

Por:   •  11/12/2014  •  Abstract  •  2.724 Palavras (11 Páginas)  •  311 Visualizações

Página 1 de 11

import math

import copy

class Simplex:

def __init__(self, testfunc, guess, increments, kR = -1, kE = 2, kC = 0.5):

"""Initializes the simplex.

INPUTS

------

testfunc the function to minimize

guess[] an list containing initial guesses

increments[] an list containing increments, perturbation size

kR reflection constant

kE expansion constant

kC contraction constant

"""

self.testfunc = testfunc

self.guess = guess

self.increments = increments

self.kR = kR

self.kE = kE

self.kC = kC

self.numvars = len(self.guess)

def minimize(self, epsilon = 0.0001, maxiters = 250, monitor = 1, **kwargs):

"""Walks to the simplex down to a local minima.

INPUTS

------

epsilon convergence requirement

maxiters maximum number of iterations

monitor if non-zero, progress info is output to stdout

OUTPUTS

-------

an array containing the final values

lowest value of the error function

number of iterations taken to get here

"""

self.simplex = []

self.lowest = -1

self.highest = -1

self.secondhighest = -1

self.errors = []

self.currenterror = 0

# Initialize vertices

for vertex in range(0, self.numvars + 3): # Two extras to store centroid and reflected point

self.simplex.append(copy.copy(self.guess))

# Use initial increments

for vertex in range(0, self.numvars + 1):

for x in range(0, self.numvars):

if x == (vertex - 1):

self.simplex[vertex][x] = self.guess[x] + self.increments[x]

self.errors.append(0)

self.calculate_errors_at_vertices(**kwargs)

iter = 0

for iter in range(0, maxiters):

# Identify highest, second highest, and lowest vertices

self.highest = 0

self.lowest = 0

for vertex in range(0, self.numvars + 1):

if self.errors[vertex] > self.errors[self.highest]:

self.highest = vertex

if self.errors[vertex] < self.errors[self.lowest]:

self.lowest = vertex

self.secondhighest = 0

for vertex in range(0, self.numvars + 1):

if vertex == self.highest:

continue

if self.errors[vertex] > self.errors[self.secondhighest]:

self.secondhighest = vertex

# Test for convergence

S = 0.0

S1 = 0.0

for vertex in range(0, self.numvars + 1):

S = S + self.errors[vertex]

F2 = S / (self.numvars + 1)

for vertex in range(0, self.numvars + 1):

S1 = S1 + (self.errors[vertex] - F2)**2

T = math.sqrt(S1 / self.numvars)

# Optionally, print progress information

if monitor:

print '#%d: Best = %f Worst = %f' % (iter,self.errors[self.lowest],self.errors[self.highest]),

for vertex in range(0, self.numvars + 1):

print "[",

for x in range(0, self.numvars):

print "%.2f" % self.simplex[vertex][x],

print "]",

print

if T <= epsilon: # We converged! Break out of loop!

break;

else: # Didn't

...

Baixar como (para membros premium)  txt (8.5 Kb)  
Continuar por mais 10 páginas »
Disponível apenas no TrabalhosGratuitos.com