-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
71 lines (50 loc) · 2.15 KB
/
solution.py
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
import numpy as np
import pyrosim_z as psz
import body_builder
import body_mutator
import brain_mutator
from body_parts import *
import os
import time
import constants as Cnsts
import warnings
from simulation import Simulation
class Solution:
def __init__(self, solution_id = 0, genome = None) -> None:
self.solution_id = solution_id
self.genome = genome
if self.genome is None:
self.genome = Genome(body_mutator.BASE_BODYCONS_ID , None, body_mutator.BASE_BODYPLAN)
self.mutation_rate = Cnsts.mutation_rate
self.mutation_magnitude = Cnsts.mutation_magnitude
def start_simulation(self, pybullet_method = "DIRECT") -> None:
self.generate_body()
self.generate_brain()
simulation = Simulation(pybullet_method, self.solution_id)
simulation.run()
return simulation.get_fitness()
def set_fitness(self, fitness):
self.fitness = fitness
def generate_body(self) -> None:
running = True
joint_names = []
sensor_parts = []
while running:
try:
mutated_body_plan, mutated_bodycons_id = body_mutator.mutate(self.genome.body_chromosome, self.genome.bodycons_id)
psz.Start_URDF("./data/robot/body{}.urdf".format(self.solution_id))
joint_names, sensor_parts, abstract_centers = body_builder.build_body(mutated_body_plan)
psz.end()
self.genome = Genome(mutated_bodycons_id, self.genome.brain_chromosome, mutated_body_plan)
running = False
except:
psz.end()
os.system("rm ./data/robot/body{}.urdf".format(self.solution_id))
self.joint_names = joint_names
self.sensor_parts = sensor_parts
def generate_brain(self) -> None:
psz.Start_NeuralNetwork("./data/robot/brain{}.nndf".format(self.solution_id))
weight_matrix = body_builder.build_brain(self.joint_names, self.sensor_parts)
psz.end()
self.weight_matrix = weight_matrix
self.genome = Genome(self.genome.bodycons_id, self.weight_matrix, self.genome.body_chromosome)