-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update dataset generator for dkitty and ant
- Loading branch information
1 parent
c0f6c48
commit c6737e1
Showing
6 changed files
with
222 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import cma | ||
import numpy as np | ||
import multiprocessing | ||
import os | ||
import argparse | ||
import itertools | ||
from design_bench.oracles.exact.ant_morphology_oracle import AntMorphologyOracle | ||
from design_bench.datasets.continuous.ant_morphology_dataset import AntMorphologyDataset | ||
from morphing_agents.mujoco.ant.designs import DEFAULT_DESIGN | ||
from morphing_agents.mujoco.ant.designs import normalize_design_vector | ||
from morphing_agents.mujoco.ant.designs import denormalize_design_vector | ||
|
||
|
||
def single_evaluate(design): | ||
placeholder_dataset = AntMorphologyDataset() | ||
oracle = AntMorphologyOracle(placeholder_dataset) | ||
return oracle.predict( | ||
denormalize_design_vector(design)[np.newaxis].astype(np.float32))[0] | ||
|
||
|
||
pool = multiprocessing.Pool() | ||
|
||
|
||
def many_evaluate(designs): | ||
return pool.map(single_evaluate, designs) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
parser = argparse.ArgumentParser("Process Raw Ant Morphologies") | ||
parser.add_argument("--shard-folder", type=str, default="./") | ||
parser.add_argument("--samples", type=int, default=25000) | ||
args = parser.parse_args() | ||
|
||
os.makedirs(os.path.join( | ||
args.shard_folder, f"ant_morphology/"), exist_ok=True) | ||
|
||
golden_design = normalize_design_vector( | ||
np.concatenate(DEFAULT_DESIGN, axis=0)) | ||
|
||
sigma = 0.02 | ||
max_iterations = 250 | ||
save_every = 1 | ||
|
||
designs = [denormalize_design_vector(golden_design)] | ||
predictions = [single_evaluate(golden_design)] | ||
|
||
for i in itertools.count(): | ||
|
||
initial_design = golden_design + \ | ||
np.random.normal(0, 0.075, golden_design.shape) | ||
|
||
es = cma.CMAEvolutionStrategy(initial_design, sigma) | ||
|
||
step = 0 | ||
while not es.stop() and step < max_iterations: | ||
|
||
if len(designs) >= args.samples: | ||
break | ||
|
||
xs = es.ask() | ||
ys = many_evaluate(xs) | ||
|
||
es.tell(xs, [-yi[0] for yi in ys]) | ||
|
||
step += 1 | ||
|
||
if step % save_every == 0: | ||
designs.extend([denormalize_design_vector(xi) for xi in xs]) | ||
predictions.extend(ys) | ||
|
||
print(f"CMA-ES ({len(designs)} samples)" | ||
f" - Restart {i+1}" | ||
f" - Step {step+1}/{max_iterations}" | ||
f" - Current Objective Value = {np.mean(ys)}") | ||
|
||
np.save(os.path.join( | ||
args.shard_folder, | ||
f"ant_morphology/ant_morphology-x-0.npy"), | ||
np.array(designs).astype(np.float32)) | ||
|
||
np.save(os.path.join( | ||
args.shard_folder, | ||
f"ant_morphology/ant_morphology-y-0.npy"), | ||
np.array(predictions).astype(np.float32).reshape([-1, 1])) | ||
|
||
if len(designs) >= args.samples: | ||
break | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import cma | ||
import numpy as np | ||
import multiprocessing | ||
import os | ||
import argparse | ||
import itertools | ||
from design_bench.oracles.exact.dkitty_morphology_oracle import DKittyMorphologyOracle | ||
from design_bench.datasets.continuous.dkitty_morphology_dataset import DKittyMorphologyDataset | ||
from morphing_agents.mujoco.dkitty.designs import DEFAULT_DESIGN | ||
from morphing_agents.mujoco.dkitty.designs import normalize_design_vector | ||
from morphing_agents.mujoco.dkitty.designs import denormalize_design_vector | ||
|
||
|
||
def single_evaluate(design): | ||
placeholder_dataset = DKittyMorphologyDataset() | ||
oracle = DKittyMorphologyOracle(placeholder_dataset) | ||
return oracle.predict( | ||
denormalize_design_vector(design)[np.newaxis].astype(np.float32))[0] | ||
|
||
|
||
pool = multiprocessing.Pool() | ||
|
||
|
||
def many_evaluate(designs): | ||
return pool.map(single_evaluate, designs) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
parser = argparse.ArgumentParser("Process Raw DKitty Morphologies") | ||
parser.add_argument("--shard-folder", type=str, default="./") | ||
parser.add_argument("--samples", type=int, default=25000) | ||
args = parser.parse_args() | ||
|
||
os.makedirs(os.path.join( | ||
args.shard_folder, f"dkitty_morphology/"), exist_ok=True) | ||
|
||
golden_design = normalize_design_vector( | ||
np.concatenate(DEFAULT_DESIGN, axis=0)) | ||
|
||
sigma = 0.02 | ||
max_iterations = 250 | ||
save_every = 1 | ||
|
||
designs = [denormalize_design_vector(golden_design)] | ||
predictions = [single_evaluate(golden_design)] | ||
|
||
for i in itertools.count(): | ||
|
||
initial_design = golden_design + \ | ||
np.random.normal(0, 0.1, golden_design.shape) | ||
|
||
es = cma.CMAEvolutionStrategy(initial_design, sigma) | ||
|
||
step = 0 | ||
while not es.stop() and step < max_iterations: | ||
|
||
if len(designs) >= args.samples: | ||
break | ||
|
||
xs = es.ask() | ||
ys = many_evaluate(xs) | ||
|
||
es.tell(xs, [-yi[0] for yi in ys]) | ||
|
||
step += 1 | ||
|
||
if step % save_every == 0: | ||
designs.extend([denormalize_design_vector(xi) for xi in xs]) | ||
predictions.extend(ys) | ||
|
||
print(f"CMA-ES ({len(designs)} samples)" | ||
f" - Restart {i+1}" | ||
f" - Step {step+1}/{max_iterations}" | ||
f" - Current Objective Value = {np.mean(ys)}") | ||
|
||
np.save(os.path.join( | ||
args.shard_folder, | ||
f"dkitty_morphology/dkitty_morphology-x-0.npy"), | ||
np.array(designs).astype(np.float32)) | ||
|
||
np.save(os.path.join( | ||
args.shard_folder, | ||
f"dkitty_morphology/dkitty_morphology-y-0.npy"), | ||
np.array(predictions).astype(np.float32).reshape([-1, 1])) | ||
|
||
if len(designs) >= args.samples: | ||
break | ||
|
Oops, something went wrong.