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 171 172 173 174 175 176 177 178 179 180
|
// $Id$
//
// Copyright (C) 2004-2006 Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/test.h>
#include <iostream>
#include <math.h>
#include <RDGeneral/Invariant.h>
#include "BFGSOpt.h"
double circ_0_0(double *v) {
double dx = v[0];
double dy = v[1];
return dx * dx + dy * dy;
}
double circ_0_0_grad(double *v, double *grad) {
double dx = v[0];
double dy = v[1];
grad[0] = 2 * dx;
grad[1] = 2 * dy;
return 1.0;
}
double circ_1_0(double *v) {
double dx = v[0] - 1;
double dy = v[1];
return dx * dx + dy * dy;
}
double circ_1_0_grad(double *v, double *grad) {
double dx = v[0] - 1;
double dy = v[1];
grad[0] = 2 * dx;
grad[1] = 2 * dy;
return 1.0;
}
double func2(double *v) {
double weight = .5;
double dx = v[0] - 1;
double dy = v[1];
double term1 = dx * dx - dy * dy;
double term2 = dx * dx + dy * dy;
return term1 * term1 + weight * term2;
}
double grad2(double *v, double *grad) {
double weight = .5;
double dx = v[0] - 1;
double dy = v[1];
double term1 = dx * dx - dy * dy;
grad[0] = 4 * dx * term1 + 2 * weight * dx;
grad[1] = -4 * dy * term1 + 2 * weight * dy;
return 1.0;
}
void test1() {
std::cerr << "-------------------------------------" << std::endl;
std::cerr << "Testing linear search." << std::endl;
int dim = 2;
double oLoc[2], oVal;
double grad[2], dir[2];
double nLoc[2], nVal;
int resCode;
double (*func)(double *);
double (*gradFunc)(double *, double *);
func = circ_0_0;
gradFunc = circ_0_0_grad;
oLoc[0] = 0;
oLoc[1] = 1.0;
oVal = func(oLoc);
TEST_ASSERT(fabs(oVal - 1.0) < 1e-4);
gradFunc(oLoc, grad);
dir[0] = 0;
dir[1] = -.5;
BFGSOpt::linearSearch(dim, oLoc, oVal, grad, dir, nLoc, nVal, func, 0.5,
resCode);
TEST_ASSERT(resCode == 0);
TEST_ASSERT(fabs(nVal - 0.25) < 1e-4);
TEST_ASSERT(fabs(nLoc[0]) < 1e-4);
TEST_ASSERT(fabs(nLoc[1] - 0.5) < 1e-4);
oLoc[0] = 1.0;
oLoc[1] = 1.0;
oVal = func(oLoc);
TEST_ASSERT(fabs(oVal - 2.0) < 1e-4);
gradFunc(oLoc, grad);
dir[0] = -.5;
dir[1] = -.5;
BFGSOpt::linearSearch(dim, oLoc, oVal, grad, dir, nLoc, nVal, func, 1.0,
resCode);
TEST_ASSERT(resCode == 0);
TEST_ASSERT(fabs(nVal - 0.5) < 1e-4);
TEST_ASSERT(fabs(nLoc[0] - 0.5) < 1e-4);
TEST_ASSERT(fabs(nLoc[1] - 0.5) < 1e-4);
// we go hugely too far, but the dir gets cut in half, so we
// immediately hit the minimum
func = circ_0_0;
oLoc[0] = 0;
oLoc[1] = 1.0;
oVal = func(oLoc);
TEST_ASSERT(fabs(oVal - 1.0) < 1e-4);
gradFunc(oLoc, grad);
dir[0] = 0;
dir[1] = -2;
BFGSOpt::linearSearch(dim, oLoc, oVal, grad, dir, nLoc, nVal, func, 2.0,
resCode);
TEST_ASSERT(resCode == 0);
TEST_ASSERT(fabs(nVal) < 1e-4);
TEST_ASSERT(fabs(nLoc[0]) < 1e-4);
TEST_ASSERT(fabs(nLoc[1]) < 1e-4);
std::cerr << " done" << std::endl;
}
void test2() {
std::cerr << "-------------------------------------" << std::endl;
std::cerr << "Testing BFGS optimization." << std::endl;
unsigned int dim = 2;
double oLoc[2], oVal;
double nVal;
unsigned int nIters;
double (*func)(double *);
double (*gradFunc)(double *, double *);
func = circ_0_0;
gradFunc = circ_0_0_grad;
oLoc[0] = 0;
oLoc[1] = 1.0;
oVal = func(oLoc);
TEST_ASSERT(fabs(oVal - 1.0) < 1e-4);
BFGSOpt::minimize(dim, oLoc, 1e-4, nIters, nVal, func, gradFunc);
TEST_ASSERT(nIters = 1);
TEST_ASSERT(fabs(nVal) < 1e-4);
TEST_ASSERT(fabs(oLoc[0]) < 1e-4);
TEST_ASSERT(fabs(oLoc[1]) < 1e-4);
func = func2;
gradFunc = grad2;
oLoc[0] = 2.0;
oLoc[1] = 0.5;
BFGSOpt::minimize(dim, oLoc, 1e-4, nIters, nVal, func, gradFunc);
// TEST_ASSERT(nIters=1);
TEST_ASSERT(fabs(nVal) < 1e-4);
TEST_ASSERT(fabs(oLoc[0] - 1) < 1e-3);
TEST_ASSERT(fabs(oLoc[1]) < 1e-3);
// up the tolerance:
oLoc[0] = 2.0;
oLoc[1] = 0.5;
BFGSOpt::minimize(dim, oLoc, 1e-4, nIters, nVal, func, gradFunc, 1e-8);
// TEST_ASSERT(nIters=1);
TEST_ASSERT(fabs(nVal) < 1e-4);
TEST_ASSERT(fabs(oLoc[0] - 1) < 1e-4);
TEST_ASSERT(fabs(oLoc[1]) < 1e-4);
std::cerr << " done" << std::endl;
}
int main() {
test1();
test2();
}
|