Skip to content
Snippets Groups Projects
Commit 1ee8f2a4 authored by Elias Pipping's avatar Elias Pipping Committed by Elias Pipping
Browse files

Have minimise() make multiple steps

parent 9680be4d
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ template <class MyConvexProblemTypeTEMPLATE> class MyBlockProblem { ...@@ -32,6 +32,7 @@ template <class MyConvexProblemTypeTEMPLATE> class MyBlockProblem {
class IterateObject; class IterateObject;
MyBlockProblem(MyConvexProblemType& problem) : problem(problem) { MyBlockProblem(MyConvexProblemType& problem) : problem(problem) {
// TODO: Is it clever to create a bisection here?
bisection = Bisection(0.0, 1.0, 1e-15, true, 1e-14); bisection = Bisection(0.0, 1.0, 1e-15, true, 1e-14);
}; };
...@@ -118,10 +119,7 @@ class MyBlockProblem<MyConvexProblemTypeTEMPLATE>::IterateObject { ...@@ -118,10 +119,7 @@ class MyBlockProblem<MyConvexProblemTypeTEMPLATE>::IterateObject {
Dune::SampleFunctional<block_size> localJ(*localA, localb, phi); Dune::SampleFunctional<block_size> localJ(*localA, localb, phi);
LocalVectorType correction; LocalVectorType correction;
for (size_t i = 1; i <= 10; ++i) { // FIXME: hardcoded value Dune::minimise(localJ, ui, 10); // FIXME: hardcoded value
Dune::minimise(localJ, ui, correction);
ui += correction;
}
} }
} }
......
...@@ -118,8 +118,8 @@ template <int dimension> class SampleFunctional { ...@@ -118,8 +118,8 @@ template <int dimension> class SampleFunctional {
}; };
template <class Functional> template <class Functional>
void minimise(const Functional J, const typename Functional::SmallVector x, void minimise(const Functional J, typename Functional::SmallVector &x,
typename Functional::SmallVector &corr, size_t steps = 1,
Bisection const &bisection = Bisection const &bisection =
Bisection(0.0, // acceptError: Stop if the search interval has Bisection(0.0, // acceptError: Stop if the search interval has
// become smaller than this number // become smaller than this number
...@@ -130,13 +130,13 @@ void minimise(const Functional J, const typename Functional::SmallVector x, ...@@ -130,13 +130,13 @@ void minimise(const Functional J, const typename Functional::SmallVector x,
// minimization // minimization
{ {
typedef typename Functional::SmallVector SmallVector; typedef typename Functional::SmallVector SmallVector;
for (size_t step = 0; step < steps; ++step) {
SmallVector descDir; SmallVector descDir;
J.descentDirection(x, descDir); J.descentDirection(x, descDir);
if (descDir == SmallVector(0.0)) { if (descDir == SmallVector(0.0))
corr = SmallVector(0.0);
return; return;
}
// {{{ Construct a restriction of J to the line x + t * descDir // {{{ Construct a restriction of J to the line x + t * descDir
...@@ -167,22 +167,23 @@ void minimise(const Functional J, const typename Functional::SmallVector x, ...@@ -167,22 +167,23 @@ void minimise(const Functional J, const typename Functional::SmallVector x,
Interval<double> D; Interval<double> D;
JRest.subDiff(0, D); JRest.subDiff(0, D);
dverb dverb << "## Directional derivative (as per subdifferential of "
<< "## Directional derivative (as per subdifferential of restriction): " "restriction): " << D[1] << " (coordinates of the restriction)"
<< D[1] << " (coordinates of the restriction)" << std::endl; << std::endl;
assert(D[1] <= assert(D[1] <=
0); // We should not be minimising in this direction otherwise 0); // We should not be minimising in this direction otherwise
} }
int count; int count;
// FIXME: The value of x_old should not matter if the factor is 1.0, correct? // FIXME: The value of x_old should not matter if the factor is 1.0,
// correct?
double const stepsize = bisection.minimize(JRest, 0.0, 1.0, count); double const stepsize = bisection.minimize(JRest, 0.0, 1.0, count);
dverb << "Number of iterations in the bisection method: " << count dverb << "Number of iterations in the bisection method: " << count
<< std::endl; << std::endl;
; ;
corr = descDir; x.axpy(stepsize, descDir);
corr *= stepsize; }
} }
} }
#endif #endif
...@@ -15,14 +15,8 @@ template <int dim> ...@@ -15,14 +15,8 @@ template <int dim>
double functionTester(Dune::SampleFunctional<dim> J, double functionTester(Dune::SampleFunctional<dim> J,
typename Dune::SampleFunctional<dim>::SmallVector &start, typename Dune::SampleFunctional<dim>::SmallVector &start,
size_t runs) { size_t runs) {
typename Dune::SampleFunctional<dim>::SmallVector correction;
std::cout << "Old value: J(...) = " << J(start) << std::endl; std::cout << "Old value: J(...) = " << J(start) << std::endl;
for (size_t i = 1; i <= runs; ++i) { Dune::minimise(J, start, runs);
Dune::minimise(J, start, correction);
start += correction;
if (i != runs)
std::cout << "New value: J(...) = " << J(start) << std::endl;
}
double const final = J(start); double const final = J(start);
std::cout << "Final value J(...) = " << final << std::endl; std::cout << "Final value J(...) = " << final << std::endl;
return final; return final;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment