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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/* $Id: $
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's offical duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================*/
/*****************************************************************************
File name: njn_dynprogprob.cpp
Author: John Spouge
Contents:
******************************************************************************/
#include <assert.h>
#include "njn_dynprogprob.hpp"
#include "njn_memutil.hpp"
using namespace Njn;
const size_t DynProgProb::ARRAY_CAPACITY = 256;
void DynProgProb::init (size_t arrayCapacity_) // range for d_array_p [0,1][0...arrayCapacity_ - 1]
{
if (arrayCapacity_ > 0)
{
for (size_t i = 0; i < 2; i++)
{
d_array_p [i] = new double [arrayCapacity_];
MemUtil::memZero (d_array_p [i], sizeof (double) * arrayCapacity_);
}
}
d_arrayCapacity = arrayCapacity_;
}
void DynProgProb::free2 ()
{
if (getArrayCapacity () > 0)
{
for (size_t i = 0; i < 2; i++)
{
delete [] d_array_p [i]; d_array_p [i] = 0;
}
}
d_arrayCapacity = 0;
}
void DynProgProb::clear (
long int valueBegin_, // lower limit for long int values in the array (an offset)
size_t arrayCapacity_) // new array capacity
// resets the computation
{
free2 ();
init (arrayCapacity_);
d_valueBegin = valueBegin_;
d_step = 0;
}
void DynProgProb::clear ( // initializes the "probabilities" with non-negative weights
long int valueLower_, // lower long int value corresponding to the "probability" array
long int valueUpper_, // one beyond present upper long int value corresponding to the "probability" array
const double *prob_) // "probabilities" prob [valueLower_, valueUpper_) corresponding to the long ints
{
assert ((! prob_ && valueLower_ <= 0 && 0 <= valueUpper_) ||
/*sls added "("*/(prob_ && valueLower_ < valueUpper_)/*sls added ")"*/ );
if (prob_)
{
for (size_t i = 0; i < static_cast <size_t> (valueUpper_ - valueLower_); i++)
{
assert (0.0 <= prob_ [i]);
}
clear (valueLower_, static_cast <size_t> (valueUpper_ - valueLower_));
d_valueLower = valueLower_;
d_valueUpper = valueUpper_;
MemUtil::memCpy (d_array_p [0], prob_, sizeof (double) * getArrayCapacity ());
return;
}
if (valueLower_ == 0 && valueUpper_ == 0)
{
clear (-static_cast <long int> (ARRAY_CAPACITY / 2) + 1, ARRAY_CAPACITY);
}
else
{
clear (valueLower_, static_cast <size_t> (valueUpper_ - valueLower_));
}
d_valueLower = 0;
d_valueUpper = 1;
d_array_p [0][getArrayPos (0)] = 1.0;
return;
}
void DynProgProb::copy (
size_t step_, // current index : starts at 0
const double *const *array_, // two corresponding arrays of probabilities
size_t arrayCapacity_, // present capacity of the array
long int valueBegin_, // lower limit for long int values in the array (an offset)
long int valueLower_, // present lower long int value in the array
long int valueUpper_, // one beyond present upper long int value in the array
ValueFct *newStateFct_, // function for updating dynamic programming values
size_t dimInputProb_,
const double *inputProb_) // array of input states : d_inputProb_p [0...dimStateProb - 1]
{
if (arrayCapacity_ != getArrayCapacity ())
{
free2 ();
init (arrayCapacity_);
}
d_step = step_;
for (size_t i = 0; i < 2; i++)
{
if (getArrayCapacity () > 0) MemUtil::memCpy (d_array_p [i], array_ [i], sizeof (double) * getArrayCapacity ());
}
d_valueBegin = valueBegin_;
d_valueLower = valueLower_;
d_valueUpper = valueUpper_;
setValueFct (newStateFct_);
setInput (dimInputProb_, inputProb_);
}
void DynProgProb::initInput (size_t dimInputProb_) // array of input states : d_inputProb_p [0...dimStateProb - 1]
{
if (dimInputProb_ > 0)
{
d_inputProb_p = new double [dimInputProb_];
MemUtil::memZero (d_inputProb_p, sizeof (double) * dimInputProb_);
}
d_dimInputProb = dimInputProb_;
}
void DynProgProb::freeInput ()
{
if (getDimInputProb () > 0)
{
delete [] d_inputProb_p; d_inputProb_p = 0;
}
d_dimInputProb = 0;
}
void DynProgProb::setInput (
size_t dimInputProb_,
const double *inputProb_) // array of input states : d_inputProb_p [0...dimStateProb - 1]
{
if (dimInputProb_ != getDimInputProb ())
{
freeInput ();
initInput (dimInputProb_);
}
if (getDimInputProb () > 0) MemUtil::memCpy (d_inputProb_p, inputProb_, sizeof (double) * getDimInputProb ());
}
void DynProgProb::update () // updates dynamic prog probs
{
assert (getValueFct ());
assert (getDimInputProb ());
assert (getInputProb ());
const size_t ARRAY_FAC = 2;
assert (1 < ARRAY_FAC);
long int i = 0;
size_t j = 0;
const double *oldArray = 0;
double *array = 0;
long int value = 0;
long int valueBegin = 0;
long int valueLower = 0;
long int valueUpper = 0;
/*sls deleted size_t arrayPos = 0;*/
oldArray = d_array_p [d_step % 2];
array = d_array_p [(d_step + 1) % 2];
valueLower = LONG_MAX;
valueUpper = LONG_MIN;
MemUtil::memZero (array, sizeof (double) * getArrayCapacity ());
for (i = getValueLower (); i < getValueUpper (); i++)
{
if (oldArray [getArrayPos (i)] == 0.0) continue;
for (j = 0; j < getDimInputProb (); j++)
{
if (getInputProb () [j] == 0.0) continue;
// adjust the reserve, if necessary
value = getValueFct () (i, j);
while (value < getValueBegin () || getValueEnd () <= value) {
valueBegin = getValueBegin ();
if (value < getValueBegin ()) valueBegin -= (ARRAY_FAC - 1) * getArrayCapacity ();
reserve (ARRAY_FAC * getArrayCapacity ());
setValueBegin (valueBegin);
oldArray = d_array_p [d_step % 2];
array = d_array_p [(d_step + 1) % 2];
}
if (value < valueLower) valueLower = value;
if (valueUpper < value) valueUpper = value;
// add the probability
assert (getValueBegin () <= i);
assert (i < getValueEnd ());
array [getArrayPos (value)] += oldArray [getArrayPos (i)] * getInputProb () [j];
}
}
d_valueLower = valueLower;
d_valueUpper = valueUpper + 1;
d_step++;
}
void DynProgProb::reserve (size_t arrayCapacity_) // new array capacity
// increases capacity of and copies d_array_p, while updating other variables
{
assert (getArrayCapacity () < arrayCapacity_);
double *array = new double [getArrayCapacity ()];
for (size_t i = 0; i < 2; i++)
{
MemUtil::memCpy (array, d_array_p [i], sizeof (double) * getArrayCapacity ());
delete [] d_array_p [i]; d_array_p [i] = 0;
d_array_p [i] = new double [arrayCapacity_];
MemUtil::memZero (d_array_p [i], sizeof (double) * arrayCapacity_);
MemUtil::memCpy (d_array_p [i], array, sizeof (double) * getArrayCapacity ());
}
d_arrayCapacity = arrayCapacity_;
delete [] array; array = 0;
}
void DynProgProb::setValueBegin (long int valueBegin_)
// resets the offset d_valueBegin
// assert (valueBegin_ <= getValueBegin ()) : enlarge the array only
// assert (offSet < getArrayCapacity ());
{
assert (valueBegin_ <= getValueBegin ());
size_t offSet = static_cast <size_t> (getValueBegin () - valueBegin_);
if (offSet == 0) return; // nothing to do
assert (offSet < getArrayCapacity ());
double *array = new double [getArrayCapacity ()];
for (size_t i = 0; i < 2; i++)
{
MemUtil::memCpy (array, d_array_p [i], sizeof (double) * getArrayCapacity ());
MemUtil::memZero (d_array_p [i], sizeof (double) * getArrayCapacity ());
MemUtil::memCpy (d_array_p [i] + offSet, array, sizeof (double) * (getArrayCapacity () - offSet));
}
delete [] array; array = 0;
d_valueBegin = valueBegin_;
}
|