File: simple.cc

package info (click to toggle)
roboptim-core 2.0-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,488 kB
  • ctags: 1,160
  • sloc: cpp: 5,388; sh: 395; ansic: 387; makefile: 25; python: 19
file content (170 lines) | stat: -rw-r--r-- 4,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2009 by Thomas Moulard, AIST, CNRS, INRIA.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim.  If not, see <http://www.gnu.org/licenses/>.

#include "shared-tests/fixture.hh"

#include <iostream>

#include <roboptim/core/io.hh>
#include <roboptim/core/plugin/dummy.hh>

using namespace roboptim;

// Specify the solver that will be used.
typedef DummySolver solver_t;

boost::shared_ptr<boost::test_tools::output_test_stream> output;

// Define a simple function.
struct F : public Function
{
  F () : Function (4, 1, "a * d * (a + b + c) + d")
  {}

  void impl_compute (result_t& result,
		     const argument_t& argument) const throw ()
  {
    result (0) = argument[0] * argument[3]
      * (argument[0] + argument[1] + argument[2]) + argument[3];
  }

  // No gradient, hessian.
};


struct CountUnaryFunction
{
  typedef void Result;

  CountUnaryFunction (int& cnt)
    : cnt_ (cnt)
  {}

  void operator () (double x)
  {
    (*output) << "Discrete point: " << x
	      << " (cnt: " << ++cnt_ << ")"
	      << std::endl;
  }

private:
  int& cnt_;
};


BOOST_FIXTURE_TEST_SUITE (core, TestSuiteConfiguration)

BOOST_AUTO_TEST_CASE (simple)
{
  output = retrievePattern ("simple");

  // Instantiate the function and the problem.
  F f;
  DummySolver::problem_t pb (f);

  (*output) << pb << std::endl;

  // Check that the problem is well formed.
  BOOST_CHECK_EQUAL (&pb.function (), &f);
  BOOST_CHECK_EQUAL (pb.constraints ().size (), 0u);
  BOOST_CHECK (!pb.startingPoint ());

  Function::vector_t x (4);
  x.setZero ();
  pb.startingPoint () = x;
  BOOST_CHECK_EQUAL (pb.startingPoint ()
  	  && pb.startingPoint ()->size (), 4);

  BOOST_CHECK_EQUAL (pb.boundsVector ().size (), 0u);
  BOOST_CHECK_EQUAL (pb.argumentBounds ().size (), 4u);
  BOOST_CHECK
    (pb.argumentBounds ()[0] == Function::makeInfiniteInterval ()
     && pb.argumentBounds ()[1] == Function::makeInfiniteInterval ()
     && pb.argumentBounds ()[2] == Function::makeInfiniteInterval ()
     && pb.argumentBounds ()[3] == Function::makeInfiniteInterval ());


  BOOST_CHECK_EQUAL (pb.scalesVector ().size (), 0u);
  BOOST_CHECK_EQUAL (pb.argumentScales ().size (), 4u);
  BOOST_CHECK
    (pb.argumentScales ()[0] == 1.
     && pb.argumentScales ()[1] == 1.
     && pb.argumentScales ()[2] == 1.
     && pb.argumentScales ()[3] == 1.);

  F* g = new F ();
  pb.addConstraint (boost::shared_ptr<F> (g),
  		    Function::makeInterval (0., 5.), 3.5);
  BOOST_CHECK_EQUAL (pb.constraints ().size (), 1u);
  BOOST_CHECK (&pb.constraints ()[0] != 0);
  BOOST_CHECK_EQUAL (pb.boundsVector ().size (), 1u);
  BOOST_CHECK_EQUAL (pb.boundsVector ()[0][0].first,
		     0.);
  BOOST_CHECK_EQUAL (pb.boundsVector ()[0][0].second,
		     5.);
  BOOST_CHECK_EQUAL (pb.scalesVector ().size (), 1u);
  BOOST_CHECK_EQUAL (pb.scalesVector ()[0][0], 3.5);


  // Try to solve it with the DummySolver (direct instantiation, no plug-in).
  solver_t solver (pb);
  solver_t::result_t res = solver.minimum ();
  solver.getMinimum<SolverError> ();

  (*output) << pb << std::endl
            << "---" << std::endl
            << solver << std::endl;

  // Try to get the minimum from a GenericSolver*.
  GenericSolver* gs = &solver;
  (*output) << gs->getMinimum<SolverError> ().what ()
            << std::endl;

  // Check iteration in discrete intervals.
  {
    int cnt = 0;
    CountUnaryFunction count (cnt);

    {
      Function::discreteInterval_t interval (2.3, 3., 0.5);
      Function::foreach (interval, count);
      BOOST_CHECK_EQUAL (cnt, 2);
      (*output) << std::endl;
    }

    {
      cnt = 0;
      Function::discreteInterval_t interval (2., 3., 0.1);
      Function::foreach (interval, count);
      BOOST_CHECK_EQUAL (cnt, 11);
      (*output) << std::endl;
    }

    {
      cnt = 0;
      Function::discreteInterval_t interval (0.8, 10.8, 1.);
      Function::foreach (interval, count);
      BOOST_CHECK_EQUAL (cnt, 11);
      (*output) << std::endl;
    }
  }

  std::cout << output->str () << std::endl;
  BOOST_CHECK (output->match_pattern ());
}

BOOST_AUTO_TEST_SUITE_END ()