ipopt-src 0.2.3+3.14.16

Redistribution of Coin-OR Ipopt as a crate
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (C) 2004, 2009 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#include "IpExpansionMatrix.hpp"
#include "IpDenseVector.hpp"

namespace Ipopt
{

#if IPOPT_VERBOSITY > 0
static const Index dbg_verbosity = 0;
#endif

ExpansionMatrix::ExpansionMatrix(
   const ExpansionMatrixSpace* owner_space
)
   : Matrix(owner_space),
     owner_space_(owner_space)
{ }

ExpansionMatrix::~ExpansionMatrix()
{ }

void ExpansionMatrix::MultVectorImpl(
   Number        alpha,
   const Vector& x,
   Number        beta,
   Vector&       y
) const
{
   //  A few sanity checks
   DBG_ASSERT(NCols() == x.Dim());
   DBG_ASSERT(NRows() == y.Dim());

   // Take care of the y part of the addition
   if( beta != 0.0 )
   {
      y.Scal(beta);
   }
   else
   {
      y.Set(0.0);  // In case y hasn't been initialized yet
   }

   // See if we can understand the data
   const DenseVector* dense_x = static_cast<const DenseVector*>(&x);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&x));
   DenseVector* dense_y = static_cast<DenseVector*>(&y);
   DBG_ASSERT(dynamic_cast<DenseVector*>(&y));

   const Index* exp_pos = ExpandedPosIndices();

   if( dense_x && dense_y )
   {
      Number* yvals = dense_y->Values();
      if( dense_x->IsHomogeneous() )
      {
         Number val = alpha * dense_x->Scalar();
         if( val != 0. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[exp_pos[i]] += val;
            }
         }
      }
      else
      {
         const Number* xvals = dense_x->Values();
         if( alpha == 1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[exp_pos[i]] += xvals[i];
            }
         }
         else if( alpha == -1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[exp_pos[i]] -= xvals[i];
            }
         }
         else
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[exp_pos[i]] += alpha * xvals[i];
            }
         }
      }
   }
}

void ExpansionMatrix::TransMultVectorImpl(
   Number        alpha,
   const Vector& x,
   Number        beta,
   Vector&       y
) const
{
   //  A few sanity checks
   DBG_ASSERT(NCols() == y.Dim());
   DBG_ASSERT(NRows() == x.Dim());

   // Take care of the y part of the addition
   if( beta != 0.0 )
   {
      y.Scal(beta);
   }
   else
   {
      y.Set(0.0);  // In case y hasn't been initialized yet
   }

   // See if we can understand the data
   const DenseVector* dense_x = static_cast<const DenseVector*>(&x);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&x));
   DenseVector* dense_y = static_cast<DenseVector*>(&y);
   DBG_ASSERT(dynamic_cast<DenseVector*>(&y));

   const Index* exp_pos = ExpandedPosIndices();

   if( dense_x && dense_y )
   {
      Number* yvals = dense_y->Values();
      if( dense_x->IsHomogeneous() )
      {
         Number val = alpha * dense_x->Scalar();
         if( val != 0. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[i] += val;
            }
         }
      }
      else
      {
         const Number* xvals = dense_x->Values();
         if( alpha == 1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[i] += xvals[exp_pos[i]];
            }
         }
         else if( alpha == -1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[i] -= xvals[exp_pos[i]];
            }
         }
         else
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               yvals[i] += alpha * xvals[exp_pos[i]];
            }
         }
      }
   }
}

// Specialized method (overloaded from IpMatrix)
void ExpansionMatrix::AddMSinvZImpl(
   Number        alpha,
   const Vector& S,
   const Vector& Z,
   Vector&       X
) const
{
   DBG_ASSERT(NCols() == S.Dim());
   DBG_ASSERT(NCols() == Z.Dim());
   DBG_ASSERT(NRows() == X.Dim());

   const DenseVector* dense_S = static_cast<const DenseVector*>(&S);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&S));
   const DenseVector* dense_Z = static_cast<const DenseVector*>(&Z);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&Z));
   DenseVector* dense_X = static_cast<DenseVector*>(&X);
   DBG_ASSERT(dynamic_cast<DenseVector*>(&X));

   // if vector S is homogeneous type, call the default implementation
   // ToDo: find out how often the default implementation is called and
   // if we should implement specialized method for the homogenenous
   // case
   if( dense_S->IsHomogeneous() )
   {
      DBG_ASSERT(false && "dense_S is homogeneous - implement specialized option?");
      Matrix::AddMSinvZImpl(alpha, S, Z, X);
      return;
   }

   const Index* exp_pos = ExpandedPosIndices();
   const Number* vals_S = dense_S->Values();
   Number* vals_X = dense_X->Values();

   if( dense_Z->IsHomogeneous() )
   {
      Number val = alpha * dense_Z->Scalar();
      if( val != 0. )
      {
         for( Index i = 0; i < NCols(); i++ )
         {
            vals_X[exp_pos[i]] += val / vals_S[i];
         }
      }
   }
   else
   {
      const Number* vals_Z = dense_Z->Values();
      if( alpha == 1. )
      {
         for( Index i = 0; i < NCols(); i++ )
         {
            vals_X[exp_pos[i]] += vals_Z[i] / vals_S[i];
         }
      }
      else if( alpha == -1. )
      {
         for( Index i = 0; i < NCols(); i++ )
         {
            vals_X[exp_pos[i]] -= vals_Z[i] / vals_S[i];
         }
      }
      else
      {
         for( Index i = 0; i < NCols(); i++ )
         {
            vals_X[exp_pos[i]] += alpha * vals_Z[i] / vals_S[i];
         }
      }
   }
}

void ExpansionMatrix::SinvBlrmZMTdBrImpl(
   Number        alpha,
   const Vector& S,
   const Vector& R,
   const Vector& Z,
   const Vector& D,
   Vector&       X
) const
{
   DBG_START_METH("ExpansionMatrix::SinvBlrmZMTdBrImpl",
                  dbg_verbosity);

   DBG_ASSERT(NCols() == S.Dim());
   DBG_ASSERT(NCols() == R.Dim());
   DBG_ASSERT(NCols() == Z.Dim());
   DBG_ASSERT(NRows() == D.Dim());
   DBG_ASSERT(NCols() == X.Dim());

   const DenseVector* dense_S = static_cast<const DenseVector*>(&S);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&S));
   const DenseVector* dense_R = static_cast<const DenseVector*>(&R);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&R));
   const DenseVector* dense_Z = static_cast<const DenseVector*>(&Z);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&Z));
   const DenseVector* dense_D = static_cast<const DenseVector*>(&D);
   DBG_ASSERT(dynamic_cast<const DenseVector*>(&D));
   DenseVector* dense_X = static_cast<DenseVector*>(&X);
   DBG_ASSERT(dynamic_cast<DenseVector*>(&X));

   // if the vectors S or D are of the homogeneous type, revert to the
   // default implementation
   // ToDo: find out how often the default implementation is called and
   // if we should implement specialized method for the homogenenous
   // case

   if( dense_S->IsHomogeneous() || dense_D->IsHomogeneous() )
   {
      DBG_ASSERT(false && "dense_S or dense_D is homogeneous - implement specialized option?");
      Matrix::SinvBlrmZMTdBrImpl(alpha, S, R, Z, D, X);
      return;
   }

   const Index* exp_pos = ExpandedPosIndices();
   const Number* vals_S = dense_S->Values();
   const Number* vals_D = dense_D->Values();
   Number* vals_X = dense_X->Values();

   if( dense_R->IsHomogeneous() )
   {
      Number scalar_R = dense_R->Scalar();
      if( dense_Z->IsHomogeneous() )
      {
         Number val = alpha * dense_Z->Scalar();
         if( val == 0. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = scalar_R / vals_S[i];
            }
         }
         else
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (scalar_R + val * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
      }
      else
      {
         const Number* vals_Z = dense_Z->Values();
         if( alpha == 1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (scalar_R + vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
         else if( alpha == -1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (scalar_R - vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
         else
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (scalar_R + alpha * vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
      }
   }
   else
   {
      const Number* vals_R = dense_R->Values();
      if( dense_Z->IsHomogeneous() )
      {
         Number val = alpha * dense_Z->Scalar();
         for( Index i = 0; i < NCols(); i++ )
         {
            vals_X[i] = (vals_R[i] + val * vals_D[exp_pos[i]]) / vals_S[i];
         }
      }
      else
      {
         const Number* vals_Z = dense_Z->Values();
         if( alpha == 1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (vals_R[i] + vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
         else if( alpha == -1. )
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (vals_R[i] - vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
         else
         {
            for( Index i = 0; i < NCols(); i++ )
            {
               vals_X[i] = (vals_R[i] + alpha * vals_Z[i] * vals_D[exp_pos[i]]) / vals_S[i];
            }
         }
      }
   }
}

void ExpansionMatrix::ComputeRowAMaxImpl(
   Vector& rows_norms,
   bool    /*init*/
) const
{
   DenseVector* dense_vec = static_cast<DenseVector*>(&rows_norms);
   DBG_ASSERT(dynamic_cast<DenseVector*>(&rows_norms));
   Number* vec_vals = dense_vec->Values();

   const Index* exp_pos = ExpandedPosIndices();

   for( Index i = 0; i < NCols(); i++ )
   {
      vec_vals[exp_pos[i]] = Max(vec_vals[exp_pos[i]], Number(1.));
   }
}

void ExpansionMatrix::ComputeColAMaxImpl(
   Vector& cols_norms,
   bool    init
) const
{
   if( init )
   {
      cols_norms.Set(1.);
   }
   else
   {
      SmartPtr<Vector> v = cols_norms.MakeNew();
      v->Set(1.);
      cols_norms.ElementWiseMax(*v);
   }
}

void ExpansionMatrix::PrintImplOffset(
   const Journalist&  jnlst,
   EJournalLevel      level,
   EJournalCategory   category,
   const std::string& name,
   Index              indent,
   const std::string& prefix,
   Index              row_offset,
   Index              col_offset
) const
{
   jnlst.Printf(level, category,
                "\n");
   jnlst.PrintfIndented(level, category, indent,
                        "%sExpansionMatrix \"%s\" with %" IPOPT_INDEX_FORMAT " rows and %" IPOPT_INDEX_FORMAT " columns:\n", prefix.c_str(), name.c_str(), NRows(), NCols());

   const Index* exp_pos = ExpandedPosIndices();

   for( Index i = 0; i < NCols(); i++ )
   {
      jnlst.PrintfIndented(level, category, indent,
                           "%s%s[%5" IPOPT_INDEX_FORMAT ",%5" IPOPT_INDEX_FORMAT "]=%23.16e  (%" IPOPT_INDEX_FORMAT ")\n", prefix.c_str(), name.c_str(), exp_pos[i] + row_offset, i + col_offset, 1., i);
   }
}

ExpansionMatrixSpace::ExpansionMatrixSpace(
   Index        NLargeVec,
   Index        NSmallVec,
   const Index* ExpPos,
   const int    offset /*= 0*/
)
   : MatrixSpace(NLargeVec, NSmallVec),
     expanded_pos_(NULL),
     compressed_pos_(NULL)
{
   if( NCols() > 0 )
   {
      expanded_pos_ = new Index[NCols()];
   }
   if( NRows() > 0 )
   {
      compressed_pos_ = new Index[NRows()];
   }
   for( Index j = 0; j < NRows(); j++ )
   {
      compressed_pos_[j] = -1;
   }
   for( Index i = 0; i < NCols(); i++ )
   {
      //ToDo decide for offset
      DBG_ASSERT(ExpPos[i] - offset < NRows() && ExpPos[i] - offset >= 0);
      expanded_pos_[i] = ExpPos[i] - offset;
      if( NRows() > 0 )
      {
         compressed_pos_[ExpPos[i] - offset] = i;
      }
   }
}

} // namespace Ipopt