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
25
26/* forward declarations */
27class VectorSpace;
28
48{
49public:
52
56 inline Vector(
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(
84 );
85
87 inline void Axpy(
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(
116 );
117
119 inline void ElementWiseDivide(
120 const Vector& x
121 );
122
124 inline void ElementWiseMultiply(
125 const Vector& x
126 );
127
133 inline void ElementWiseSelect(
134 const Vector& x
135 );
136
138 inline void ElementWiseMax(
139 const Vector& x
140 );
141
143 inline void ElementWiseMin(
144 const Vector& x
145 );
146
148 inline void ElementWiseReciprocal();
149
151 inline void ElementWiseAbs();
152
154 inline void ElementWiseSqrt();
155
159 inline void ElementWiseSgn();
160
162 inline void AddScalar(
164 );
165
167 inline Number Max() const;
168
170 inline Number Min() const;
171
173 inline Number Sum() const;
174
176 inline Number SumLogs() const;
178
185
189 inline void AddOneVector(
190 Number a,
191 const Vector& v1,
192 Number c
193 );
194
199 inline void AddTwoVectors(
200 Number a,
201 const Vector& v1,
202 Number b,
203 const Vector& v2,
204 Number c
205 );
206
211 inline Number FracToBound(
212 const Vector& delta,
213 Number tau
214 ) const;
215
217 inline void AddVectorQuotient(
218 Number a,
219 const Vector& z,
220 const Vector& s,
221 Number c
222 );
224
226 inline bool HasValidNumbers() const;
227
230
231 inline Index Dim() const;
232
234 inline SmartPtr<const VectorSpace> OwnerSpace() const;
236
243
244 void Print(
248 const std::string& name,
249 Index indent = 0,
250 const std::string& prefix = ""
251 ) const;
252
253 void Print(
254 const Journalist& jnlst,
257 const std::string& name,
258 Index indent = 0,
259 const std::string& prefix = ""
260 ) const;
262
263protected:
268
269 virtual void CopyImpl(
270 const Vector& x
271 ) = 0;
272
274 virtual void ScalImpl(
276 ) = 0;
277
279 virtual void AxpyImpl(
281 const Vector& x
282 ) = 0;
283
286 const Vector& x
287 ) const = 0;
288
290 virtual Number Nrm2Impl() const = 0;
291
293 virtual Number AsumImpl() const = 0;
294
296 virtual Number AmaxImpl() const = 0;
297
299 virtual void SetImpl(
301 ) = 0;
302
305 const Vector& x
306 ) = 0;
307
310 const Vector& x
311 ) = 0;
312
315 const Vector& x
316 ) = 0;
317
319 virtual void ElementWiseMaxImpl(
320 const Vector& x
321 ) = 0;
322
324 virtual void ElementWiseMinImpl(
325 const Vector& x
326 ) = 0;
327
329 virtual void ElementWiseReciprocalImpl() = 0;
330
332 virtual void ElementWiseAbsImpl() = 0;
333
335 virtual void ElementWiseSqrtImpl() = 0;
336
338 virtual void ElementWiseSgnImpl() = 0;
339
341 virtual void AddScalarImpl(
343 ) = 0;
344
346 virtual Number MaxImpl() const = 0;
347
349 virtual Number MinImpl() const = 0;
350
352 virtual Number SumImpl() const = 0;
353
355 virtual Number SumLogsImpl() const = 0;
356
361 virtual void AddTwoVectorsImpl(
362 Number a,
363 const Vector& v1,
364 Number b,
365 const Vector& v2,
366 Number c
367 );
368
371 const Vector& delta,
372 Number tau
373 ) const;
374
377 Number a,
378 const Vector& z,
379 const Vector& s,
380 Number c
381 );
382
386 virtual bool HasValidNumbersImpl() const;
387
389 virtual void PrintImpl(
390 const Journalist& jnlst,
393 const std::string& name,
395 const std::string& prefix
396 ) const = 0;
398
399private:
409
411
414 const Vector&
415 );
416
419 const Vector&
420 );
422
425
428
430
433
436
439
442
445
448
451
453 mutable bool cached_valid_;
454
455 // AW: I removed this cache since it gets in the way for the
456 // quality function search
457 // /** Cache for FracToBound */
458 // mutable CachedResults<Number> frac_to_bound_cache_;
460
461};
462
473{
474public:
477
481 Index dim
482 );
483
485 virtual ~VectorSpace()
486 { }
488
492 virtual Vector* MakeNew() const = 0;
493
495 Index Dim() const
496 {
497 return dim_;
498 }
499
500private:
510
512
515 const VectorSpace&
516 );
517
520 const VectorSpace&
521 );
523
525 const Index dim_;
526};
527
528/* inline methods */
530{ }
531
534)
535 : TaggedObject(),
536 owner_space_(owner_space),
537 dot_cache_(10),
538 nrm2_cache_tag_(0),
539 asum_cache_tag_(0),
540 amax_cache_tag_(0),
541 max_cache_tag_(0),
542 min_cache_tag_(0),
543 sum_cache_tag_(0),
544 sumlogs_cache_tag_(0),
545 cached_valid_(0)
546{
548}
549
550inline Vector* Vector::MakeNew() const
551{
552 return owner_space_->MakeNew();
553}
554
556{
557 // ToDo: We can probably copy also the cached values for Norms etc here
558 Vector* copy = MakeNew();
559 copy->Copy(*this);
560 return copy;
561}
562
563inline void Vector::Copy(
564 const Vector& x
565)
566{
567 CopyImpl(x);
569 // Also copy any cached scalar values from the original vector
570 // ToDo: Check if that is too much overhead
572 if( x_tag == x.nrm2_cache_tag_ )
573 {
576 }
577 if( x_tag == x.asum_cache_tag_ )
578 {
581 }
582 if( x_tag == x.amax_cache_tag_ )
583 {
586 }
587 if( x_tag == x.max_cache_tag_ )
588 {
591 }
592 if( x_tag == x.min_cache_tag_ )
593 {
596 }
597 if( x_tag == x.sum_cache_tag_ )
598 {
601 }
602 if( x_tag == x.sumlogs_cache_tag_ )
603 {
606 }
607}
608
609inline void Vector::Axpy(
611 const Vector& x
612)
613{
614 AxpyImpl(alpha, x);
616}
617
619 const Vector& x
620) const
621{
622 // The current implementation of the caching doesn't allow to have
623 // a dependency of something with itself. Therefore, we use the
624 // Nrm2 method if the dot product is to be taken with the vector
625 // itself. Might be more efficient anyway.
626 if( this == &x )
627 {
628 Number nrm2 = Nrm2();
629 return nrm2 * nrm2;
630 }
632 if( !dot_cache_.GetCachedResult2Dep(retValue, this, &x) )
633 {
634 retValue = DotImpl(x);
636 }
637 return retValue;
638}
639
640inline Number Vector::Nrm2() const
641{
642 if( nrm2_cache_tag_ != GetTag() )
643 {
646 }
647 return cached_nrm2_;
648}
649
650inline Number Vector::Asum() const
651{
652 if( asum_cache_tag_ != GetTag() )
653 {
656 }
657 return cached_asum_;
658}
659
660inline Number Vector::Amax() const
661{
662 if( amax_cache_tag_ != GetTag() )
663 {
666 }
667 return cached_amax_;
668}
669
670inline Number Vector::Sum() const
671{
672 if( sum_cache_tag_ != GetTag() )
673 {
676 }
677 return cached_sum_;
678}
679
681{
682 if( sumlogs_cache_tag_ != GetTag() )
683 {
686 }
687 return cached_sumlogs_;
688}
689
691{
694}
695
696inline void Vector::Set(
698)
699{
700 // Could initialize caches here
701 SetImpl(alpha);
703}
704
706 const Vector& x
707)
708{
711}
712
714 const Vector& x
715)
716{
719}
720
722 const Vector& x
723)
724{
727}
728
734
736 const Vector& x
737)
738{
739 // Could initialize some caches here
742}
743
745 const Vector& x
746)
747{
748 // Could initialize some caches here
751}
752
754{
755 // Could initialize some caches here
758}
759
761{
764}
765
768{
769 // Could initialize some caches here
772}
773
774inline Number Vector::Max() const
775{
776 if( max_cache_tag_ != GetTag() )
777 {
780 }
781 return cached_max_;
782}
783
784inline Number Vector::Min() const
785{
786 if( min_cache_tag_ != GetTag() )
787 {
790 }
791 return cached_min_;
792}
793
795 Number a,
796 const Vector& v1,
797 Number c
798)
799{
800 AddTwoVectors(a, v1, 0., v1, c);
801}
802
804 Number a,
805 const Vector& v1,
806 Number b,
807 const Vector& v2,
808 Number c
809)
810{
811 AddTwoVectorsImpl(a, v1, b, v2, c);
813}
814
816 const Vector& delta,
817 Number tau
818) const
819{
820 /* AW: I avoid the caching here, since it leads to overhead in the
821 quality function search. Caches for this are in
822 CalculatedQuantities.
823 Number retValue;
824 std::vector<const TaggedObject*> tdeps(1);
825 tdeps[0] = &delta;
826 std::vector<Number> sdeps(1);
827 sdeps[0] = tau;
828 if (!frac_to_bound_cache_.GetCachedResult(retValue, tdeps, sdeps)) {
829 retValue = FracToBoundImpl(delta, tau);
830 frac_to_bound_cache_.AddCachedResult(retValue, tdeps, sdeps);
831 }
832 return retValue;
833 */
834 return FracToBoundImpl(delta, tau);
835}
836
838 Number a,
839 const Vector& z,
840 const Vector& s,
841 Number c
842)
843{
844 AddVectorQuotientImpl(a, z, s, c);
846}
847
848inline bool Vector::HasValidNumbers() const
849{
850 if( valid_cache_tag_ != GetTag() )
851 {
854 }
855 return cached_valid_;
856}
857
858inline Index Vector::Dim() const
859{
860 return owner_space_->Dim();
861}
862
867
869 Index dim
870)
871 : dim_(dim)
872{ }
873
874} // namespace Ipopt
875
876// Macro definitions for debugging vectors
877#if IPOPT_VERBOSITY == 0
878# define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec)
879#else
880# define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec) \
881 if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
882 if (dbg_jrnl.Jnlst()!=NULL) { \
883 (__vec).Print(dbg_jrnl.Jnlst(), \
884 J_ERROR, J_DBG, \
885 __vec_name, \
886 dbg_jrnl.IndentationLevel()*2, \
887 "# "); \
888 } \
889 }
890#endif //if IPOPT_VERBOSITY == 0
891
892#endif
#define DBG_ASSERT(test)
Definition IpDebug.hpp:27
#define DECLARE_STD_EXCEPTION(__except_type)
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.
Templated class which stores one entry for the CachedResult class.
Class responsible for all message output.
Storing the reference count of all the smart pointers that currently reference it.
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...
VectorSpace base class, corresponding to the Vector base class.
Definition IpVector.hpp:473
VectorSpace(const VectorSpace &)
Copy constructor.
Index Dim() const
Accessor function for the dimension of the vectors of this type.
Definition IpVector.hpp:495
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:525
virtual ~VectorSpace()
Destructor.
Definition IpVector.hpp:485
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:863
Number Max() const
Returns the maximum value in the vector.
Definition IpVector.hpp:774
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:803
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:735
virtual void ElementWiseSgnImpl()=0
Replaces entries with sgn of the entry.
Number Min() const
Returns the minimum value in the vector.
Definition IpVector.hpp:784
Number cached_amax_
Definition IpVector.hpp:438
TaggedObject::Tag sum_cache_tag_
Definition IpVector.hpp:446
Vector()
Default constructor.
TaggedObject::Tag asum_cache_tag_
Definition IpVector.hpp:434
void ElementWiseMin(const Vector &x)
Element-wise min against entries in x.
Definition IpVector.hpp:744
virtual void ElementWiseMinImpl(const Vector &x)=0
Element-wise min against entries in x.
Number cached_min_
Definition IpVector.hpp:444
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:690
Vector(const Vector &)
Copy constructor.
TaggedObject::Tag nrm2_cache_tag_
Definition IpVector.hpp:431
CachedResults< Number > dot_cache_
Cache for dot products.
Definition IpVector.hpp:429
TaggedObject::Tag sumlogs_cache_tag_
Definition IpVector.hpp:449
virtual Number MaxImpl() const =0
Max value in the vector.
void ElementWiseMultiply(const Vector &x)
Element-wise multiplication .
Definition IpVector.hpp:713
void Axpy(Number alpha, const Vector &x)
Add the multiple alpha of vector x to this vector (DAXPY)
Definition IpVector.hpp:609
const SmartPtr< const VectorSpace > owner_space_
Vector Space.
Definition IpVector.hpp:424
bool HasValidNumbers() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
Definition IpVector.hpp:848
Vector * MakeNewCopy() const
Create new Vector of the same type and copy the data over.
Definition IpVector.hpp:555
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:640
virtual Number SumLogsImpl() const =0
Sum of logs of entries in the vector.
Number cached_sum_
Definition IpVector.hpp:447
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:766
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:837
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:550
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:680
void ElementWiseDivide(const Vector &x)
Element-wise division .
Definition IpVector.hpp:705
virtual void ScalImpl(Number alpha)=0
Scales the vector by scalar alpha (DSCAL)
TaggedObject::Tag amax_cache_tag_
Definition IpVector.hpp:437
Number cached_asum_
Definition IpVector.hpp:435
void Copy(const Vector &x)
Copy the data of the vector x into this vector (DCOPY).
Definition IpVector.hpp:563
Index Dim() const
Dimension of the Vector.
Definition IpVector.hpp:858
void Set(Number alpha)
Set each element in the vector to the scalar alpha.
Definition IpVector.hpp:696
virtual void ElementWiseSelectImpl(const Vector &x)=0
Element-wise selection .
Number cached_sumlogs_
Definition IpVector.hpp:450
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:660
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:670
void AddOneVector(Number a, const Vector &v1, Number c)
Add one vector, y = a * v1 + c * y.
Definition IpVector.hpp:794
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:760
Number Asum() const
Computes the 1-norm of this vector (DASUM)
Definition IpVector.hpp:650
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:440
Number FracToBound(const Vector &delta, Number tau) const
Fraction to the boundary parameter.
Definition IpVector.hpp:815
void ElementWiseReciprocal()
Reciprocates the entries in the vector.
Definition IpVector.hpp:729
void ElementWiseSelect(const Vector &x)
Element-wise selection .
Definition IpVector.hpp:721
Number cached_max_
Definition IpVector.hpp:441
void ElementWiseAbs()
Absolute values of the entries in the vector.
Definition IpVector.hpp:753
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:529
Number cached_nrm2_
Definition IpVector.hpp:432
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:452
virtual void ElementWiseSqrtImpl()=0
Take elementwise square-root of the elements of the vector.
TaggedObject::Tag min_cache_tag_
Definition IpVector.hpp:443
Number Dot(const Vector &x) const
Computes inner product of vector x with this (DDOT)
Definition IpVector.hpp:618
#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)
ipindex Index
Type of all indices of vectors, matrices etc.
Definition IpTypes.hpp:20
EJournalCategory
Category Selection Enum.
EJournalLevel
Print Level Enum.
T Max(T a, T b)
Definition IpUtils.hpp:23
T Min(T a, T b)
Definition IpUtils.hpp:53
ipnumber Number
Type of all numbers.
Definition IpTypes.hpp:17