-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdCGPANN.py
More file actions
80 lines (70 loc) · 2.59 KB
/
dCGPANN.py
File metadata and controls
80 lines (70 loc) · 2.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
"""
Author: Fergal Stapleton
"""
import sys
import os
import json
import random
import numpy as np
from src import linearGenProgTree
from problem import neuroevolution
# randomGenerationNeuroLGP can be used to test method is better than random,
from algorithm import singleObjNeuroLGP
#from algorithm import randomGenerationNeuroLGP as singleObjNeuroLGP
def read_json(file_path):
with open(file_path, "r") as f:
return json.load(f)
def main():
print("Welcome to GenProgMO library")
paramsLoad = read_json('experimentCGPANN.json')
print(json.dumps(paramsLoad, indent=4))
# First and foremost 'what is our problem domain?''
problem_type = paramsLoad["PROBLEM DOMAIN"]["PROBLEM TYPE"]
experiment = paramsLoad["EXPERIMENTAL SETUP"]
try:
problemCode = paramsLoad["PROBLEM DOMAIN"]["PROBLEM CODE"]
dataSource = paramsLoad["PROBLEM DOMAIN"]["DATA SOURCE"] # Not really needed, but will leave in for consistency or if needed in future
except:
print("Error: one of the problem domain properties has not been specified in experiment.json")
sys.exit(1)
try:
opt = paramsLoad["OPTIMIZATION"]
except:
print("Error: one of the optimization properties has not been specified in experiment.json")
sys.exit(1)
# Set up class files
probDom = neuroevolution.CGPANN(run, gp) # problem domain
if (opt["ALGORITHM"] == 'singleObjNeuroLGP'):
print("")
print("...Loading single objective neuroevolution LGP algorithm")
print("")
fitnessList = []
modelList = []
diversityList = []
# 30 runs
for i in range(15):
alg = singleObjCGPANNSurrogate.Standard(probDom, gp, run, experiment, opt, problemCode)
alg.evolve()
fitnessList.append(alg.finalFitness)
modelList.append(alg.finalProgram)
print("----------------------")
print("")
print("Fitness for every run:")
print(fitnessList)
print("")
print("Mean and std dev for fitness across all runs:")
print(np.mean(fitnessList))
print(np.std(fitnessList))
print("")
print("Best run fitness:")
best_run_idx = fitnessList.index(min(fitnessList))
print(fitnessList[best_run_idx])
print("Best run model:")
best_list = [gene[0] for gene in modelList[best_run_idx]]
pseudo_acc = gp.levenshtein_dist(best_list, probDom.psuedo_best)
print(pseudo_acc)
alg.gp.printProgram(modelList[best_run_idx])
print("")
if __name__ == "__main__":
random.seed(12)
main()