Ipopt Documentation  
 
Loading...
Searching...
No Matches
IpVector.hpp
Go to the documentation of this file.
1// Copyright (C) 2004, 2008 International Business Machines and others.
2// All Rights Reserved.
3// This code is published under the Eclipse Public License.
4//
5// Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
6
7#ifndef __IPVECTOR_HPP__
8#define __IPVECTOR_HPP__
9
10#include "IpTypes.hpp"
11#include "IpTaggedObject.hpp"
12#include "IpCachedResults.hpp"
13#include "IpSmartPtr.hpp"
14#include "IpJournalist.hpp"
15#include "IpException.hpp"
16
17#include <vector>
18
19namespace Ipopt
20{
21
24DECLARE_STD_EXCEPTION(UNIMPLEMENTED_LINALG_METHOD_CALLED);
25
26/* forward declarations */
27class VectorSpace;
28
48{
49public:
52
56 inline Vector(
57 const VectorSpace* owner_space
58 );
59
61 inline virtual ~Vector();
63
65 inline Vector* MakeNew() const;
66
68 inline Vector* MakeNewCopy() const;
69
76
77 inline void Copy(
78 const Vector& x
79 );
80
82 void Scal(
83 Number alpha
84 );
85
87 inline void Axpy(
88 Number alpha,
89 const Vector& x
90 );
91
93 inline Number Dot(
94 const Vector& x
95 ) const;
96
98 inline Number Nrm2() const;
99
101 inline Number Asum() const;
102
104 inline Number Amax() const;
106
113
114 inline void Set(
115 Number alpha
116 );
117
119 inline void ElementWiseDivide(
120 const Vector& x
121 );
122
124 inline void ElementWiseMultiply(
125 const Vector& x
126 );
127
129 inline void ElementWiseMax(
130 const Vector& x
131 );
132
134 inline void ElementWiseMin(
135 const Vector& x
136 );
137
139 inline void ElementWiseReciprocal();
140
142 inline void ElementWiseAbs();
143
145 inline void ElementWiseSqrt();
146
150 inline void ElementWiseSgn();
151
153 inline void AddScalar(
154 Number scalar
155 );
156
158 inline Number Max() const;
159
161 inline Number Min() const;
162
164 inline Number Sum() const;
165
167 inline Number SumLogs() const;
169
176
180 inline void AddOneVector(
181 Number a,
182 const Vector& v1,
183 Number c
184 );
185
190 inline void AddTwoVectors(
191 Number a,
192 const Vector& v1,
193 Number b,
194 const Vector& v2,
195 Number c
196 );
197
202 inline Number FracToBound(
203 const Vector& delta,
204 Number tau
205 ) const;
206
208 inline void AddVectorQuotient(
209 Number a,
210 const Vector& z,
211 const Vector& s,
212 Number c
213 );
215
217 inline bool HasValidNumbers() const;
218
221
222 inline Index Dim() const;
223
225 inline SmartPtr<const VectorSpace> OwnerSpace() const;
227
234
235 void Print(
237 EJournalLevel level,
238 EJournalCategory category,
239 const std::string& name,
240 Index indent = 0,
241 const std::string& prefix = ""
242 ) const;
243
244 void Print(
245 const Journalist& jnlst,
246 EJournalLevel level,
247 EJournalCategory category,
248 const std::string& name,
249 Index indent = 0,
250 const std::string& prefix = ""
251 ) const;
253
254protected:
259
260 virtual void CopyImpl(
261 const Vector& x
262 ) = 0;
263
265 virtual void ScalImpl(
266 Number alpha
267 ) = 0;
268
270 virtual void AxpyImpl(
271 Number alpha,
272 const Vector& x
273 ) = 0;
274
277 const Vector& x
278 ) const = 0;
279
281 virtual Number Nrm2Impl() const = 0;
282
284 virtual Number AsumImpl() const = 0;
285
287 virtual Number AmaxImpl() const = 0;
288
290 virtual void SetImpl(
291 Number alpha
292 ) = 0;
293
296 const Vector& x
297 ) = 0;
298
301 const Vector& x
302 ) = 0;
303
305 virtual void ElementWiseMaxImpl(
306 const Vector& x
307 ) = 0;
308
310 virtual void ElementWiseMinImpl(
311 const Vector& x
312 ) = 0;
313
315 virtual void ElementWiseReciprocalImpl() = 0;
316
318 virtual void ElementWiseAbsImpl() = 0;
319
321 virtual void ElementWiseSqrtImpl() = 0;
322
324 virtual void ElementWiseSgnImpl() = 0;
325
327 virtual void AddScalarImpl(
328 Number scalar
329 ) = 0;
330
332 virtual Number MaxImpl() const = 0;
333
335 virtual Number MinImpl() const = 0;
336
338 virtual Number SumImpl() const = 0;
339
341 virtual Number SumLogsImpl() const = 0;
342
347 virtual void AddTwoVectorsImpl(
348 Number a,
349 const Vector& v1,
350 Number b,
351 const Vector& v2,
352 Number c
353 );
354
357 const Vector& delta,
358 Number tau
359 ) const;
360
363 Number a,
364 const Vector& z,
365 const Vector& s,
366 Number c
367 );
368
372 virtual bool HasValidNumbersImpl() const;
373
375 virtual void PrintImpl(
376 const Journalist& jnlst,
377 EJournalLevel level,
378 EJournalCategory category,
379 const std::string& name,
380 Index indent,
381 const std::string& prefix
382 ) const = 0;
384
385private:
395
397
400 const Vector&
401 );
402
405 const Vector&
406 );
408
411
414
416
419
422
425
428
431
434
437
439 mutable bool cached_valid_;
440
441 // AW: I removed this cache since it gets in the way for the
442 // quality function search
443 // /** Cache for FracToBound */
444 // mutable CachedResults<Number> frac_to_bound_cache_;
446
447};
448
459{
460public:
463
467 Index dim
468 );
469
471 virtual ~VectorSpace()
472 { }
474
478 virtual Vector* MakeNew() const = 0;
479
481 Index Dim() const
482 {
483 return dim_;
484 }
485
486private:
496
498
501 const VectorSpace&
502 );
503
506 const VectorSpace&
507 );
509
511 const Index dim_;
512};
513
514/* inline methods */
516{ }
517
519 const VectorSpace* owner_space
520)
521 : TaggedObject(),
522 owner_space_(owner_space),
523 dot_cache_(10),
524 nrm2_cache_tag_(0),
525 asum_cache_tag_(0),
526 amax_cache_tag_(0),
527 max_cache_tag_(0),
528 min_cache_tag_(0),
529 sum_cache_tag_(0),
530 sumlogs_cache_tag_(0),
531 cached_valid_(0)
532{
534}
535
536inline Vector* Vector::MakeNew() const
537{
538 return owner_space_->MakeNew();
539}
540
542{
543 // ToDo: We can probably copy also the cached values for Norms etc here
544 Vector* copy = MakeNew();
545 copy->Copy(*this);
546 return copy;
547}
548
549inline void Vector::Copy(
550 const Vector& x
551)
552{
553 CopyImpl(x);
555 // Also copy any cached scalar values from the original vector
556 // ToDo: Check if that is too much overhead
557 TaggedObject::Tag x_tag = x.GetTag();
558 if( x_tag == x.nrm2_cache_tag_ )
559 {
562 }
563 if( x_tag == x.asum_cache_tag_ )
564 {
567 }
568 if( x_tag == x.amax_cache_tag_ )
569 {
572 }
573 if( x_tag == x.max_cache_tag_ )
574 {
577 }
578 if( x_tag == x.min_cache_tag_ )
579 {
582 }
583 if( x_tag == x.sum_cache_tag_ )
584 {
587 }
588 if( x_tag == x.sumlogs_cache_tag_ )
589 {
592 }
593}
594
595inline void Vector::Axpy(
596 Number alpha,
597 const Vector& x
598)
599{
600 AxpyImpl(alpha, x);
602}
603
605 const Vector& x
606) const
607{
608 // The current implementation of the caching doesn't allow to have
609 // a dependency of something with itself. Therefore, we use the
610 // Nrm2 method if the dot product is to be taken with the vector
611 // itself. Might be more efficient anyway.
612 if( this == &x )
613 {
614 Number nrm2 = Nrm2();
615 return nrm2 * nrm2;
616 }
617 Number retValue;
618 if( !dot_cache_.GetCachedResult2Dep(retValue, this, &x) )
619 {
620 retValue = DotImpl(x);
621 dot_cache_.AddCachedResult2Dep(retValue, this, &x);
622 }
623 return retValue;
624}
625
626inline Number Vector::Nrm2() const
627{
628 if( nrm2_cache_tag_ != GetTag() )
629 {
632 }
633 return cached_nrm2_;
634}
635
636inline Number Vector::Asum() const
637{
638 if( asum_cache_tag_ != GetTag() )
639 {
642 }
643 return cached_asum_;
644}
645
646inline Number Vector::Amax() const
647{
648 if( amax_cache_tag_ != GetTag() )
649 {
652 }
653 return cached_amax_;
654}
655
656inline Number Vector::Sum() const
657{
658 if( sum_cache_tag_ != GetTag() )
659 {
662 }
663 return cached_sum_;
664}
665
667{
668 if( sumlogs_cache_tag_ != GetTag() )
669 {
672 }
673 return cached_sumlogs_;
674}
675
677{
680}
681
682inline void Vector::Set(
683 Number alpha
684)
685{
686 // Could initialize caches here
687 SetImpl(alpha);
689}
690
692 const Vector& x
693)
694{
697}
698
700 const Vector& x
701)
702{
705}
706
708{
711}
712
714 const Vector& x
715)
716{
717 // Could initialize some caches here
720}
721
723 const Vector& x
724)
725{
726 // Could initialize some caches here
729}
730
732{
733 // Could initialize some caches here
736}
737
739{
742}
743
745 Number scalar)
746{
747 // Could initialize some caches here
748 AddScalarImpl(scalar);
750}
751
752inline Number Vector::Max() const
753{
754 if( max_cache_tag_ != GetTag() )
755 {
758 }
759 return cached_max_;
760}
761
762inline Number Vector::Min() const
763{
764 if( min_cache_tag_ != GetTag() )
765 {
768 }
769 return cached_min_;
770}
771
773 Number a,
774 const Vector& v1,
775 Number c
776)
777{
778 AddTwoVectors(a, v1, 0., v1, c);
779}
780
782 Number a,
783 const Vector& v1,
784 Number b,
785 const Vector& v2,
786 Number c
787)
788{
789 AddTwoVectorsImpl(a, v1, b, v2, c);
791}
792
794 const Vector& delta,
795 Number tau
796) const
797{
798 /* AW: I avoid the caching here, since it leads to overhead in the
799 quality function search. Caches for this are in
800 CalculatedQuantities.
801 Number retValue;
802 std::vector<const TaggedObject*> tdeps(1);
803 tdeps[0] = &delta;
804 std::vector<Number> sdeps(1);
805 sdeps[0] = tau;
806 if (!frac_to_bound_cache_.GetCachedResult(retValue, tdeps, sdeps)) {
807 retValue = FracToBoundImpl(delta, tau);
808 frac_to_bound_cache_.AddCachedResult(retValue, tdeps, sdeps);
809 }
810 return retValue;
811 */
812 return FracToBoundImpl(delta, tau);
813}
814
816 Number a,
817 const Vector& z,
818 const Vector& s,
819 Number c
820)
821{
822 AddVectorQuotientImpl(a, z, s, c);
824}
825
826inline bool Vector::HasValidNumbers() const
827{
828 if( valid_cache_tag_ != GetTag() )
829 {
832 }
833 return cached_valid_;
834}
835
836inline Index Vector::Dim() const
837{
838 return owner_space_->Dim();
839}
840
842{
843 return owner_space_;
844}
845
847 Index dim
848)
849 : dim_(dim)
850{ }
851
852} // namespace Ipopt
853
854// Macro definitions for debugging vectors
855#if IPOPT_VERBOSITY == 0
856# define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec)
857#else
858# define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec) \
859 if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
860 if (dbg_jrnl.Jnlst()!=NULL) { \
861 (__vec).Print(dbg_jrnl.Jnlst(), \
862 J_ERROR, J_DBG, \
863 __vec_name, \
864 dbg_jrnl.IndentationLevel()*2, \
865 "# "); \
866 } \
867 }
868#endif //if IPOPT_VERBOSITY == 0
869
870#endif
#define DBG_ASSERT(test)
Definition: IpDebug.hpp:27
#define DECLARE_STD_EXCEPTION(__except_type)
Templated class for Cached Results.
void AddCachedResult2Dep(const T &result, const TaggedObject *dependent1, const TaggedObject *dependent2)
Method for adding a result to the cache, proving two dependencies as a TaggedObject explicitly.
bool GetCachedResult2Dep(T &retResult, const TaggedObject *dependent1, const TaggedObject *dependent2)
Method for retrieving a cached result, proving two dependencies as a TaggedObject explicitly.
Class responsible for all message output.
Storing the reference count of all the smart pointers that currently reference it.
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:172
TaggedObject class.
Tag GetTag() const
Users of TaggedObjects call this to update their own internal tags every time they perform the expens...
void ObjectChanged()
Objects derived from TaggedObject MUST call this method every time their internal state changes to up...
unsigned int Tag
Type for the Tag values.
VectorSpace base class, corresponding to the Vector base class.
Definition: IpVector.hpp:459
VectorSpace(const VectorSpace &)
Copy constructor.
Index Dim() const
Accessor function for the dimension of the vectors of this type.
Definition: IpVector.hpp:481
VectorSpace & operator=(const VectorSpace &)
Default Assignment Operator.
VectorSpace()
Default constructor.
virtual Vector * MakeNew() const =0
Pure virtual method for creating a new Vector of the corresponding type.
const Index dim_
Dimension of the vectors in this vector space.
Definition: IpVector.hpp:511
virtual ~VectorSpace()
Destructor.
Definition: IpVector.hpp:471
Vector Base Class.
Definition: IpVector.hpp:48
virtual void AddTwoVectorsImpl(Number a, const Vector &v1, Number b, const Vector &v2, Number c)
Add two vectors (a * v1 + b * v2).
virtual Number AmaxImpl() const =0
Computes the max-norm of this vector (based on IDAMAX)
SmartPtr< const VectorSpace > OwnerSpace() const
Return the owner VectorSpace.
Definition: IpVector.hpp:841
Number Max() const
Returns the maximum value in the vector.
Definition: IpVector.hpp:752
virtual void AddScalarImpl(Number scalar)=0
Add scalar to every component of vector.
virtual void ElementWiseReciprocalImpl()=0
Reciprocates the elements of the vector.
void Print(SmartPtr< const Journalist > jnlst, EJournalLevel level, EJournalCategory category, const std::string &name, Index indent=0, const std::string &prefix="") const
Print the entire vector.
void AddTwoVectors(Number a, const Vector &v1, Number b, const Vector &v2, Number c)
Add two vectors, y = a * v1 + b * v2 + c * y.
Definition: IpVector.hpp:781
virtual Number SumImpl() const =0
Sum of entries in the vector.
void ElementWiseMax(const Vector &x)
Element-wise max against entries in x.
Definition: IpVector.hpp:713
virtual void ElementWiseSgnImpl()=0
Replaces entries with sgn of the entry.
Number Min() const
Returns the minimum value in the vector.
Definition: IpVector.hpp:762
Number cached_amax_
Definition: IpVector.hpp:424
TaggedObject::Tag sum_cache_tag_
Definition: IpVector.hpp:432
Vector()
Default constructor.
TaggedObject::Tag asum_cache_tag_
Definition: IpVector.hpp:420
void ElementWiseMin(const Vector &x)
Element-wise min against entries in x.
Definition: IpVector.hpp:722
virtual void ElementWiseMinImpl(const Vector &x)=0
Element-wise min against entries in x.
Number cached_min_
Definition: IpVector.hpp:430
void ElementWiseSgn()
Replaces the vector values with their sgn values ( -1 if x_i < 0, 0 if x_i == 0, and 1 if x_i > 0)
Definition: IpVector.hpp:676
Vector(const Vector &)
Copy constructor.
TaggedObject::Tag nrm2_cache_tag_
Definition: IpVector.hpp:417
CachedResults< Number > dot_cache_
Cache for dot products.
Definition: IpVector.hpp:415
TaggedObject::Tag sumlogs_cache_tag_
Definition: IpVector.hpp:435
virtual Number MaxImpl() const =0
Max value in the vector.
void ElementWiseMultiply(const Vector &x)
Element-wise multiplication .
Definition: IpVector.hpp:699
void Axpy(Number alpha, const Vector &x)
Add the multiple alpha of vector x to this vector (DAXPY)
Definition: IpVector.hpp:595
const SmartPtr< const VectorSpace > owner_space_
Vector Space.
Definition: IpVector.hpp:410
bool HasValidNumbers() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
Definition: IpVector.hpp:826
Vector * MakeNewCopy() const
Create new Vector of the same type and copy the data over.
Definition: IpVector.hpp:541
virtual void CopyImpl(const Vector &x)=0
Copy the data of the vector x into this vector (DCOPY).
virtual void AxpyImpl(Number alpha, const Vector &x)=0
Add the multiple alpha of vector x to this vector (DAXPY)
Number Nrm2() const
Computes the 2-norm of this vector (DNRM2)
Definition: IpVector.hpp:626
virtual Number SumLogsImpl() const =0
Sum of logs of entries in the vector.
Number cached_sum_
Definition: IpVector.hpp:433
virtual void SetImpl(Number alpha)=0
Set each element in the vector to the scalar alpha.
void AddScalar(Number scalar)
Add scalar to every vector component.
Definition: IpVector.hpp:744
void AddVectorQuotient(Number a, const Vector &z, const Vector &s, Number c)
Add the quotient of two vectors, y = a * z/s + c * y.
Definition: IpVector.hpp:815
void Scal(Number alpha)
Scales the vector by scalar alpha (DSCAL)
virtual void AddVectorQuotientImpl(Number a, const Vector &z, const Vector &s, Number c)
Add the quotient of two vectors.
Vector * MakeNew() const
Create new Vector of the same type with uninitialized data.
Definition: IpVector.hpp:536
virtual Number AsumImpl() const =0
Computes the 1-norm of this vector (DASUM)
Number SumLogs() const
Returns the sum of the logs of each vector entry.
Definition: IpVector.hpp:666
void ElementWiseDivide(const Vector &x)
Element-wise division .
Definition: IpVector.hpp:691
virtual void ScalImpl(Number alpha)=0
Scales the vector by scalar alpha (DSCAL)
TaggedObject::Tag amax_cache_tag_
Definition: IpVector.hpp:423
Number cached_asum_
Definition: IpVector.hpp:421
void Copy(const Vector &x)
Copy the data of the vector x into this vector (DCOPY).
Definition: IpVector.hpp:549
Index Dim() const
Dimension of the Vector.
Definition: IpVector.hpp:836
void Set(Number alpha)
Set each element in the vector to the scalar alpha.
Definition: IpVector.hpp:682
Number cached_sumlogs_
Definition: IpVector.hpp:436
virtual void ElementWiseMultiplyImpl(const Vector &x)=0
Element-wise multiplication .
Number Amax() const
Computes the max-norm of this vector (based on IDAMAX)
Definition: IpVector.hpp:646
void Print(const Journalist &jnlst, EJournalLevel level, EJournalCategory category, const std::string &name, Index indent=0, const std::string &prefix="") const
Number Sum() const
Returns the sum of the vector entries.
Definition: IpVector.hpp:656
void AddOneVector(Number a, const Vector &v1, Number c)
Add one vector, y = a * v1 + c * y.
Definition: IpVector.hpp:772
virtual void ElementWiseDivideImpl(const Vector &x)=0
Element-wise division .
virtual Number DotImpl(const Vector &x) const =0
Computes inner product of vector x with this (DDOT)
void ElementWiseSqrt()
Element-wise square root of the entries in the vector.
Definition: IpVector.hpp:738
Number Asum() const
Computes the 1-norm of this vector (DASUM)
Definition: IpVector.hpp:636
virtual Number MinImpl() const =0
Min number in the vector.
virtual Number Nrm2Impl() const =0
Computes the 2-norm of this vector (DNRM2)
Vector & operator=(const Vector &)
Default Assignment Operator.
virtual void PrintImpl(const Journalist &jnlst, EJournalLevel level, EJournalCategory category, const std::string &name, Index indent, const std::string &prefix) const =0
Print the entire vector.
TaggedObject::Tag max_cache_tag_
Definition: IpVector.hpp:426
Number FracToBound(const Vector &delta, Number tau) const
Fraction to the boundary parameter.
Definition: IpVector.hpp:793
void ElementWiseReciprocal()
Reciprocates the entries in the vector.
Definition: IpVector.hpp:707
Number cached_max_
Definition: IpVector.hpp:427
void ElementWiseAbs()
Absolute values of the entries in the vector.
Definition: IpVector.hpp:731
virtual void ElementWiseAbsImpl()=0
Take elementwise absolute values of the elements of the vector.
virtual bool HasValidNumbersImpl() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
virtual ~Vector()
Destructor.
Definition: IpVector.hpp:515
Number cached_nrm2_
Definition: IpVector.hpp:418
virtual void ElementWiseMaxImpl(const Vector &x)=0
Element-wise max against entries in x.
virtual Number FracToBoundImpl(const Vector &delta, Number tau) const
Fraction to boundary parameter.
TaggedObject::Tag valid_cache_tag_
Definition: IpVector.hpp:438
virtual void ElementWiseSqrtImpl()=0
Take elementwise square-root of the elements of the vector.
bool cached_valid_
Definition: IpVector.hpp:439
TaggedObject::Tag min_cache_tag_
Definition: IpVector.hpp:429
Number Dot(const Vector &x) const
Computes inner product of vector x with this (DDOT)
Definition: IpVector.hpp:604
#define IPOPTLIB_EXPORT
This file contains a base class for all exceptions and a set of macros to help with exceptions.
bool IsValid(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:674
Index Max(Index a, Index b)
Definition: IpUtils.hpp:17
EJournalCategory
Category Selection Enum.
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:17
Index Min(Index a, Index b)
Definition: IpUtils.hpp:48
EJournalLevel
Print Level Enum.
double Number
Type of all numbers.
Definition: IpTypes.hpp:15