prepare for experiment
This commit is contained in:
44
examples/enhane_xor.py
Normal file
44
examples/enhane_xor.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
import jax
|
||||
from utils import Configer
|
||||
from algorithms.neat import Pipeline
|
||||
from time_utils import using_cprofile
|
||||
from algorithms.neat.function_factory import FunctionFactory
|
||||
from problems import EnhanceLogic
|
||||
import time
|
||||
|
||||
|
||||
def evaluate(problem, func):
|
||||
inputs = problem.ask_for_inputs()
|
||||
pop_predict = jax.device_get(func(inputs))
|
||||
# print(pop_predict)
|
||||
fitnesses = []
|
||||
for predict in pop_predict:
|
||||
f = problem.evaluate_predict(predict)
|
||||
fitnesses.append(f)
|
||||
return np.array(fitnesses)
|
||||
|
||||
# @using_cprofile
|
||||
# @partial(using_cprofile, root_abs_path='/mnt/e/neatax/', replace_pattern="/mnt/e/neat-jax/")
|
||||
def main():
|
||||
tic = time.time()
|
||||
config = Configer.load_config()
|
||||
problem = EnhanceLogic("xor", n=3)
|
||||
problem.refactor_config(config)
|
||||
function_factory = FunctionFactory(config)
|
||||
evaluate_func = lambda func: evaluate(problem, func)
|
||||
pipeline = Pipeline(config, function_factory, seed=33413)
|
||||
print("start run")
|
||||
pipeline.auto_run(evaluate_func)
|
||||
|
||||
total_time = time.time() - tic
|
||||
compile_time = pipeline.function_factory.compile_time
|
||||
total_it = pipeline.generation
|
||||
mean_time_per_it = (total_time - compile_time) / total_it
|
||||
evaluate_time = pipeline.evaluate_time
|
||||
print(f"total time: {total_time:.2f}s, compile time: {compile_time:.2f}s, real_time: {total_time - compile_time:.2f}s, evaluate time: {evaluate_time:.2f}s")
|
||||
print(f"total it: {total_it}, mean time per it: {mean_time_per_it:.2f}s")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
56
examples/final_design_experiement.py
Normal file
56
examples/final_design_experiement.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import numpy as np
|
||||
import jax
|
||||
from utils import Configer
|
||||
from algorithms.neat import Pipeline
|
||||
from time_utils import using_cprofile
|
||||
from algorithms.neat.function_factory import FunctionFactory
|
||||
from problems import EnhanceLogic
|
||||
import time
|
||||
|
||||
|
||||
def evaluate(problem, func):
|
||||
outs = func(problem.inputs)
|
||||
outs = jax.device_get(outs)
|
||||
fitnesses = -np.mean((problem.outputs - outs) ** 2, axis=(1, 2))
|
||||
return fitnesses
|
||||
|
||||
|
||||
def main():
|
||||
config = Configer.load_config()
|
||||
problem = EnhanceLogic("xor", n=3)
|
||||
problem.refactor_config(config)
|
||||
function_factory = FunctionFactory(config)
|
||||
evaluate_func = lambda func: evaluate(problem, func)
|
||||
|
||||
# precompile
|
||||
pipeline = Pipeline(config, function_factory, seed=114514)
|
||||
pipeline.auto_run(evaluate_func)
|
||||
|
||||
for r in range(10):
|
||||
print(f"running: {r}/{10}")
|
||||
tic = time.time()
|
||||
|
||||
pipeline = Pipeline(config, function_factory, seed=r)
|
||||
pipeline.auto_run(evaluate_func)
|
||||
|
||||
total_time = time.time() - tic
|
||||
evaluate_time = pipeline.evaluate_time
|
||||
total_it = pipeline.generation
|
||||
print(f"total time: {total_time:.2f}s, evaluate time: {evaluate_time:.2f}s, total_it: {total_it}")
|
||||
|
||||
if total_it >= 500:
|
||||
res = "fail"
|
||||
else:
|
||||
res = "success"
|
||||
|
||||
with open("log", "wb") as f:
|
||||
f.write(f"{res}, total time: {total_time:.2f}s, evaluate time: {evaluate_time:.2f}s, total_it: {total_it}\n".encode("utf-8"))
|
||||
f.write(str(pipeline.generation_time_list).encode("utf-8"))
|
||||
|
||||
compile_time = function_factory.compile_time
|
||||
|
||||
print("total_compile_time:", compile_time)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,19 +1,27 @@
|
||||
from functools import partial
|
||||
|
||||
from utils import Configer
|
||||
from algorithms.neat import Pipeline
|
||||
from time_utils import using_cprofile
|
||||
from problems import Sin, Xor, DIY
|
||||
import time
|
||||
|
||||
|
||||
@using_cprofile
|
||||
# @using_cprofile
|
||||
# @partial(using_cprofile, root_abs_path='/mnt/e/neatax/', replace_pattern="/mnt/e/neat-jax/")
|
||||
def main():
|
||||
tic = time.time()
|
||||
config = Configer.load_config()
|
||||
problem = Xor()
|
||||
problem.refactor_config(config)
|
||||
pipeline = Pipeline(config, seed=1)
|
||||
pipeline.auto_run(problem.evaluate)
|
||||
pipeline = Pipeline(config, seed=6)
|
||||
nodes, cons = pipeline.auto_run(problem.evaluate)
|
||||
# print(nodes, cons)
|
||||
total_time = time.time() - tic
|
||||
compile_time = pipeline.function_factory.compile_time
|
||||
total_it = pipeline.generation
|
||||
mean_time_per_it = (total_time - compile_time) / total_it
|
||||
evaluate_time = pipeline.evaluate_time
|
||||
print(f"total time: {total_time:.2f}s, compile time: {compile_time:.2f}s, real_time: {total_time - compile_time:.2f}s, evaluate time: {evaluate_time:.2f}s")
|
||||
print(f"total it: {total_it}, mean time per it: {mean_time_per_it:.2f}s")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user