File: Wm5MinimizeN.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (215 lines) | stat: -rw-r--r-- 6,479 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2011/03/09)

#include "Wm5MathematicsPCH.h"
#include "Wm5MinimizeN.h"
#include "Wm5Math.h"
#include "Wm5Memory.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
MinimizeN<Real>::MinimizeN (int dimensions, Function function,
    int maxLevel, int maxBracket, int maxIterations, void* userData)
    :
    mDimensions(dimensions),
    mFunction(function),
    mMaxIterations(maxIterations),
    mUserData(userData),
    mMinimizer(LineFunction, maxLevel, maxBracket)
{
    assertion(mDimensions >= 1 && mFunction, "Invalid inputs\n");

    mTCurr = new1<Real>(mDimensions);
    mTSave = new1<Real>(mDimensions);
    mDirectionStorage = new1<Real>(mDimensions*(mDimensions + 1));
    mDirection = new1<Real*>(mDimensions + 1);
    for (int i = 0; i <= mDimensions; ++i)
    {
        mDirection[i] = &mDirectionStorage[i*mDimensions];
    }
    mDConj = mDirection[mDimensions];

    mLineArg = new1<Real>(mDimensions);
}
//----------------------------------------------------------------------------
template <typename Real>
MinimizeN<Real>::~MinimizeN ()
{
    delete1(mTCurr);
    delete1(mTSave);
    delete1(mDirectionStorage);
    delete1(mDirection);
    delete1(mLineArg);
}
//----------------------------------------------------------------------------
template <typename Real>
void MinimizeN<Real>::GetMinimum (const Real* t0, const Real* t1,
    const Real* tInitial, Real* tMin, Real& fMin)
{
    // For 1D function callback.
    mMinimizer.SetUserData(this);

    // The initial guess.
    size_t numBytes = mDimensions*sizeof(Real);
    mFCurr = mFunction(tInitial, mUserData);
    memcpy(mTSave, tInitial, numBytes);
    memcpy(mTCurr, tInitial, numBytes);

    // Initialize the direction set to the standard Euclidean basis.
    size_t numBasisBytes = numBytes*(mDimensions + 1);
    memset(mDirectionStorage, 0, numBasisBytes);
    int i;
    for (i = 0; i < mDimensions; ++i)
    {
        mDirection[i][i] = (Real)1;
    }

    Real ell0, ell1, ellMin;
    for (int iter = 0; iter < mMaxIterations; iter++)
    {
        // Find minimum in each direction and update current location.
        for (i = 0; i < mDimensions; ++i)
        {
            mDCurr = mDirection[i];
            ComputeDomain(t0, t1, ell0, ell1);
            mMinimizer.GetMinimum(ell0, ell1, (Real)0, ellMin, mFCurr);
            for (int j = 0; j < mDimensions; ++j)
            {
                mTCurr[j] += ellMin*mDCurr[j];
            }
        }

        // Estimate a unit-length conjugate direction.
        Real length = (Real)0;
        for (i = 0; i < mDimensions; ++i)
        {
            mDConj[i] = mTCurr[i] - mTSave[i];
            length += mDConj[i]*mDConj[i];
        }

        const Real epsilon = (Real)1e-06;
        length = Math<Real>::Sqrt(length);
        if (length < epsilon)
        {
            // New position did not change significantly from old one.
            // Should there be a better convergence criterion here?
            break;
        }

        Real invlength = ((Real)1)/length;
        for (i = 0; i < mDimensions; ++i)
        {
            mDConj[i] *= invlength;
        }

        // Minimize in conjugate direction.
        mDCurr = mDConj;
        ComputeDomain(t0, t1, ell0, ell1);
        mMinimizer.GetMinimum(ell0, ell1, (Real)0, ellMin, mFCurr);
        for (i = 0; i < mDimensions; ++i)
        {
            mTCurr[i] += ellMin*mDCurr[i];
        }

        // Cycle the directions and add conjugate direction to set.
        mDConj = mDirection[0];
        for (i = 0; i < mDimensions; ++i)
        {
            mDirection[i] = mDirection[i+1];
        }

        // Set parameters for next pass.
        memcpy(mTSave, mTCurr, numBytes);
    }

    memcpy(tMin, mTCurr, numBytes);
    fMin = mFCurr;
}
//----------------------------------------------------------------------------
template <typename Real>
void MinimizeN<Real>::ComputeDomain (const Real* t0, const Real* t1,
    Real& ell0, Real& ell1)
{
    ell0 = -Math<Real>::MAX_REAL;
    ell1 = +Math<Real>::MAX_REAL;

    for (int i = 0; i < mDimensions; ++i)
    {
        Real b0 = t0[i] - mTCurr[i];
        Real b1 = t1[i] - mTCurr[i];
        Real inv;
        if (mDCurr[i] > (Real)0)
        {
            // The valid t-interval is [b0,b1].
            inv = ((Real)1)/mDCurr[i];
            b0 *= inv;
            if (b0 > ell0)
            {
                ell0 = b0;
            }
            b1 *= inv;
            if (b1 < ell1)
            {
                ell1 = b1;
            }
        }
        else if (mDCurr[i] < (Real)0)
        {
            // The valid t-interval is [b1,b0].
            inv = ((Real)1)/mDCurr[i];
            b0 *= inv;
            if (b0 < ell1)
            {
                ell1 = b0;
            }
            b1 *= inv;
            if (b1 > ell0)
            {
                ell0 = b1;
            }
        }
    }

    // Correction if numerical errors lead to values nearly zero.
    if (ell0 > (Real)0)
    {
        ell0 = (Real)0;
    }
    if (ell1 < (Real)0)
    {
        ell1 = (Real)0;
    }
}
//----------------------------------------------------------------------------
template <typename Real>
Real MinimizeN<Real>::LineFunction (Real t, void* userData)
{
    MinimizeN& self = *(MinimizeN*)userData;

    for (int i = 0; i < self.mDimensions; ++i)
    {
        self.mLineArg[i] = self.mTCurr[i] + t*self.mDCurr[i];
    }

    Real result = self.mFunction(self.mLineArg, self.mUserData);
    return result;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class MinimizeN<float>;

template WM5_MATHEMATICS_ITEM
class MinimizeN<double>;
//----------------------------------------------------------------------------
}