def test_load_scenario(self):
     a = load_scenario(filename=self.example)
     b = load_scenario(filename=self.example, scenario_name="example")
     expected = ", ".join([
         "{'controller_params': [5, 50, 5, '1e3']",
         "'initial_state': [1.2, 0, 0, 0]", "'t_final': 30.0",
         "'tape_period': 0.05}"
     ])
     self.assertEqual(str(a), expected)
     self.assertEqual(str(b), expected)
     self.assertEqual(a, b)
Beispiel #2
0
    def test_stochastic_scenario(self):
        if _backend == "py":
            return  # Python backend does not support stochastic scenarios.

        scenario = FindResourceOrThrow(
            "drake/examples/acrobot/dev/test/example_stochastic_scenario.yaml")
        output = os.path.join(os.environ["TEST_TMPDIR"], "output.yaml")
        dump_file = os.path.join(os.environ["TEST_TMPDIR"],
                                 "scenario_out.yaml")
        subprocess.check_call([
            self.dut, "--scenario", scenario, "--output", output,
            "--dump_scenario", dump_file])
        x_tape = load_output(filename=output)
        # 4 states x 30 seconds of samples at 20 Hz per sample_scenario.
        self.assertEqual(x_tape.shape, (4, 601))

        # Load the scenario dump; don't bother checking exact distribution
        # semantics; just check that we have the right data shape and type.
        dump = load_scenario(filename=dump_file)
        self.assertEqual(len(dump["controller_params"]), 4)
        self.assertTrue(all(type(x) == float
                            for x in dump["controller_params"]))
        self.assertEqual(len(dump["initial_state"]), 4)
        self.assertTrue(all(type(x) == float
                            for x in dump["initial_state"]))
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser(__doc__)
    parser.add_argument(
        "--scenario_file", "-f", type=str, default=None,
        help="Scenario to run (default is example_stochastic_scenario.yaml)")
    parser.add_argument(
        "--metric", "-m", type=str, choices=METRICS.keys(),
        default="ensemble_cost")
    parser.add_argument(
        "--ensemble_size", "-e", type=int, default=10,
        help="Size of ensemble for each cost function evaluation.")
    parser.add_argument(
        "--num_evaluations", "-n", type=int, default=250,
        help="Cost function call budget of the optimizer.")
    parser.add_argument(
        "--output", "-o", type=argparse.FileType("w"), default=sys.stdout,
        help="File to write the optimized output (default=stdout)")
    args = parser.parse_args()
    with closing(args.output) as output:
        scenario_file = args.scenario_file or FindResourceOrThrow(
            "drake/examples/acrobot/dev/test/example_stochastic_scenario.yaml")
        input_scenario = load_scenario(filename=scenario_file)
        result = optimize_controller_params(
            scenario=input_scenario,
            metric=METRICS[args.metric],
            ensemble_size=args.ensemble_size,
            num_evaluations=args.num_evaluations)
        output_scenario = input_scenario
        output_scenario["controller_params"] = result
        output.write(save_scenario(scenario=output_scenario,
                                   scenario_name="optimized_scenario"))
Beispiel #4
0
def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--scenario",
                        metavar="*.yaml",
                        help="Scenario file to load (required).")
    parser.add_argument("--output",
                        metavar="*.yaml",
                        help="Output file to save (required).")
    args = parser.parse_args()
    if not args.scenario:
        parser.error("A --scenario file is required.")
    if not args.output:
        parser.error("An --output file is required.")
    scenario = load_scenario(filename=args.scenario)
    x_tape = simulate(**scenario)
    text = save_output(x_tape=x_tape)
    with open(args.output, 'w') as handle:
        handle.write(text)
    return 0
 def test_save_scenario(self):
     scenario = load_scenario(filename=self.example)
     actual = save_scenario(scenario_name="example", scenario=scenario)
     self.assertEqual(actual, self.expected_save)