-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.py
More file actions
126 lines (109 loc) · 5.03 KB
/
Copy pathdump.py
File metadata and controls
126 lines (109 loc) · 5.03 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
# coding: utf8
#############################################################################
#
# This file is part of evolveRobot. It contains functions to save in
# a directory details about a genetic algorithm run. You can view
# the report using your web browser and loading the index.html files.
#
# Contributors:
# - created by damien.marchal@univ.lille1.fr
#############################################################################
import os
from imgTools import matriceToImage
from datetime import datetime
def activated():
return False#True
theBasePath = None
theIndexFileName = None
theIndexFile = None
generationCount = 0
maxgeneration = 0
def saveCandidateHistory(aCandidate):
global generationCount
historyPath = theBasePath+"/history/"
if not os.path.exists(historyPath):
os.mkdir(historyPath)
theCandidateFileName = historyPath+"/candidate{0}.html".format(aCandidate.myId)
def saveHistories(theCandidates):
for candidate in theCandidates:
saveCandidateHistory(candidate)
def addGeneration(theGeneration, theFitnessFunction):
global generationCount
theHistoryIndexFileName = "../../"+theBasePath+"/history/index.html"
theScoringFileName = "../../"+theBasePath+"/history/score.html"
generationPath = theBasePath+"/generation{0}".format(generationCount)
if not os.path.exists(generationPath):
os.mkdir(generationPath)
if generationCount > 0:
prevGenerationPath = "../generation{0}/index.html".format(generationCount-1)
else:
prevGenerationPath = "../../"+theIndexFileName
if generationCount+1 < maxgeneration:
nextGenerationPath = "../generation{0}/index.html".format(generationCount+1)
else:
nextGenerationPath = "../../"+theIndexFileName
theGenerationIndexFileName = generationPath+"/index.html"
theGenerationIndexFile = open(theGenerationIndexFileName, "w+t")
theGenerationIndexFile.write(
"""
<html>
<header>
</header>
<body>
<center><h1>Generation: {0}</h1> <a href='{2}'>[prev]</a>[<a href='{4}'>history</a>][<a href='{5}'>scoring</a>]<a href='{3}'>[next]</a></center>
<hr>
Number of candidates: {1} <br>
<hr>
<table width='100%'><tr= width='100%'>
""".format(generationCount, len(theGeneration), prevGenerationPath, nextGenerationPath, theHistoryIndexFileName, theScoringFileName))
num=0
for candidate in theGeneration:
if num % 20 == 0 and num != 0:
theGenerationIndexFile.write("</tr><tr>")
#fixme {toMatrix instead of toMatrice}
imageName = "candidate{0}.png".format(candidate.myId)
imagePath = generationPath+"/"+imageName
matriceToImage(theFitnessFunction.toMatrice(candidate), theFitnessFunction.canvas.res[0],
theFitnessFunction.canvas.res[1], imagePath)
theGenerationIndexFile.write("""
<td width='5%'><center>
<img src='{0}' width='100%' ></img><br>
{2}<br>
Score: {1:.0f}
</center>
</td>
""".format(imageName, candidate.fitness, candidate.myId))
num+=1
theGenerationIndexFile.write("""</tr></table></body></html>""")
theIndexFile.write("""<a href='../{0}'>Generation {1}</a><br> """.format(theGenerationIndexFileName, generationCount))
generationCount+=1
def newExperiment(theDestPath, theFitnessFunction, theInitialPopulation, numGenerations):
global theIndexFileName, theIndexFile, theBasePath, maxgeneration
if not os.path.exists(theDestPath):
os.mkdir(theDestPath)
maxgeneration = numGenerations
theBasePath = theDestPath
theIndexFileName=theDestPath+"/index.html"
historyIndexFileName = "../"+theBasePath+"/history/index.html"
theIndexFile=open(theIndexFileName, "w+t")
theIndexFile.write("""
<html>
<header>
</header>
<body>
<center>
<h1>Experiment {1} </h1>
<hr>
</center>
Fitness function: {0} <br>
Initial Population: {2} <br>
Number of generations: {3} <br>
History: <a href='{4}'>View</a>
<hr>
""".format(theFitnessFunction.name, str(datetime.now()), len(theInitialPopulation), numGenerations, historyIndexFileName))
def endExperiment():
global theIndexFile
theIndexFile.write("""
</body>
</html>
""")