1 #ifndef PROBLEM_H
2 #define PROBLEM_H
3 
4 #ifdef HAVE_CONFIG_H
5 #include "OPT++_config.h"
6 #endif
7 
8 #include <iostream>
9 #include <string>
10 #include <vector>
11 #include <dlfcn.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 #ifdef HAVE_STD
15 #include <cstdlib>
16 #else
17 #include <stdlib.h>
18 #endif
19 
20 #include "xercesc/dom/DOM.hpp"
21 #include "xercesc/util/PlatformUtils.hpp"
22 
23 #include "Opt.h"
24 #include "AppLauncher.h"
25 #include "VariableList.h"
26 #include "NLFAPP.h"
27 #include "BoundConstraint.h"
28 #include "NonLinearEquation.h"
29 #include "CompoundConstraint.h"
30 
31 using std::string;
32 
33 namespace OPTPP {
34 
35 typedef struct{
36 	int value;
37 	string msg;
38 } OptError;
39 
40 /**
41  * Class Problem is the parent for all the different types of solvers.
42  */
43 class Problem
44 {
45 	private:
46 		DOMElement* solverXML_;
47 		bool hasSubroutineXML_;
48 		bool hasParameterXML_;
49 		bool useApplicationOptimizer_;
50 		bool hasNumVar_;
51 		bool hasDerivOrder_;
52 		OptimizeClass * objfcn_;
53 		NLP0 * func_;
54 		int numVar_;
55 		int derivOrder_;
56 		DOMElement* subroutineXML_;
57 		DOMElement* applicationXML_;
58 		VariableList variables_;
59 
60 
61 
62 		AppLauncher launcher_;
63 
64 		vector<void *> loadedLibs_;
65 
66 		/**
67 		 * Dymanically loads the given Library function
68 		 * and returns int as a void pointer
69 		 */
70 		void * GetFunction(string libName, string funcName);
71 
72 		/**
73 		 * Closes all of the Libraries that have been dynamically Loaded
74 		 */
75 		void CloseLoadedLibs();
76 
77 	protected:
78 		// this needs to be set by each individual subclass
79 		DOMElement* parameterXML_;
80 
81 		/** Returns the number of Variables */
82 		int GetNumVar();
83 
84 		/** Reterns the order of Differentiation */
85 		int GetDerivOrder();
86 
87 		/**
88 		 * Returns the XML Element containing Subroutine and library info
89 		 */
90 		DOMElement* GetSubroutineXML();
91 
92 		/**
93 		 * Returns the XML Element Which contains the solver properites
94 		 */
95 		DOMElement* GetSolverXML();
96 
97 		/**
98 		 * Returns the XML Element containing the Algorithm parameters
99 		 */
100 		DOMElement* GetParameterXML();
101 
102 		/**
103 		 * Initially Retrieves the XML Element containing Algorithm parameters
104 		 * from the Solver XML
105 		 */
106 		virtual DOMElement* FindParameterXML() = 0;
107 
108 		/** Returns the non-differentiable function to be optimized */
109 		USERFCN0 GetUserFunction0();
110 
111 		/** Returns the first order differentiable function to be optimized */
112 		USERFCN1 GetUserFunction1();
113 
114 		/** Returns the second order differentiable function to be optimized */
115 		USERFCN2 GetUserFunction2();
116 
117 		/** Returns the initialization funciton for the Optimization */
118 		INITFCN GetInitFunction();
119 
120 		/**
121 		 * Returns the Application Launcher for Optimizing an external
122 		 * Function Evaluation
123 		 */
124 		AppLauncher * GetAppLauncher();
125 
126 		/**
127 		 * Sets the Algorithm Parameters for the Optimization run
128 		 */
129 		virtual void SetParameters(OptimizeClass* objfcn);
130 
131 		/**
132 		 * Gets a VariableList object containing the
133 		 * optimization  variables and related information.
134 		 */
135 		VariableList * GetVariables();
136 
137 		/**
138 		 * Gets a CompoundConstraint object based upon whether the
139 		 * optimization is unconstrained, bounded, or general
140 		 */
141 		CompoundConstraint * GetConstraints();
142 
143 		/**
144 		 * Creates an Optimizer that will optimize a dynamically loaded
145 		 * function from an external library
146 		 */
147 		virtual OptError CreateFunctionOptimizer(OptimizeClass * &objfcn, NLP0* &func) = 0;
148 
149 		/**
150 		 * Creates an Optimizer that will optimize a function run by an
151 		 * external Application
152 		 */
153 		virtual OptError CreateApplicationOptimizer(OptimizeClass * &objfcn, NLP0* &func) = 0;
154 
155 	public:
156 		/** constructor */
Problem(DOMElement * solverXML)157 		Problem(DOMElement* solverXML):
158 			solverXML_(solverXML),
159 			hasSubroutineXML_(false),
160 			hasParameterXML_(false),
161 			useApplicationOptimizer_(false),
162 			hasNumVar_(false),
163 			hasDerivOrder_(false)
164 		{;}
165 
166 		/* Destructor */
167 
~Problem()168 		virtual ~Problem(){}
169 
170 		/** Optimize the Function */
171 		OptError optimize();
172 
173 		/** Sets the XML that defines the Problem to be solved */
174 		void SetProblemXML(DOMElement* applicationXML);
175 };
176 
177 } // namespace OPTPP
178 
179 #endif
180