source: colin/trunk/colin/OptProblem.h @ 5786

Revision 5786, 4.6 KB checked in by wehart, 5 years ago (diff)

Update of source to include Acro copyright statement

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*  _________________________________________________________________________
2 *
3 *  Acro: A Common Repository for Optimizers
4 *  Copyright (c) 2008 Sandia Corporation.
5 *  This software is distributed under the BSD License.
6 *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7 *  the U.S. Government retains certain rights in this software.
8 *  For more information, see the README.txt file in the top Acro directory.
9 *  _________________________________________________________________________
10 */
11
12/**
13 * \file OptProblem.h
14 *
15 * Defines the colin::OptProblem class.
16 */
17
18#ifndef colin_OptProblem_h
19#define colin_OptProblem_h
20
21#include <acro_config.h>
22
23#include <colin/OptApplication_Base.h>
24#include <colin/OptProblemTraits.h>
25#include <colin/ContextMngr.h>
26
27namespace colin
28{
29
30template<typename ProblemT> class OptApplication;
31
32//============================================================================
33//============================================================================
34// Class OptProblem
35//============================================================================
36//============================================================================
37
38/**
39 *  The class \c OptProblem provides a class for managing the interface to all
40 *  optimization problems.   This class defines a letter-envelope idiom that
41 *  leaves the specific details of how the problem is represented to
42 *  an \c OptApplication object.
43 *
44 *  The methods for \c OptProblem define envelope operations.
45 *  The \c app method provides access to the application object.
46 *
47 *  NOTE: this letter/envelope idiom is somewhat different from a canonical
48 *  design in that the ApplicationManager class is responsible for
49 *  managing applications, and for translating applications between different
50 *  problem types.  Further, this class manages the memory for these objects.
51 */
52template <class ProblemT>
53class OptProblem
54{
55public:
56
57   /// Generic constructor
58   OptProblem()
59      : app(NULL),
60        app_handle()
61   {}
62
63   /// Generic constructor
64   OptProblem( OptApplication<ProblemT>* app_ )
65      : app(app_),
66        app_handle()
67   {
68      if ( app != NULL )
69         app_handle = app_->get_handle();
70   }
71
72   /// Copy constructor
73   template <class LProblemT>
74   OptProblem(const OptProblem<LProblemT>& rhs)
75   {
76      ( *this ) = rhs;
77   }
78
79
80   /// Returns true if there is no application handle.
81   bool empty() const
82   { return app == NULL; }
83
84   ///
85   operator ApplicationHandle() const
86   {
87      return app_handle;
88   }
89
90   /// Returns the application handle
91   OptApplication<ProblemT>* application()
92   {
93      assert( app_handle.object() == app );
94      return app;
95   }
96
97   /// Returns the application handle
98   const OptApplication<ProblemT>* application() const
99   {
100      assert( app_handle.object() == app );
101      return app;
102   }
103
104   /// Returns true if there is an application handle.
105   OptApplication<ProblemT>* operator->()
106   { return application(); }
107
108   /// Returns true if there is an application handle.
109   const OptApplication<ProblemT>* operator->() const
110   { return application(); }
111
112   /// Set the application object
113   void set_application(OptApplication<ProblemT>* new_app)
114   {
115      app = new_app;
116      if ( app == NULL )
117         app_handle = ApplicationHandle();
118      else
119         app_handle = app->get_handle();
120   }
121
122   template<typename LProblemT>
123   OptProblem<ProblemT>& operator=(const OptProblem<LProblemT>& rhs)
124   {
125      if ( rhs.empty() )
126      {
127         app = NULL;
128         app_handle = ApplicationHandle();
129      }
130      else
131      {
132         ProblemMngr().lexical_cast(rhs, *this);
133      }
134
135      return *this;
136   }
137
138   OptProblem<ProblemT>& operator=(const OptProblem<ProblemT>& rhs)
139   {
140      app = rhs.app;
141      app_handle = rhs.app_handle;
142      return *this;
143   }
144
145
146private:
147
148   /// The (derived) optimization application
149   OptApplication<ProblemT>* app;
150
151   /// A handle on the original application object (to help keep it in scope)
152   ApplicationHandle  app_handle;
153};
154
155} // namespace colin
156
157
158// This is a *very* unusual place to include a header file; however, it
159// is intentional.  There is a tight linkage between OptApplication and
160// OptProblem: both cause the other to be instantiated.  Any one who
161// includes one header MUST also include the other.  However, the order
162// in which they are included definitely matters (OptProblem must be
163// above OptApplication) -- and the error tossed by the compiler for
164// getting it wrong is not at all obvious.  By having both header files
165// include the other (and relying on the #ifdef guards), we can
166// guarantee that things will compile correctly.
167#include <colin/OptApplication.h>
168
169#endif
Note: See TracBrowser for help on using the repository browser.