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
// 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 "IpoptConfig.h"
#include "IpCompoundSymMatrix.hpp"
#include "IpCompoundVector.hpp"

# include <cstdio>

namespace Ipopt
{

CompoundSymMatrix::CompoundSymMatrix(
   const CompoundSymMatrixSpace* owner_space
)
   : SymMatrix(owner_space),
     owner_space_(owner_space),
     matrices_valid_(false)
{
   for( Index irow = 0; irow < NComps_Dim(); irow++ )
   {
      std::vector<SmartPtr<Matrix> > row(irow + 1);
      std::vector<SmartPtr<const Matrix> > const_row(irow + 1);
      comps_.push_back(row);
      const_comps_.push_back(const_row);
   }
}

CompoundSymMatrix::~CompoundSymMatrix()
{ }

void CompoundSymMatrix::SetComp(
   Index         irow,
   Index         jcol,
   const Matrix& matrix
)
{
   DBG_ASSERT(!matrices_valid_);
   DBG_ASSERT(irow < NComps_Dim());
   DBG_ASSERT(jcol <= irow);
   // Matrices on the diagonal must be symmetric
   DBG_ASSERT(irow != jcol || dynamic_cast<const SymMatrix*>(&matrix));
   DBG_ASSERT(owner_space_->GetCompSpace(irow, jcol)->IsMatrixFromSpace(matrix));

   comps_[irow][jcol] = NULL;
   const_comps_[irow][jcol] = &matrix;
   ObjectChanged();
}

void CompoundSymMatrix::SetCompNonConst(
   Index   irow,
   Index   jcol,
   Matrix& matrix
)
{
   DBG_ASSERT(!matrices_valid_);
   DBG_ASSERT(irow < NComps_Dim());
   DBG_ASSERT(jcol <= irow);
   // Matrices on the diagonal must be symmetric
   DBG_ASSERT( irow != jcol || dynamic_cast<SymMatrix*>(&matrix));
   DBG_ASSERT(owner_space_->GetCompSpace(irow, jcol)->IsMatrixFromSpace(matrix));

   const_comps_[irow][jcol] = NULL;
   comps_[irow][jcol] = &matrix;
   ObjectChanged();
}

Index CompoundSymMatrix::NComps_Dim() const
{
   return owner_space_->NComps_Dim();
}

void CompoundSymMatrix::MultVectorImpl(
   Number        alpha,
   const Vector& x,
   Number        beta,
   Vector&       y
) const
{
   if( !matrices_valid_ )
   {
      matrices_valid_ = MatricesValid();
   }
   DBG_ASSERT(matrices_valid_);

   // The vectors are assumed to be compound Vectors as well
   const CompoundVector* comp_x = static_cast<const CompoundVector*>(&x);
   DBG_ASSERT(dynamic_cast<const CompoundVector*>(&x));
   CompoundVector* comp_y = static_cast<CompoundVector*>(&y);
   DBG_ASSERT(dynamic_cast<CompoundVector*>(&y));

   //  A few sanity checks
   if( comp_x )
   {
      DBG_ASSERT(NComps_Dim() == comp_x->NComps());
   }
   else
   {
      DBG_ASSERT(NComps_Dim() == 1);
   }
   if( comp_y )
   {
      DBG_ASSERT(NComps_Dim() == comp_y->NComps());
   }
   else
   {
      DBG_ASSERT(NComps_Dim() == 1);
   }

   // 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
   }

   for( Index irow = 0; irow < NComps_Dim(); irow++ )
   {
      SmartPtr<Vector> y_i;
      if( comp_y )
      {
         y_i = comp_y->GetCompNonConst(irow);
      }
      else
      {
         y_i = &y;
      }
      DBG_ASSERT(IsValid(y_i));

      for( Index jcol = 0; jcol <= irow; jcol++ )
      {
         SmartPtr<const Vector> x_j;
         if( comp_x )
         {
            x_j = comp_x->GetComp(irow);
         }
         else
         {
            x_j = &x;
         }
         DBG_ASSERT(IsValid(x_j));

         if( ConstComp(irow, jcol) )
         {
            ConstComp(irow, jcol)->MultVector(alpha, *comp_x->GetComp(jcol), 1., *comp_y->GetCompNonConst(irow));
         }
      }

      for( Index jcol = irow + 1; jcol < NComps_Dim(); jcol++ )
      {
         if( ConstComp(jcol, irow) )
         {
            ConstComp(jcol, irow)->TransMultVector(alpha, *comp_x->GetComp(jcol), 1., *comp_y->GetCompNonConst(irow));
         }
      }
   }
}

bool CompoundSymMatrix::HasValidNumbersImpl() const
{
   if( !matrices_valid_ )
   {
      matrices_valid_ = MatricesValid();
   }
   DBG_ASSERT(matrices_valid_);

   for( Index irow = 0; irow < NComps_Dim(); irow++ )
   {
      for( Index jcol = 0; jcol <= irow; jcol++ )
      {
         if( ConstComp(irow, jcol) )
         {
            if( !ConstComp(irow, jcol)->HasValidNumbers() )
            {
               return false;
            }
         }
      }
   }
   return true;
}

void CompoundSymMatrix::ComputeRowAMaxImpl(
   Vector& rows_norms,
   bool    /*init*/
) const
{
   if( !matrices_valid_ )
   {
      matrices_valid_ = MatricesValid();
   }
   DBG_ASSERT(matrices_valid_);

   // The vector is assumed to be compound Vectors as well except if
   // there is only one component
   CompoundVector* comp_vec = dynamic_cast<CompoundVector*>(&rows_norms);

   //  A few sanity checks
   if( comp_vec )
   {
      DBG_ASSERT(NComps_Dim() == comp_vec->NComps());
   }
   else
   {
      DBG_ASSERT(NComps_Dim() == 1);
   }

   for( Index jcol = 0; jcol < NComps_Dim(); jcol++ )
   {
      for( Index irow = 0; irow < NComps_Dim(); irow++ )
      {
         SmartPtr<Vector> vec_i;
         if( comp_vec )
         {
            vec_i = comp_vec->GetCompNonConst(irow);
         }
         else
         {
            vec_i = &rows_norms;
         }
         DBG_ASSERT(IsValid(vec_i));
         if( jcol <= irow && ConstComp(irow, jcol) )
         {
            ConstComp(irow, jcol)->ComputeRowAMax(*vec_i, false);
         }
         else if( jcol > irow && ConstComp(jcol, irow) )
         {
            ConstComp(jcol, irow)->ComputeRowAMax(*vec_i, false);
         }
      }
   }
}

void CompoundSymMatrix::PrintImpl(
   const Journalist&  jnlst,
   EJournalLevel      level,
   EJournalCategory   category,
   const std::string& name,
   Index              indent,
   const std::string& prefix
) const
{
   jnlst.Printf(level, category,
                "\n");
   jnlst.PrintfIndented(level, category, indent,
                        "%sCompoundSymMatrix \"%s\" with %" IPOPT_INDEX_FORMAT " rows and columns components:\n", prefix.c_str(), name.c_str(), NComps_Dim());
   for( Index irow = 0; irow < NComps_Dim(); irow++ )
   {
      for( Index jcol = 0; jcol <= irow; jcol++ )
      {
         jnlst.PrintfIndented(level, category, indent,
                              "%sComponent for row %" IPOPT_INDEX_FORMAT " and column %" IPOPT_INDEX_FORMAT ":\n", prefix.c_str(), irow, jcol);
         if( ConstComp(irow, jcol) )
         {
            DBG_ASSERT(name.size() < 200);
            char buffer[256];
            Snprintf(buffer, 255, "%s[%" IPOPT_INDEX_FORMAT "][%" IPOPT_INDEX_FORMAT "]", name.c_str(), irow, jcol);
            std::string term_name = buffer;
            ConstComp(irow, jcol)->Print(&jnlst, level, category, term_name, indent + 1, prefix);
         }
         else
         {
            jnlst.PrintfIndented(level, category, indent,
                                 "%sThis component has not been set.\n", prefix.c_str());
         }
      }
   }
}

bool CompoundSymMatrix::MatricesValid() const
{
   // Check to make sure we have matrices everywhere the space has matrices
   // We already check that the matrix agrees with the block space
   // in the SetComp methods
   bool retValue = true;
   for( Index i = 0; i < NComps_Dim(); i++ )
   {
      for( Index j = 0; j <= i; j++ )
      {
         if( (!ConstComp(i, j) && IsValid(owner_space_->GetCompSpace(i, j)))
             || (ConstComp(i, j) && IsNull(owner_space_->GetCompSpace(i, j))) )
         {
            retValue = false;
            break;
         }
      }
   }

   return retValue;
}

CompoundSymMatrixSpace::CompoundSymMatrixSpace(
   Index ncomp_spaces,
   Index total_dim
)
   : SymMatrixSpace(total_dim),
     ncomp_spaces_(ncomp_spaces),
     block_dim_(ncomp_spaces, -1),
     dimensions_set_(false)
{
   for( Index irow = 0; irow < ncomp_spaces_; irow++ )
   {
      std::vector<SmartPtr<const MatrixSpace> > row(irow + 1);
      std::vector<bool> allocate_row(irow + 1, false);
      comp_spaces_.push_back(row);
      allocate_block_.push_back(allocate_row);
   }
}

void CompoundSymMatrixSpace::SetBlockDim(
   Index irow_jcol,
   Index dim
)
{
   DBG_ASSERT(!dimensions_set_ && "for now, if dimensions are set, they cannot be changed");
   DBG_ASSERT(block_dim_[irow_jcol] == -1 && "This dimension has already been set - sanity check");
   DBG_ASSERT(irow_jcol < ncomp_spaces_);
   block_dim_[irow_jcol] = dim;
}

Index CompoundSymMatrixSpace::GetBlockDim(
   Index irow_jcol
) const
{
   DBG_ASSERT(dimensions_set_ && "Cannot get block dimensions before all dimensions are set.");
   DBG_ASSERT(irow_jcol < ncomp_spaces_);
   return block_dim_[irow_jcol];
}

void CompoundSymMatrixSpace::SetCompSpace(
   Index              irow,
   Index              jcol,
   const MatrixSpace& mat_space,
   bool               auto_allocate /*=false*/
)
{
   if( !dimensions_set_ )
   {
      dimensions_set_ = DimensionsSet();
   }
   DBG_ASSERT(dimensions_set_);
   DBG_ASSERT(irow < ncomp_spaces_);
   DBG_ASSERT(jcol <= irow);
   DBG_ASSERT(IsNull(comp_spaces_[irow][jcol]));
   DBG_ASSERT(irow != jcol || dynamic_cast<const SymMatrixSpace*> (&mat_space));
   DBG_ASSERT(block_dim_[jcol] != -1 && block_dim_[jcol] == mat_space.NCols());
   DBG_ASSERT(block_dim_[irow] != -1 && block_dim_[irow] == mat_space.NRows());

   comp_spaces_[irow][jcol] = &mat_space;
   allocate_block_[irow][jcol] = auto_allocate;
}

CompoundSymMatrix* CompoundSymMatrixSpace::MakeNewCompoundSymMatrix() const
{
   if( !dimensions_set_ )
   {
      dimensions_set_ = DimensionsSet();
   }
   DBG_ASSERT(dimensions_set_);

   CompoundSymMatrix* mat = new CompoundSymMatrix(this);
   for( Index i = 0; i < NComps_Dim(); i++ )
   {
      for( Index j = 0; j <= i; j++ )
      {
         if( allocate_block_[i][j] )
         {
            mat->SetCompNonConst(i, j, *GetCompSpace(i, j)->MakeNew());
         }
      }
   }

   return mat;
}

bool CompoundSymMatrixSpace::DimensionsSet() const
{
   DBG_DO(Index total_dim = 0);
   bool valid = true;
   for( Index i = 0; i < ncomp_spaces_; i++ )
   {
      if( block_dim_[i] == -1 )
      {
         valid = false;
         break;
      }
      DBG_DO(total_dim += block_dim_[i]);
   }

   if( valid )
   {
      DBG_ASSERT(total_dim == Dim());
   }

   return valid;
}

} // namespace Ipopt