Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #define _CRT_SECURE_NO_WARNINGS
10 
12 #include <cstdio>
13 #include <cstring>
14 #include <cstdlib>
15 #include <iostream>
16 
17 using namespace std;
18 using namespace mrpt::graphs;
19 
20 /**
21  * This is a example of problem resolution using the CAStarAlgorithm template.
22  * Although this problem is better solved with dynamic programming, it
23  * illustrates
24  * perfectly how to inherit from that template in order to solve a problem.
25  *
26  * Let's assume a currency composed of coins whose values are 2, 7, 8 and 19.
27  * The problem consists of finding a minimal set of these coins whose total
28  * value
29  * equals a given amount.
30  */
31 
33 {
34  public:
35  size_t coins2;
36  size_t coins7;
37  size_t coins8;
38  size_t coins19;
39  CCoinDistribution() : coins2(0), coins7(0), coins8(0), coins19(0) {}
40  /**
41  * Auxiliary function to calculate the amount of money. Not strictly
42  * necessary, but handy.
43  */
44  size_t money() const
45  {
46  return 2 * coins2 + 7 * coins7 + 8 * coins8 + 19 * coins19;
47  }
48  /**
49  * Implementing the == operator is mandatory, because it allows the A*
50  * algorithm to reject repeated solutions. Actually, the template should not
51  * compile if
52  * this operator is not present.
53  */
54  inline bool operator==(const CCoinDistribution& mon) const
55  {
56  return (coins2 == mon.coins2) && (coins7 == mon.coins7) &&
57  (coins8 == mon.coins8) && (coins19 == mon.coins19);
58  }
59 };
60 
61 /**
62  * To use the template, a class must be derived from CAStarAlgorithm<Solution
63  * Class>. In this case, the Solution Class is CCoinDistribution.
64  */
65 class CAStarExample : public CAStarAlgorithm<CCoinDistribution>
66 {
67  private:
68  /**
69  * Problem goal.
70  */
71  const size_t N;
72 
73  public:
74  /**
75  * When a class derives from CAStarAlgorithm, its constructor should
76  * include all the data that define the specific problem.
77  */
78  CAStarExample(size_t goal) : N(goal) {}
79  /**
80  * The following five methods must be implemented to use the algorithm:
81  */
82  virtual bool isSolutionEnded(const CCoinDistribution& s)
83  { // True if the solution is complete.
84  return s.money() == N;
85  }
86  virtual bool isSolutionValid(const CCoinDistribution& s)
87  { // True if the solution is valid.
88  return s.money() <= N;
89  }
90  virtual void generateChildren(
91  const CCoinDistribution& s, vector<CCoinDistribution>& sols)
92  { // Get all the children of a solution.
93  // Complex classes might want to define a copy constructor (note how the
94  // vector in the following line is created by cloning this object four
95  // times).
96  sols = vector<CCoinDistribution>(4, s);
97  sols[0].coins2++; // Misma solución, más una moneda de 2.
98  sols[1].coins7++; // Misma solución, más una moneda de 7.
99  sols[2].coins8++; // Misma solución, más una moneda de 8...
100  sols[3].coins19++; // Y misma solución, más una moneda de 19.
101  }
102  virtual double getHeuristic(const CCoinDistribution& s)
103  { // Heuristic cost of the remaining part of the solution.
104  // Check the documentation of CAStarAlgorithm to know which
105  // characteristics does this function need to comply.
106  return static_cast<double>(N - s.money()) / 19.0;
107  }
108  virtual double getCost(const CCoinDistribution& s)
109  { // Known cost of the partial solution.
110  return s.coins2 + s.coins7 + s.coins8 + s.coins19;
111  }
112 };
113 
114 /**
115  * Main function. Just calls the A* algorithm as many times as needed.
116  */
117 int main(int argc, char** argv)
118 {
119  for (;;)
120  {
121  char text[11];
122  printf(
123  "Input an integer number to solve a problem, or \"e\" to end.\n");
124  if (1 != scanf(
125  "%10s",
126  text)) // GCC warning: scanf -> return cannot be ignored!
127  {
128  printf("Please, input a positive integer.\n\n");
129  continue;
130  }
131  if (strlen(text) == 1 && (text[0] == 'e' || text[0] == 'E')) break;
132  int val = atoi(text);
133  if (val <= 0)
134  {
135  printf("Please, input a positive integer.\n\n");
136  continue;
137  }
138  // The solution objects and the problem are created...
139  CCoinDistribution solIni,
140  solFin; // Initial solution automatically initialized to (0,0,0,0).
141  CAStarExample prob(static_cast<size_t>(val));
142  switch (prob.getOptimalSolution(solIni, solFin, HUGE_VAL, 15))
143  {
144  case 0:
145  printf(
146  "No solution has been found. Either the number is too "
147  "small, or the time elapsed has exceeded 15 seconds.\n\n");
148  break;
149  case 1:
150  printf("An optimal solution has been found:\n");
151  printf(
152  "\t%u coins of 2 piastres.\n\t%u coins of 7 "
153  "piastres.\n\t%u coins of 8 piastres.\n\t%u coins of 19 "
154  "piastres.\n\n",
155  (unsigned)solFin.coins2, (unsigned)solFin.coins7,
156  (unsigned)solFin.coins8, (unsigned)solFin.coins19);
157  break;
158  case 2:
159  printf(
160  "A solution has been found, although it may not be "
161  "optimal:\n");
162  printf(
163  "\t%u coins of 2 piastres.\n\t%u coins of 7 "
164  "piastres.\n\t%u coins of 8 piastres.\n\t%u coins of 19 "
165  "piastres.\n\n",
166  (unsigned)solFin.coins2, (unsigned)solFin.coins7,
167  (unsigned)solFin.coins8, (unsigned)solFin.coins19);
168  break;
169  }
170  }
171  return 0;
172 }
CAStarAlgorithm.h
s
GLdouble s
Definition: glext.h:3676
CAStarExample
To use the template, a class must be derived from CAStarAlgorithm<Solution Class>.
Definition: vision_stereo_rectify/test.cpp:65
CCoinDistribution
This is a example of problem resolution using the CAStarAlgorithm template.
Definition: vision_stereo_rectify/test.cpp:32
mrpt::img::operator==
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:201
CCoinDistribution::coins2
size_t coins2
Definition: vision_stereo_rectify/test.cpp:35
mrpt::graphs::CAStarAlgorithm
This class is intended to efficiently solve graph-search problems using heuristics to determine the b...
Definition: CAStarAlgorithm.h:41
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
val
int val
Definition: mrpt_jpeglib.h:955
mrpt::graphs
Abstract graph and tree data structures, plus generic graph algorithms.
Definition: CAStarAlgorithm.h:18
CCoinDistribution::coins19
size_t coins19
Definition: vision_stereo_rectify/test.cpp:38
CCoinDistribution::coins7
size_t coins7
Definition: vision_stereo_rectify/test.cpp:36
CCoinDistribution::coins8
size_t coins8
Definition: vision_stereo_rectify/test.cpp:37



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at mié 12 jul 2023 10:03:34 CEST