-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
129 lines (102 loc) · 4.59 KB
/
Copy pathMain.py
File metadata and controls
129 lines (102 loc) · 4.59 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import sys
import Logger
import GenePool
import ProblemPool
limitGeneration = 500
def operateGenepool(geenpool, logger="", numSimulation=0, numGeneration=0, blockBank = -1, numBlock = 10):
geenpool.doGame(blockBank,numBlock)
#gp.statLayerCount()
geenpool.evaluationLR(True)
# geenpool.statLayerCount()
# numSimulation,numGeneration,nameCategory,strContent,numBlock=-1
logger.writeGenerationResult(numSimulation,numGeneration,"Percent",geenpool.getStrGenerationPercent(),blockBank)
logger.writeGenerationResult(numSimulation,numGeneration,"Diversity",geenpool.getStrDiversity(),blockBank)
learningState = geenpool.checkLearningState(True)
if limitGeneration == numGeneration:
return False
geenpool.crossover()
geenpool.mutation()
geenpool.evolution()
geenpool.resetCounter()
geenpool.newGene()
return learningState
def learningLeann(nameFile):
prbPool = ProblemPool.PROBLEM_POOL(nameFile)
gp = GenePool.GENE_POOL()
gp.initGenePool(prbPool, 100)
generation = 0
learningState = True
while learningState:
if len(gp.genePool) < 1:
print "All dead"
break
else:
print "\n\tGeneration " + str(generation) + " is started. Pool size: "+ str(len(gp.genePool)) + "\n"
learningState = operateGenepool(gp)
print "\n\tGeneration " + str(generation) + " is Ended."
generation += 1
if generation > 100:
print "\nGeneration is over a hundred. It is too long time... The simulation end."
learningState = False
def excuteSingleBlockCrossValidation(indexBlock, logger, nameFile, numSimulation=0, numBlock=10):
prbPool = ProblemPool.PROBLEM_POOL()
prbPool.initFromFile(nameFile,True,numBlock)
gp = GenePool.GENE_POOL()
gp.initGenePool(prbPool, 100)
generation = 0
learningState = True
while learningState:
if len(gp.genePool) < 1:
print "All dead"
break
else:
print "\n\tGeneration " + str(generation) + " is started. Pool size: "+ str(len(gp.genePool)) + "\n"
learningState = operateGenepool(gp,logger,numSimulation,generation,indexBlock,numBlock)
print "\n\tGeneration " + str(generation) + " is Ended."
generation += 1
if generation > limitGeneration:
print "\nGeneration is over a hundred. It is too long time... The simulation end."
learningState = False
print "\n--------------------------------TEST SET " + str(indexBlock) + " RESULT------------------------------\n"
gp.remainBestOne()
print "\tTrain set result"
gp.resetCounter()
gp.doGame(indexBlock,numBlock)
gp.calSolvingPercentage()
logger.writeSimulationResult(numSimulation, "Train_Percent", gp.getStrGenerationPercent(), indexBlock)
print ""
print "\tTest set result"
gp.resetCounter()
gp.excuteBlock(indexBlock, numBlock, True)
gp.calSolvingPercentage()
logger.writeSimulationResult(numSimulation, "Test_Percent", gp.getStrGenerationPercent(), indexBlock)
logger.writeBlockResult(numSimulation, "Train_Best", gp.getStrStructureBest(), indexBlock)
print "\n-------------------------------------------------------------------------------"
def learningLeannCrossValidation(logger, nameFile, numSimulation=0, numBlock=10):
for i in range(numBlock):
excuteSingleBlockCrossValidation(i, logger, nameFile, numSimulation, numBlock)
nameFile = "./iris.csv"
shellExecuteBlock = -1
print sys.argv, len(sys.argv)
if len(sys.argv) > 1:
nameFile = sys.argv[1]
if not os.path.isfile(nameFile):
print "There is no test set named as ", nameFile
nameFile = "./iris.csv"
if len(sys.argv) == 3:
shellExecuteBlock = int(sys.argv[2])
if shellExecuteBlock > 9:
print "Execute block number out of range", shellExecuteBlock
shellExecuteBlock = -1
print nameFile, shellExecuteBlock
antLogger = Logger.LOGGER()
antLogger.initLogger(nameFile,"../results/",activate=True)
if shellExecuteBlock == -1:
numTimes = 0
while numTimes < 10:
learningLeannCrossValidation(antLogger, nameFile, numSimulation=numTimes)
numTimes += 1
print "\n" + str(numTimes) + " Simulation is ended."
else:
excuteSingleBlockCrossValidation(shellExecuteBlock, antLogger, nameFile, numSimulation=0, numBlock=10)