GA4StratificationP1: The genetic algorithm function to determine the stratum...

Description Usage Arguments Note Author(s) References See Also Examples

View source: R/GA4StratificationP1.R

Description

This is the general function in Genetic Algorithm that initially generates a random generation and then applies the fitness function, selects, mutates and crossovers in order to obtain the best solution.

Usage

1
GA4StratificationP1(dataName, numberOfStrata, sampleSize, iteration, GAgenerationSize, mutationRate)

Arguments

dataName

A string: The name of any type of numeric data containing the values of a univariate stratification variable.

numberOfStrata

An integer: The number of strata.

sampleSize

An integer: The total sample size.

iteration

An integer: The number of iterations in the Genetic Algorithm process.

GAgenerationSize

An integer: The number of the generations in the Genetic Algorithm process.

mutationRate

A numeric: The mutation rate in the Genetic Algorithm process. Mutation rate must be in between 0 and 1, inclusive. Small levels of mutation rate is preferable in Genetic Algorithm approach.

Note

This study is part of a project supported by the Scientific and Technological Research Council of Turkey (TUBITAK).

Author(s)

Sebnem Er, Timur Keskinturk, Charlie Daly

Maintainer: Sebnem Er <[email protected]>

References

http://ideas.repec.org/a/eee/csdana/v52y2007i1p53-67.html

http://www.sciencedirect.com/science/article/B6V8V-4NHM520-1/2/a21e0295aa1616ff56da1ddf2c0ba1ac

See Also

GA4StratificationP1fit GA4StratificationP1fitt GA4StratificationP1m GA4StratificationP1x

GA4StratificationSelection randomnumGenerator

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## The function is currently defined as
function(dataName,numberOfStrata,sampleSize,iteration,GAgenerationSize,mutationRate)
{
   dataName=data.frame(dataName)
   dataName=data.frame(dataName[order(dataName[,1]),])
   tableData=as.data.frame(table(dataName))
   randomnumRange=cumsum(tableData[,2])
   lengthRandomnum=length(randomnumRange)

   lengthData=nrow(dataName)
   randomGeneration=array(0,dim=c(GAgenerationSize,lengthData))

   nocrom=nrow(randomGeneration)
   fitp1=array(0,dim=c(1,nocrom))
   fit=array(0,dim=c(nocrom,1))
   N=means=s=n=vars=mas=NN=k=p=t=array(0,dim=c(nocrom,numberOfStrata))
   dd=array(0,dim=c(nocrom,1))

   randomNumbers=array(0,dim=c(GAgenerationSize,numberOfStrata-1))

   randomNumbersX=array(0,dim=c(nocrom,2))

   aftermut=array(0,dim=c(GAgenerationSize,lengthData+numberOfStrata+1))
   cumTotal=cumsum(dataName)
   sumSquares=cumsum(dataName^2)


   for (i in 1:GAgenerationSize)
   {
	randomNumbers[i,]=randomnumGenerator(randomnumRange,lengthRandomnum,numberOfStrata-1)

   }
   

   son=array(lengthData,dim=c(GAgenerationSize,1))
   indis=array(c(1:GAgenerationSize,randomNumbers,son),dim=c(GAgenerationSize,(numberOfStrata+1)))

   for(i in 2:(numberOfStrata+1))
   {
   	randomGeneration[indis[,c(1,i)]]=1
   }

   bestValue=-99999999999999999999999999999999999999999999999999999999999999
   for ( i in 1:iteration )
   {

	fitnessValueGeneration=GA4StratificationP1fit(randomGeneration,dataName,numberOfStrata,sampleSize,cumTotal,sumSquares,lengthData,dd,nocrom,fitp1,fit,N,means,s,n,vars,mas,NN,k,p,t)
	if ( max(fitnessValueGeneration)>bestValue )
	{
	    bestValue=max(fitnessValueGeneration)
	    bestGeneration=randomGeneration[max(which(fitnessValueGeneration==bestValue)),]
	}

	randomGeneration=GA4StratificationSelection(randomGeneration,fitnessValueGeneration)

	randomGeneration=GA4StratificationP1x(randomGeneration,bestGeneration,dataName,numberOfStrata,sampleSize,fitnessValueGeneration,cumTotal,sumSquares,lengthData,dd,nocrom,fitp1,fit,N,means,s,n,vars,mas,NN,k,p,t,randomNumbersX,tableData,randomnumRange,lengthRandomnum)

	randomGeneration=GA4StratificationP1m(randomGeneration,mutationRate,i)

	fitnessValueGeneration=GA4StratificationP1fit(randomGeneration,dataName,numberOfStrata,sampleSize,cumTotal,sumSquares,lengthData,dd,nocrom,fitp1,fit,N,means,s,n,vars,mas,NN,k,p,t)

	if ( max(fitnessValueGeneration)>bestValue )
	{
	    bestValue=max(fitnessValueGeneration)
	    bestGeneration=randomGeneration[max(which(fitnessValueGeneration==bestValue)),]
	} else

	{
	randomGeneration[sample(GAgenerationSize,1),]=bestGeneration
	}

	cat(i, " ",-bestValue,'\n')
	flush.console()
	Sys.sleep(1)
   }
   GA4StratificationP1fitt(bestGeneration,dataName,numberOfStrata,sampleSize,-bestValue,cumTotal,sumSquares)
  }

GA4Stratification documentation built on May 30, 2017, 1:22 a.m.