Loading...
Searching...
No Matches
Koules.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Beck Chen, Mark Moll */
36
65
66#include "KoulesConfig.h"
67#include "KoulesSetup.h"
68#include "KoulesStateSpace.h"
69#include <ompl/tools/benchmark/Benchmark.h>
70#include <ompl/config.h>
71#include <boost/program_options.hpp>
72#include <boost/format.hpp>
73#include <fstream>
74
75namespace ob = ompl::base;
76namespace oc = ompl::control;
77namespace ot = ompl::tools;
78namespace po = boost::program_options;
79
80void writeParams(std::ostream &out)
81{
82 out << sideLength << ' ' << shipRadius << ' ' << kouleRadius << ' ' << ' ' << propagationStepSize << ' '
83 << shipAcceleration << ' ' << shipRotVel << ' ' << shipDelta << ' ' << shipEps << std::endl;
84}
85
86void plan(KoulesSetup &ks, double maxTime, const std::string &outputFile)
87{
88 if (ks.solve(maxTime))
89 {
90 std::ofstream out(outputFile.c_str());
91 oc::PathControl path(ks.getSolutionPath());
92 path.interpolate();
93 if (!path.check())
94 OMPL_ERROR("Path is invalid");
95 writeParams(out);
96 path.printAsMatrix(out);
97 if (!ks.haveExactSolutionPath())
98 OMPL_INFORM("Solution is approximate. Distance to actual goal is %g",
99 ks.getProblemDefinition()->getSolutionDifference());
100 OMPL_INFORM("Output saved in %s", outputFile.c_str());
101 }
102
103#if 0
104 // Get the planner data, save the ship's (x,y) coordinates to one file and
105 // the edge information to another file. This can be used for debugging
106 // purposes; plotting the tree of states might give you some idea of
107 // a planner's strategy.
108 ob::PlannerData pd(ks.getSpaceInformation());
109 ks.getPlannerData(pd);
110 std::ofstream vertexFile((outputFile + "-vertices").c_str()), edgeFile((outputFile + "-edges").c_str());
111 double* coords;
112 unsigned numVerts = pd.numVertices();
113 std::vector<unsigned int> edgeList;
114
115 for (unsigned int i = 0; i < numVerts; ++i)
116 {
117 coords = pd.getVertex(i).getState()->as<KoulesStateSpace::StateType>()->values;
118 vertexFile << coords[0] << ' ' << coords[1] << '\n';
119
120 pd.getEdges(i, edgeList);
121 for (unsigned int j = 0; j < edgeList.size(); ++j)
122 edgeFile << i << ' ' << edgeList[j] << '\n';
123 }
124#endif
125}
126
127void benchmark(KoulesSetup &ks, ot::Benchmark::Request request, const std::string &plannerName,
128 const std::string &outputFile)
129{
130 // Create a benchmark class
131 ompl::tools::Benchmark b(ks, "Koules");
132 b.addExperimentParameter("num_koules", "INTEGER", std::to_string((ks.getStateSpace()->getDimension() - 5) / 4));
133 // Add the planner to evaluate
134 b.addPlanner(ks.getConfiguredPlannerInstance(plannerName));
135 // Start benchmark
136 b.benchmark(request);
137 // Save the results
138 b.saveResultsToFile(outputFile.c_str());
139 OMPL_INFORM("Output saved in %s", outputFile.c_str());
140}
141
142int main(int argc, char **argv)
143{
144 try
145 {
146 unsigned int numKoules, numRuns;
147 double maxTime, kouleVel;
148 std::string plannerName, outputFile;
149 po::options_description desc("Options");
150 desc.add_options()("help", "show help message")("plan", "solve the game of koules")(
151 "benchmark", "benchmark the game of koules")(
152 "numkoules", po::value<unsigned int>(&numKoules)->default_value(3), "start from <numkoules> koules")(
153 "maxtime", po::value<double>(&maxTime)->default_value(10.),
154 "time limit in seconds")("output", po::value<std::string>(&outputFile), "output file name")(
155 "numruns", po::value<unsigned int>(&numRuns)->default_value(10),
156 "number of runs for each planner in benchmarking mode")(
157 "planner", po::value<std::string>(&plannerName)->default_value("kpiece"),
158 "planning algorithm to use (pdst, kpiece, sst, rrt, est, sycloprrt, or syclopest)")(
159 "velocity", po::value<double>(&kouleVel)->default_value(0.), "initial velocity of each koule");
160
161 po::variables_map vm;
162 po::store(po::parse_command_line(argc, argv, desc,
163 po::command_line_style::unix_style ^ po::command_line_style::allow_short),
164 vm);
165 po::notify(vm);
166
167 KoulesSetup ks(numKoules, plannerName, kouleVel);
168 if ((vm.count("help") != 0u) || argc == 1)
169 {
170 std::cout << "Solve the games of Koules.\nSelect one of these two options:\n"
171 << "\"--plan\", or \"--benchmark\"\n\n"
172 << desc << "\n";
173 return 1;
174 }
175
176 if (outputFile.empty())
177 {
178 std::string prefix(vm.count("plan") != 0u ? "koules_" : "koulesBenchmark_");
179 outputFile = boost::str(boost::format("%1%%2%_%3%_%4%.dat") % prefix % numKoules % plannerName % maxTime);
180 }
181 if (vm.count("plan") != 0u)
182 plan(ks, maxTime, outputFile);
183 else if (vm.count("benchmark") != 0u)
184 benchmark(ks, ot::Benchmark::Request(maxTime, 10000.0, numRuns), plannerName, outputFile);
185 }
186 catch (std::exception &e)
187 {
188 std::cerr << "Error: " << e.what() << "\n";
189 return 1;
190 }
191 catch (...)
192 {
193 std::cerr << "Exception of unknown type!\n";
194 }
195
196 return 0;
197}
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
Definition of a control path.
Definition PathControl.h:61
Benchmark a set of planners on a problem instance.
Definition Benchmark.h:49
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition Console.h:68
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition Console.h:64
This namespace contains sampling based planning routines shared by both planning under geometric cons...
This namespace contains sampling based planning routines used by planning under differential constrai...
Definition Control.h:45
Includes various tools such as self config, benchmarking, etc.
Representation of a benchmark request.
Definition Benchmark.h:152