Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSimpleBarrierConstraintChecker.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include <algorithm>
16 
17 #include "mitkExceptionMacro.h"
18 
21 {
22  PenaltyArrayType result(m_Constraints.size());
23 
24  PenaltyArrayType::iterator penaltyPos = result.begin();
25 
26  for (ConstraintVectorType::const_iterator pos = m_Constraints.begin(); pos != m_Constraints.end();
27  ++pos, ++penaltyPos)
28  {
29  *penaltyPos = CalcPenalty(parameters, *pos);
30  }
31 
32  return result;
33 };
34 
36 {
37  return m_Constraints.size();
38 };
39 
42 {
43  return m_MaxConstraintPenalty;
44 };
45 
47  BarrierValueType barrier, BarrierWidthType width)
48 {
49  Constraint c;
51  params.push_back(parameterID);
52  c.parameters = params;
53  c.barrier = barrier;
54  c.width = width;
55  c.upperBarrier = false;
56 
57  m_Constraints.push_back(c);
58 };
59 
61  BarrierValueType barrier, BarrierWidthType width)
62 {
63  Constraint c;
65  params.push_back(parameterID);
66  c.parameters = params;
67  c.barrier = barrier;
68  c.width = width;
69  c.upperBarrier = true;
70 
71  m_Constraints.push_back(c);
72 };
73 
75  parameterIDs, BarrierValueType barrier, BarrierWidthType width)
76 {
77  Constraint c;
78 
79  c.parameters = parameterIDs;
80  c.barrier = barrier;
81  c.width = width;
82  c.upperBarrier = false;
83 
84  m_Constraints.push_back(c);
85 };
86 
88  parameterIDs, BarrierValueType barrier, BarrierWidthType width)
89 {
90  Constraint c;
91 
92  c.parameters = parameterIDs;
93  c.barrier = barrier;
94  c.width = width;
95  c.upperBarrier = true;
96 
97  m_Constraints.push_back(c);
98 };
99 
102 {
103  if (index >= GetNumberOfConstraints())
104  {
105  mitkThrow() <<
106  "Error. Cannot get constraint. Invalid constraint index passed.Parameter Invalid index:"
107  << index;
108  }
109 
110  return m_Constraints[index];
111 };
112 
115 {
116  if (index >= GetNumberOfConstraints())
117  {
118  mitkThrow() <<
119  "Error. Cannot get constraint. Invalid constraint index passed.Parameter Invalid index:"
120  << index;
121  }
122 
123  return m_Constraints[index];
124 };
125 
127 {
128  if (index >= GetNumberOfConstraints())
129  {
130  return;
131  }
132 
133  m_Constraints.erase(m_Constraints.begin() + index);
134 };
135 
136 
138 {
139  m_Constraints.clear();
140 };
141 
144  const Constraint& constraint) const
145 {
146  double constrainedValue = 0;
147 
148  for (ParameterIndexVectorType::const_iterator pos = constraint.parameters.begin();
149  pos != constraint.parameters.end(); ++pos)
150  {
151  if (*pos >= parameters.size())
152  {
153  mitkThrow() <<
154  "Error. Parameter specified by constraint is no part of the passed parameter values. Invalid parameter ID:"
155  << *pos;
156  }
157 
158  constrainedValue += parameters[*pos];
159  };
160 
161  PenaltyValueType result = 0;
162 
163  //transform into an inequality against 0.
164  //basic form of inequalities are constrainedValue (relation) constraint.barrier
165  double transformedTermValue = constraint.barrier - constrainedValue;
166 
167  if (!constraint.upperBarrier)
168  {
169  // need the form 0 <= term, but right now it would be 0 >= term, so *-1
170  transformedTermValue *= -1;
171  }
172 
173  double barrierSize = std::abs(static_cast<double>(constraint.width));
174 
175  //Check if it would be the maximum penalty anyway.
176  if (transformedTermValue <= 0)
177  {
178  result = this->m_MaxConstraintPenalty;
179  }
180  else if (transformedTermValue > barrierSize)
181  {
182  //Check if it is outside the barrier region
183  //the constrained value is outside the barrier region within a legal valid range.
184  result = 0;
185  }
186  else
187  {
188  // the constrained value seems to violate the constraint or at least is within the
189  // barrier region, so compute the penalty
190 
191  //calculate the barrier function value
192  double barrierValue = -1 * log(transformedTermValue / barrierSize);
193 
194  result = std::min(barrierValue, this->m_MaxConstraintPenalty);
195  }
196 
197  return result;
198 };
void SetUpperBarrier(ParameterIndexType parameterID, BarrierValueType barrier, BarrierWidthType width=0.0)
Superclass::PenaltyValueType PenaltyValueType
PenaltyValueType CalcPenalty(const ParametersType &parameters, const Constraint &constraint) const
std::vector< ParameterIndexType > ParameterIndexVectorType
Superclass::PenaltyArrayType PenaltyArrayType
void SetLowerSumBarrier(const ParameterIndexVectorType &parameterIDs, BarrierValueType barrier, BarrierWidthType width=0.0)
void SetLowerBarrier(ParameterIndexType parameterID, BarrierValueType barrier, BarrierWidthType width=0.0)
PenaltyArrayType GetPenalties(const ParametersType &parameters) const override
#define mitkThrow()
PenaltyValueType GetFailedConstraintValue() const override
static T min(T x, T y)
Definition: svm.cpp:53
void SetUpperSumBarrier(const ParameterIndexVectorType &parameterIDs, BarrierValueType barrier, BarrierWidthType width=0.0)
Superclass::ParametersType ParametersType