In [1]:
# rm(list=ls())
options(OutDec = ",") 
#==============================================================================
# Exemplo para encontrar o maximo de uma funcao envolvendo senos e cosenos 
# pelo metodo de arrefecimento simulado (simulated anneling)
#==============================================================================
# Funcao do metodo de arrefecimento simulado
#==============================================================================
maxf <- function(fx,x0,temperatura,Delta,maxit,percent=0.95){
	xmax <- rep(NA,maxit)
	x    <- x0
	for(k in 1:maxit){
		xprop <- runif(1,x-Delta,x+Delta)
		prob  <- min(1,exp((fx(xprop)-fx(x))/temperatura))
		if(runif(1) < prob){
			xmax[k] <- xprop
			x       <- xprop
		}else{
			xmax[k] <- x 
		}
		temperatura <- percent*temperatura
	}
	return(xmax)
}
In [2]:
#==============================================================================
# Funcao de seno e coseno
#==============================================================================
fx  <- function(x){
	(cos(50*x)+sin(20*x))^2
}
In [3]:
#==============================================================================
# Encontrando o maximo em uma grade de valores
#==============================================================================
xseq   <- seq(0,0.5,length=100000)
yseq   <- fx(xseq)
xmax   <- xseq[yseq==max(yseq)]
print(xmax)
print(max(yseq))
[1] 0,3791388
[1] 3,832544
In [4]:
#==============================================================================
# Grafico
#==============================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(6,6,0),
    mar=c(4.5,5,2,1),cex.main=2.0,bty="n")
curve(fx(x),from=0,to=0.5,lwd=4,ylab=expression(f(x)),xlim=c(0,0.5),
      ylim=c(0,4))
lines(c(xmax,xmax),c(0,max(yseq)),lty=2,lwd=4,col="red")
No description has been provided for this image
In [5]:
#==============================================================================
# Encontrando o maximo pelo arrefecimento simulado
#==============================================================================
set.seed(12345)
xmax.1 <- maxf(fx,0.12,100,0.05,500)
print(xmax.1)
  [1] 0,14209039 0,16818862 0,16383672 0,14634626 0,16911678 0,12257033
  [7] 0,14613882 0,13525915 0,12407355 0,09196991 0,08734272 0,13388425
 [13] 0,14833851 0,16819288 0,14083960 0,17014031 0,13891156 0,12592197
 [19] 0,16280146 0,17454392 0,20276324 0,24549064 0,22145877 0,17747828
 [25] 0,13298366 0,17943069 0,16093352 0,18418313 0,20716033 0,20071337
 [31] 0,22987015 0,27846854 0,32644636 0,37131708 0,38135278 0,40018811
 [37] 0,38748174 0,34230687 0,38845160 0,38948080 0,42652559 0,37739038
 [43] 0,34184157 0,37440726 0,40476452 0,44756003 0,40544137 0,42691936
 [49] 0,44893101 0,40849506 0,38794160 0,43536901 0,43750593 0,45125137
 [55] 0,42636315 0,43731075 0,46283786 0,49233506 0,54073768 0,49168409
 [61] 0,49762976 0,45660095 0,47759815 0,52200599 0,49987022 0,45321498
 [67] 0,48449169 0,52893916 0,54867628 0,54867628 0,55085404 0,55085404
 [73] 0,55085404 0,55085404 0,55085404 0,58355979 0,61312691 0,58397968
 [79] 0,58397968 0,58397968 0,58397968 0,58397968 0,57202201 0,57202201
 [85] 0,57202201 0,57202201 0,57202201 0,55992790 0,55992790 0,55992790
 [91] 0,55992790 0,55992790 0,55992790 0,55992790 0,55992790 0,55992790
 [97] 0,55992790 0,55992790 0,55992790 0,55992790 0,55992790 0,55992790
[103] 0,55992790 0,55992790 0,56553774 0,56553774 0,55330473 0,55330473
[109] 0,55330473 0,56589889 0,56589889 0,56589889 0,56589889 0,56589889
[115] 0,56268078 0,56268078 0,56060110 0,56060110 0,56060110 0,55784989
[121] 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989
[127] 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989
[133] 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989 0,55784989
[139] 0,56298902 0,56298902 0,56298902 0,56298902 0,56298902 0,56298902
[145] 0,56362155 0,56362155 0,56362155 0,56362155 0,56362155 0,56913683
[151] 0,56913683 0,56913683 0,56913683 0,56913683 0,56481186 0,56481186
[157] 0,56481186 0,56481186 0,56481186 0,56481186 0,56481186 0,56481186
[163] 0,56481186 0,56481186 0,56481186 0,56481186 0,56481186 0,56481186
[169] 0,56481186 0,56386725 0,56386725 0,56386725 0,56386725 0,56386725
[175] 0,56386725 0,56386725 0,56386725 0,56386725 0,56386725 0,56386725
[181] 0,56386725 0,56191144 0,56191144 0,56191144 0,56191144 0,56191144
[187] 0,56191144 0,56191144 0,56191144 0,56191144 0,56191144 0,56191144
[193] 0,56191144 0,56191144 0,56191144 0,56191144 0,56191144 0,56191144
[199] 0,56191144 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222
[205] 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222
[211] 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222
[217] 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222
[223] 0,56244222 0,56244222 0,56244222 0,56244222 0,56244222 0,56292822
[229] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[235] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[241] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[247] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[253] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[259] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[265] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[271] 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822 0,56292822
[277] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[283] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[289] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[295] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[301] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[307] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[313] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[319] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[325] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[331] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[337] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[343] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[349] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[355] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[361] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[367] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[373] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[379] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[385] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[391] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[397] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[403] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[409] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[415] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[421] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[427] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[433] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[439] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[445] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[451] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[457] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[463] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[469] 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838 0,56351838
[475] 0,56351838 0,56351838 0,56351838 0,56351838 0,56332160 0,56332160
[481] 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160
[487] 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160
[493] 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160 0,56332160
[499] 0,56332160 0,56332160
In [6]:
#==============================================================================
# Encontrando o maximo pelo arrefecimento simulado
#==============================================================================
xmax.2 <- maxf(fx,0.20,100,0.05,500)
print(xmax.2)
  [1] 0,15777567 0,10845704 0,12706314 0,09447607 0,14443276 0,13796083
  [7] 0,14668502 0,17763925 0,12922112 0,11998002 0,07226318 0,03310879
 [13] 0,06323963 0,06138542 0,07706165 0,10492108 0,13562677 0,09610831
 [19] 0,08983911 0,07026057 0,08628101 0,10765202 0,13426210 0,11816546
 [25] 0,11982519 0,12869868 0,15711729 0,14071882 0,12637986 0,08024110
 [31] 0,07295990 0,10277041 0,08062386 0,06660615 0,11488093 0,13894789
 [37] 0,12668776 0,16489575 0,18590993 0,18376649 0,23209741 0,22465274
 [43] 0,22858622 0,27578199 0,32053599 0,36871884 0,34315830 0,29426751
 [49] 0,24915764 0,27070711 0,25258120 0,25125657 0,26596153 0,30361257
 [55] 0,34551667 0,39381005 0,35765173 0,39645312 0,42153997 0,39492036
 [61] 0,39941265 0,40201245 0,37842666 0,39823359 0,36933393 0,36933393
 [67] 0,36785169 0,35654681 0,32179074 0,36369048 0,39355215 0,38062606
 [73] 0,38062606 0,38062606 0,38062606 0,38062606 0,38062606 0,38062606
 [79] 0,38062606 0,38283782 0,38283782 0,34168538 0,35136653 0,33929543
 [85] 0,36606761 0,37404760 0,37404760 0,37498658 0,40384855 0,35437853
 [91] 0,35437853 0,35437853 0,38321794 0,38321794 0,38321794 0,38321794
 [97] 0,37426594 0,37426594 0,37426594 0,37426594 0,37426594 0,38264352
[103] 0,38264352 0,38264352 0,37735503 0,37735503 0,38041163 0,38041163
[109] 0,38041163 0,38127672 0,38127672 0,38127672 0,38127672 0,38127672
[115] 0,38391643 0,38391643 0,38150329 0,38150329 0,38150329 0,38150329
[121] 0,38150329 0,38150329 0,37926560 0,37926560 0,37926560 0,37926560
[127] 0,37926560 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[133] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[139] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[145] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[151] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[157] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[163] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[169] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[175] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[181] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[187] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[193] 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026 0,38065026
[199] 0,38065026 0,38065026 0,38065026 0,38065026 0,37834252 0,37834252
[205] 0,37834252 0,37834252 0,37834252 0,37834252 0,37834252 0,37834252
[211] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[217] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[223] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[229] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[235] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[241] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[247] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[253] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[259] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[265] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[271] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[277] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[283] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[289] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[295] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[301] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[307] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[313] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[319] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[325] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[331] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[337] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920
[343] 0,37812920 0,37812920 0,37812920 0,37812920 0,37812920 0,37917302
[349] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[355] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[361] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[367] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[373] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[379] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[385] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[391] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[397] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[403] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[409] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[415] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[421] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[427] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[433] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[439] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[445] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[451] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[457] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[463] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[469] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[475] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[481] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[487] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[493] 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302 0,37917302
[499] 0,37917302 0,37917302
In [7]:
#==============================================================================
# Grafico: iteracoes
#==============================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(6,5,5),
    mar=c(4.5,5,1,1),cex.main=2.0,bty="n")
ts.plot(xmax.1,lwd=4,col="black",xlab="Iteração",ylim=c(0,0.7),
	ylab=expression(g(x[i])))
lines(xmax.2,col="red",lwd=4)
lines(1:500,rep(0  ,500),col="blue",lwd=4,lty=2)
lines(1:500,rep(0.5,500),col="blue",lwd=4,lty=2)
No description has been provided for this image
In [8]:
# rm(list=ls()) 
#==============================================================================
# graphics.off()
#==============================================================================
# Fim
#==============================================================================