Merge branch 'unstable' of github.com:lteacy/maxsum-cpp · lteacy/maxsum-cpp@feff767 · GitHub
Skip to content

Commit feff767

Browse files
author
Luke Teacy
committed
Merge branch 'unstable' of github.com:lteacy/maxsum-cpp
2 parents 54ce8d9 + 9c51a26 commit feff767

5 files changed

Lines changed: 318 additions & 10 deletions

File tree

include/DiscreteFunction.h

Lines changed: 147 additions & 3 deletions

include/MaxSumController.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ namespace maxsum
4848
*/
4949
FactorMap factors_i;
5050

51+
/**
52+
* Map storing the total value for each factor (the factor + the sum of
53+
* all its input messages. This is used if we need to calculate
54+
* additional things, such as the second best joint action for each
55+
* factor (for VPI).
56+
*/
57+
FactorMap factorTotalValue_i;
58+
5159
/**
5260
* Type of container used to map (action) variables to their currently
5361
* assigned values.
@@ -145,6 +153,24 @@ namespace maxsum
145153
: maxIterations_i(maxIterations),
146154
maxNormThreshold_i(maxnorm) {}
147155

156+
/**
157+
* Returns the total value for a specified factor.
158+
* The total value is the factor plus the sum of all its received messages.
159+
* @pre the optimise method must be called first to properly initialise
160+
* this value.
161+
* @returns the total value for this factor.
162+
*/
163+
const DiscreteFunction& getTotalValue(FactorID fac) const
164+
{
165+
FactorMap::const_iterator pos = factorTotalValue_i.find(fac);
166+
if(factors_i.end()==pos)
167+
{
168+
throw new NoSuchElementException("MaxSumController::getFactor()",
169+
"No such factor in factor graph.");
170+
}
171+
return pos->second;
172+
}
173+
148174
/**
149175
* Accessor method for factor function.
150176
* @param[in] id the unique identifier of the desired factor.
@@ -284,6 +310,58 @@ namespace maxsum
284310
return pos->second;
285311
}
286312

313+
/**
314+
* Accessor method for (writable) reference to factor function.
315+
* This version returns a writable reference to a function to allow
316+
* efficient updates to the function values. Note this is an UNSAFE
317+
* procedure, because the MaxSumController will not be notified if
318+
* any change is made to function's domain through this handle, which
319+
* in turn will change the factor graph.
320+
*
321+
* In future, we may modify this function to unsure that no unexpected
322+
* domain changes are made, but for now, this allows us to bypass all
323+
* the redundant copying and checking that is done by
324+
* MaxSumController::setFactor. If you need a safe procedure use that
325+
* function instead.
326+
*
327+
* Also, if the factor is modified in anyway through the returned
328+
* reference, the MaxSumController must be notified by calling
329+
* MaxSumController::notifyFactorChange.
330+
*
331+
* @param[in] id the unique identifier of the desired factor.
332+
* @returns a reference to the function associated with the factor with
333+
* unique identifier <code>id</code>.
334+
* @throws maxsum::NoSuchElementException if the specified factor is not
335+
* known to this maxsum::MaxSumController.
336+
* @pre a factor with this id must already be in the factor graph.
337+
* @post the caller must ensure that the domain of the returned function
338+
* is not changed, and so promise only to modify its values. Any change
339+
* in value must be flagged to the MaxSumController by calling
340+
* MaxSumController::notifyFactorChange.
341+
*/
342+
DiscreteFunction& getUnSafeWritableFactorHandle(FactorID id)
343+
{
344+
FactorMap::iterator pos = factors_i.find(id);
345+
if(factors_i.end()==pos)
346+
{
347+
throw new NoSuchElementException("MaxSumController::getFactor()",
348+
"No such factor in factor graph.");
349+
}
350+
return pos->second;
351+
}
352+
353+
/**
354+
* Function used to notify this MaxSumController of any changes made
355+
* to a factor without its knowledge.
356+
* This function should be called after any change to a factor
357+
* made using MaxSumController::getUnSafeWritableFactorHandle
358+
* @param id the id of the changed factor
359+
*/
360+
void notifyFactor(FactorID id)
361+
{
362+
var2facMsgs_i.notify(id);
363+
}
364+
287365
/**
288366
* Returns true if and only if the specified variable is in the domain
289367
* of at least one of the factors managed by this maxsum::MaxSumController.

src/DiscreteFunction.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ ValType DiscreteFunction::min() const
462462
}
463463

464464
/**
465-
* Returns the linear index of the maximum value accross entire domain.
466-
*/
465+
* Returns the linear index of the maximum value accross entire domain.
466+
*/
467467
ValIndex DiscreteFunction::argmax() const
468468
{
469469
std::vector<ValType>::const_iterator pos =
@@ -472,6 +472,44 @@ ValIndex DiscreteFunction::argmax() const
472472
return result;
473473
}
474474

475+
/**
476+
* Returns the linear index of the 2nd largest value.
477+
* Example usage:
478+
* <p>
479+
* <code>
480+
* ValIndex mx1 = fun.argmax();
481+
* ValIndex mx2 = fun.argmax2(mx1);
482+
* </code>
483+
* </p>
484+
* @param mxInd the value returned by DiscreteFunction::argmax()
485+
* This is the maximum of the set of values, excluding the largest one,
486+
* returned by DiscreteFunction::argmax
487+
*/
488+
ValIndex DiscreteFunction::argmax2(const ValIndex mxInd) const
489+
{
490+
ValType mx2Val = -std::numeric_limits<ValType>::max();
491+
ValIndex mx2Ind = 0;
492+
493+
for(ValIndex it=0; it<domainSize(); ++it)
494+
{
495+
if(mxInd==it) // skip max to find largest of rest.
496+
{
497+
continue;
498+
}
499+
500+
ValType curVal = at(it);
501+
if(curVal>mx2Val)
502+
{
503+
mx2Val = curVal;
504+
mx2Ind = it;
505+
}
506+
507+
} // for loop
508+
509+
return mx2Ind;
510+
511+
} // argmax2
512+
475513
/**
476514
* Returns the maxnorm for this function.
477515
* The maxnorm is defined as the maximum absolute value of the function:

src/MaxSumController.cpp

Lines changed: 4 additions & 5 deletions

0 commit comments

Comments
 (0)