File: otbQuadraticallyConstrainedSimpleSolver.hxx

package info (click to toggle)
otb 7.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,005,476 kB
  • sloc: cpp: 270,143; xml: 128,722; ansic: 4,367; sh: 1,768; python: 1,084; perl: 92; makefile: 72
file content (316 lines) | stat: -rw-r--r-- 9,511 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (C) 1999-2011 Insight Software Consortium
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 * Copyright (C) 2016-2019 IRSTEA
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef QuadraticallyConstrainedSimpleSolver_hxx
#define QuadraticallyConstrainedSimpleSolver_hxx

#include "otbQuadraticallyConstrainedSimpleSolver.h"

namespace otb
{

template <class ValueType>
QuadraticallyConstrainedSimpleSolver<ValueType>::QuadraticallyConstrainedSimpleSolver()
{
  m_WeightOfStandardDeviationTerm = 1.0;
  oft                             = Cost_Function_rmse;
}

template <class ValueType>
QuadraticallyConstrainedSimpleSolver<ValueType>::~QuadraticallyConstrainedSimpleSolver()
{
}

/*
 * Used to check layout topology consistency (Deep First Search)
 *
 * "m_AreaInOverlaps[i][j]>0" is equivalent to "images i and j are
 * overlapping with a non empty intersection (i.e. non null data)"
 */
template <class ValueType>
void QuadraticallyConstrainedSimpleSolver<ValueType>::DFS(std::vector<bool>& marked, unsigned int s) const
{

  // mark the s vertex
  marked[s] = true;

  // Get neighborhood
  for (unsigned int i = 0; i < m_AreaInOverlaps.rows(); i++)
  {
    if (s != i && m_AreaInOverlaps[s][i] > 0 && !marked[i])
    {
      DFS(marked, i);
    }
  }
}

/*
 * Check input matrices dimensions, and layout consistency
 */
template <class ValueType>
void QuadraticallyConstrainedSimpleSolver<ValueType>::CheckInputs() const
{

  // Check area matrix is non empty
  const unsigned int n = m_AreaInOverlaps.cols();
  if (n == 0)
  {
    itkExceptionMacro(<< "Input area matrix has 0 elements");
  }

  bool inputMatricesAreValid = true;

  // Check "areas" and "means" matrices size
  if ((n != m_AreaInOverlaps.rows()) || (n != m_MeanInOverlaps.cols()) || (n != m_MeanInOverlaps.rows()))
  {
    inputMatricesAreValid = false;
  }

  // Check "std" matrix size
  if ((oft == Cost_Function_musig) || (oft == Cost_Function_weighted_musig) || (oft == Cost_Function_rmse))
  {
    if ((n != m_StandardDeviationInOverlaps.cols()) || (n != m_StandardDeviationInOverlaps.rows()))
    {
      inputMatricesAreValid = false;
    }
  }

  // Check "means of products" matrix size
  if (oft == Cost_Function_musig)
  {
    if ((n != m_MeanOfProductsInOverlaps.cols()) || (n != m_MeanOfProductsInOverlaps.rows()))
    {
      inputMatricesAreValid = false;
    }
  }

  if (!inputMatricesAreValid)
  {
    itkExceptionMacro(<< "Input matrices must be square and have the same number of elements.");
  }
}

/*
 * Compute the objective function
 *
 * VNL is not sufficient: it has weak solving routines, and can not deal with QP subject to
 * a linear equality constraint plus lower limits (that is < and = linear constraints)
 * With vnl, we keep it simple and solve only the zero-y intercept linear case
 * but sometimes it fails because numerical instabilities. (vnl quadratic routines are not very reliable)
 *
 * With a good quadratic solver, we could e.g. use a general linear model (Xout = Xin*a+b)
 * Unfortunately it can be done with VNL so far. Tested & implemented successfully with
 * OOQP (Fastest, o(n)) and Quadprog++ (Fast, o(n)), and CGAL exact type solver (very slow, o(n^a) with a>1)
 * but has to rely on external dependencies...
 *
 */
template <class ValueType>
const typename QuadraticallyConstrainedSimpleSolver<ValueType>::DoubleMatrixType
QuadraticallyConstrainedSimpleSolver<ValueType>::GetQuadraticObjectiveMatrix(const DoubleMatrixType& areas, const DoubleMatrixType& means,
                                                                             const DoubleMatrixType& stds, const DoubleMatrixType& mops)
{
  // Set STD matrix weight
  ValueType w;

  if (oft == Cost_Function_mu)
  {
    w = 0.0;
  }
  if (oft == Cost_Function_musig)
  {
    w = 1.0;
  }
  if (oft == Cost_Function_weighted_musig)
  {
    w = (ValueType)m_WeightOfStandardDeviationTerm;
  }

  const unsigned int n = areas.cols();

  // Temporary matrices H, K, L
  DoubleMatrixType H(n, n, 0), K(n, n, 0), L(n, n, 0);
  DoubleMatrixType H_RMSE(n, n, 0);
  for (unsigned int i = 0; i < n; i++)
  {
    for (unsigned int j = 0; j < n; j++)
    {
      if (i == j)
      {
        // Diag (i=j)
        for (unsigned int k = 0; k < n; k++)
        {
          if (i != k)
          {
            H[i][j] += areas[i][k] * (means[i][k] * means[i][k] + w * stds[i][k] * stds[i][k]);
            K[i][j] += areas[i][k] * means[i][k];
            L[i][j] += areas[i][k];
            H_RMSE[i][j] += areas[i][k] * (means[i][k] * means[i][k] + stds[i][k] * stds[i][k]);
          }
        }
      }
      else
      {
        // Other than diag (i!=j)
        H[i][j]      = -areas[i][j] * (means[i][j] * means[j][i] + w * stds[i][j] * stds[j][i]);
        K[i][j]      = -areas[i][j] * means[i][j];
        L[i][j]      = -areas[i][j];
        H_RMSE[i][j] = -areas[i][j] * mops[i][j];
      }
    }
  }

  if (oft == Cost_Function_rmse)
  {
    H = H_RMSE;
  }

  return H;
}

/*
 * Returns the sub-matrix of mat, composed only by rows/cols in idx
 */
template <class ValueType>
const typename QuadraticallyConstrainedSimpleSolver<ValueType>::DoubleMatrixType
QuadraticallyConstrainedSimpleSolver<ValueType>::ExtractMatrix(const RealMatrixType& mat, const ListIndexType& idx)
{
  unsigned int     n = idx.size();
  DoubleMatrixType newMat(n, n, 0);
  for (unsigned int i = 0; i < n; i++)
  {
    for (unsigned int j = 0; j < n; j++)
    {
      unsigned int mat_i = idx[i];
      unsigned int mat_j = idx[j];
      newMat[i][j]       = mat[mat_i][mat_j];
    }
  }
  return newMat;
}

/*
 * QP Solving using vnl
 */
template <class ValueType>
void QuadraticallyConstrainedSimpleSolver<ValueType>::Solve()
{
  // Check matrices dimensions
  CheckInputs();

  // Display a warning if overlap matrix is null
  if (m_AreaInOverlaps.max_value() == 0)
  {
    itkExceptionMacro("No overlap in images!");
  }

  // Identify the connected components
  unsigned int               nbOfComponents = m_AreaInOverlaps.rows();
  unsigned int               nextVertex     = 0;
  std::vector<ListIndexType> connectedComponentsIndices;
  std::vector<bool>          markedVertices(nbOfComponents, false);
  bool                       allMarked = false;
  while (!allMarked)
  {
    // Depth First Search starting from nextVertex
    std::vector<bool> marked(nbOfComponents, false);
    DFS(marked, nextVertex);

    // Id the connected component
    ListIndexType list;
    for (unsigned int i = 0; i < nbOfComponents; i++)
    {
      if (marked[i])
      {
        // Tag the connected component index
        list.push_back(i);
        markedVertices[i] = true;
      }
      else if (!markedVertices[i])
      {
        // if the i-th vertex is not marked, DFS will start from it next
        nextVertex = i;
        break;
      }
    }
    connectedComponentsIndices.push_back(list);

    // Check if vertices are all marked
    allMarked = true;
    for (unsigned int i = 0; i < nbOfComponents; i++)
      if (!markedVertices[i])
        allMarked = false;
  }

  // Prepare output model
  m_OutputCorrectionModel.set_size(nbOfComponents);
  m_OutputCorrectionModel.fill(itk::NumericTraits<ValueType>::One);

  // Extract and solve all connected components one by one
  if (connectedComponentsIndices.size() > 1)
    itkWarningMacro("Seems to be more that one group of overlapping images. Trying to harmonize groups separately.");

  for (unsigned int component = 0; component < connectedComponentsIndices.size(); component++)
  {
    // Indices list
    ListIndexType      list = connectedComponentsIndices[component];
    const unsigned int n    = list.size();

    // Extract matrices
    DoubleMatrixType sub_areas = ExtractMatrix(m_AreaInOverlaps, list);
    DoubleMatrixType sub_means = ExtractMatrix(m_MeanInOverlaps, list);
    DoubleMatrixType sub_stdev = ExtractMatrix(m_StandardDeviationInOverlaps, list);
    DoubleMatrixType sub_mOfPr = ExtractMatrix(m_MeanOfProductsInOverlaps, list);

    // Objective function
    DoubleMatrixType Q = GetQuadraticObjectiveMatrix(sub_areas, sub_means, sub_stdev, sub_mOfPr);
    DoubleVectorType g(n, 0);

    // Constraint (Energy conservation)
    DoubleMatrixType A(1, n);
    DoubleVectorType b(1, 0);
    for (unsigned int i = 0; i < n; i++)
    {
      double energy = sub_areas[i][i] * sub_means[i][i];
      b[0] += energy;
      A[0][i] = energy;
    }

    // Solution
    DoubleVectorType x(n, 1);

    // Change tol. to 0.01 is a quick hack to avoid numerical instability...
    bool solv = vnl_solve_qp_with_non_neg_constraints(Q, g, A, b, x, 0.01);
    if (solv)
    {
      for (unsigned int i = 0; i < n; i++)
      {
        m_OutputCorrectionModel[list[i]] = static_cast<double>(x[i]);
      }
    }
    else
    {
      itkWarningMacro("vnl_solve_qp_with_non_neg_constraints failed for component #" << component);
    }
  }
}
}

#endif