BRL-CAD
solver_test.cpp
Go to the documentation of this file.
1 /* S O L V E R _ T E S T . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file solver_test.cpp
21  *
22  * @brief Simple Test cases for Constraint Solver
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <iostream>
29 #include <utility> // for std::pair
30 #include <algorithm> // for std::for_each
31 
32 #include "pcVariable.h"
33 #include "pcVCSet.h"
34 #include "pcNetwork.h"
35 #include "pcSolver.h"
36 #include "pcParser.h"
37 #include "pcMathVM.h"
38 #include "pc.h"
39 
40 typedef Variable<int> *Vip;
41 /* Constraint functions */
42 struct f1 {
43 public:
44  bool operator() (VCSet & vcset, std::list<std::string> UNUSED(Vid)) const {
45  typedef Variable<int> *Vi ;
46  int A = ((Variable<int>*) vcset.getVariablebyID("A"))->getValue();
47  int B = ((Vi) vcset.getVariablebyID("B"))->getValue();
48  return (A * B == 12);
49  }
50 } f1;
51 struct f2 {
52 public:
53  bool operator() (VCSet & vcset, std::list<std::string> UNUSED(Vid)) const {
54  typedef Variable<int> *Vi ;
55  int B = ((Vi) vcset.getVariablebyID("B"))->getValue();
56  int C = ((Vi) vcset.getVariablebyID("C"))->getValue();
57  return (B + C < 5);
58  }
59 } f2;
60 
61 struct f3 {
62 public:
63  bool operator() (VCSet & vcset, std::list<std::string> UNUSED(Vid)) const {
64  typedef Variable<int> *Vi ;
65  int A = ((Vi) vcset.getVariablebyID("A"))->getValue();
66  int D = ((Vi) vcset.getVariablebyID("D"))->getValue();
67  return (A - D == 2);
68  }
69 } f3;
70 
71 struct f4 {
72 public:
73  bool operator() (VCSet & vcset, std::list<std::string> UNUSED(Vid)) const {
74  typedef Variable<int> *Vi ;
75  int C = ((Vi) vcset.getVariablebyID("C"))->getValue();
76  int A = ((Vi) vcset.getVariablebyID("A"))->getValue();
77  return (A * C == 4);
78  }
79 } f4;
80 
81 int main()
82 {
83  BackTrackSolver<double> GBTS; /* Generic Backtracker */
84  PCSolver<double> GPCS; /* Generic Solver */
85  Solution<double> S1;
86  struct pc_pc_set pcs;
87  VCSet vc_set;
88 
89  pc_init_pcset(&pcs);
90  fastf_t D = 8.04;
91  point_t E = {8.04, 3.2, 0.0};
92  vect_t F = {5.4, 3.6, 4.4};
93  pc_pushparam_struct(&pcs, "D", PC_DB_FASTF_T, &D);
94  pc_pushparam_struct(&pcs, "E", PC_DB_POINT_T, &E);
95  pc_pushparam_struct(&pcs, "G", PC_DB_VECTOR_T, &F);
96  const char *args[] = { "F", "G" };
97  pc_pushconstraint_expr(&pcs, "Constraint-test", "A + B < 0");
98  pc_pushparam_struct(&pcs, "F", PC_DB_VECTOR_T, &F);
99  pc_constrnt *con;
100  pc_mk_isperpendicular(&con, "G _|_ F", args);
101  pc_pushconstraint_struct(&pcs, "Struct-test", 2, 3, &pc_isperpendicular, args);
102  pc_free_constraint(con);
103 
104  Parser myparser(vc_set); /* generate vc_set from pcs using parser */
105  myparser.parse(&pcs);
106 
107  /* modify/access parameter property in vc_set using getParameter */
108  vc_set.getParameter("G")->setConst(true);
109 
110  /* Two solution methods*/
111  vc_set.display();
112  GBTS.solve(vc_set, S1);
113  std::cout << "\nSolution using Generic BackTracking Solver "
114  << GBTS.numChecks() << "\t" << GBTS.numSolutions() << std::endl;
115  S1.display();
116  S1.clear();
117 
118  vc_set.display();
119  GPCS.solve(vc_set, S1);
120  std::cout << "\nSolution using Generic Solver "
121  << GPCS.numChecks() << "\t" << GPCS.numSolutions() << std::endl;
122  S1.cdisplay();
123 
124  pc_free_pcset(&pcs);
125 
126  /* NOTE: THE BELOW TESTS ARE VOLATILE.
127  *
128  * This is why they are disabled, kept as an example of PC solving
129  * in use and for development continuation.
130  */
131 #if 0
132  /** Testing PCSolver Methods */
133 
134  VCSet myvcset;
135  Solution<int> S;
136 
137  /* add Variables to VCSet */
138  myvcset.addVariable<int>("A", 1, 0, 5, 1);
139  myvcset.addVariable<int>("B", 3, 0, 5, 1);
140  myvcset.addVariable<int>("C", 2, 0, 5, 1);
141  myvcset.addVariable<int>("D", 0, 0, 5, 1);
142 
143  /* add Constraints to VCSet */
144  myvcset.addConstraint("0", "A * B = 12", f1, 2, "A", "B");
145  myvcset.addConstraint("1", "B + C < 5", f2, 2, "B", "C");
146  myvcset.addConstraint("2", "A - D = 2", f3, 2, "A", "D");
147  myvcset.addConstraint("3", "A * C = 4", f4, 2, "A", "C");
148  myvcset.display();
149 
150  BinaryNetwork<int > N(myvcset);
151  N.display();
152 
153  GTSolver<int> GTS;
154  BackTrackSolver<int> BTS;
155  PCSolver<int> PCS;
156 
157  std::cout << "-----------------------------" << std::endl;
158  GTS.solve(N, S);
159  std::cout << "Solution using Generate-Test" << std::endl;
160  S.display();
161  S.clear();
162  std::cout << "-----------------------------" << std::endl;
163  BTS.solve(myvcset, S);
164  std::cout << "Solution using BackTracking" << std::endl;
165  S.display();
166  S.clear();
167  std::cout << "-----------------------------" << std::endl;
168  PCS.solve(myvcset, S);
169  std::cout << "Solution using Generic GT Solver" << std::endl;
170  S.display();
171  std::cout << "-----------------------------" << std::endl;
172  std::cout << "Number of Constraint checks performed" << std::endl;
173  std::cout << "Generate-Test:" << GTS.numChecks() << std::endl;
174  std::cout << "BackTracking based:" << BTS.numChecks() << std::endl;
175  std::cout << "Generic Generate-Test:" << PCS.numChecks() << std::endl;
176 #endif
177  return 0;
178 }
179 
180 
181 /*
182  * Local Variables:
183  * mode: C++
184  * tab-width: 8
185  * indent-tabs-mode: t
186  * c-file-style: "stroustrup"
187  * End:
188  * ex: shiftwidth=4 tabstop=8
189  */
bool operator()(VCSet &vcset, std::list< std::string > Vid) const
Definition: solver_test.cpp:53
fastf_t C[2 *MAX_CNT+1][2 *MAX_CNT+1]
Definition: dsp_brep.cpp:38
#define PC_DB_POINT_T
Definition: pc.h:63
#define N
Definition: randmt.c:39
Definition: pc.h:108
void pc_pushconstraint_struct(struct pc_pc_set *pcs, const char *name, int nargs, int dimension, int(*fp)(double **args), const char **args)
Header file for the BRL-CAD common definitions.
#define PC_DB_VECTOR_T
Definition: pc.h:64
struct f4 f4
int main()
Definition: solver_test.cpp:81
void pc_init_pcset(struct pc_pc_set *pcs)
void pc_free_pcset(struct pc_pc_set *pcs)
struct f2 f2
struct f3 f3
Definition: pc.h:97
bool operator()(VCSet &vcset, std::list< std::string > Vid) const
Definition: solver_test.cpp:44
struct f1 f1
#define PC_DB_FASTF_T
Definition: pc.h:62
Variable< int > * Vip
Definition: solver_test.cpp:40
#define UNUSED(parameter)
Definition: common.h:239
bool operator()(VCSet &vcset, std::list< std::string > Vid) const
Definition: solver_test.cpp:73
int pc_isperpendicular(double **v)
void pc_free_constraint(struct pc_constrnt *c)
#define A
Definition: msr.c:51
bool operator()(VCSet &vcset, std::list< std::string > Vid) const
Definition: solver_test.cpp:63
void pc_mk_isperpendicular(struct pc_constrnt **c, const char *name, const char **args)
void pc_pushparam_struct(struct pc_pc_set *pcs, const char *name, int type, void *ptr)
void pc_pushconstraint_expr(struct pc_pc_set *pcsp, const char *name, const char *str)
double fastf_t
Definition: defines.h:300