20 #include "TermVariable.h"
26 this->hessian_ = NULL;
27 this->gradient_ = NULL;
28 this->objectiveFunction_ = NULL;
29 this->terms =
new TermSet();
30 this->variables_ = NULL;
31 this->variablesSet_ =
new VariableSet();
38 TermSet::iterator position;
39 for (position = this->terms->begin();
40 position != this->terms->end();
41 position = this->terms->begin()) {
42 Term *term = *position;
43 this->terms->erase(position);
46 delete this->hessian_;
47 delete this->gradient_;
48 delete this->variables_;
50 delete this->variablesSet_;
62 if (0 == this->terms->count(term)) {
64 this->terms->insert(term);
68 ret = *this->terms->find(term);
87 if (0 == this->variablesSet_->count(ret)) {
89 this->variablesSet_->insert(ret);
102 if (this->hessian_ != NULL) {
103 delete this->hessian_;
104 this->hessian_ = NULL;
106 if (this->gradient_ != NULL) {
107 delete this->gradient_;
108 this->gradient_ = NULL;
110 if (this->variables_ != NULL) {
111 delete this->variables_;
112 this->variables_ = NULL;
114 this->objectiveFunction_ = NULL;
115 this->objectiveFunction_ = ret;
124 return this->objectiveFunction_;
139 if (this->gradient_ == NULL) {
140 this->prepareGradient();
142 return this->gradient_;
150 if (this->hessian_ == NULL) {
151 this->prepareHessian();
153 return this->hessian_;
161 if (this->variables_ == NULL) {
164 return this->variables_;
175 DoubleVector** gradient) {
176 if (this->gradient_ == NULL) {
179 int size = this->gradient_->size();
180 *gradient =
new DoubleVector(size);
181 TermValueSet* termValues;
182 termValues =
new TermValueSet;
183 for (
int i = 0; i < size; i++) {
184 (**gradient)[i] = (*gradient_)[i]->evaluate(values, termValues);
198 DoubleVector **hessian) {
199 if (this->hessian_ == NULL) {
202 int size = this->hessian_->size();
203 *hessian =
new DoubleVector(size);
204 TermValueSet* termValues;
205 termValues =
new TermValueSet;
206 for (
int i = 0; i < size; i++) {
207 (**hessian)[i] = (*hessian_)[i]->evaluate(values, termValues);
218 return this->terms->size();
226 return this->variablesSet_->size();
238 *obj = this->objectiveFunction_->
evaluate(values);
246 bool Problem::prepareGradient() {
247 this->prepareVariables();
248 if (this->gradient_ == NULL) {
249 int size = this->variablesSet_->size();
250 this->gradient_ =
new TermPointerVector(size);
251 for (
int i = 0; i < size; i++) {
252 (*this->gradient_)[i] =
254 (*this->variables_)[i]);
264 bool Problem::prepareHessian() {
265 this->prepareGradient();
266 if (this->hessian_ == NULL) {
267 int size = this->variablesSet_->size();
268 this->hessian_ =
new TermPointerVector(size * size);
269 for (
int i = 0; i < size; i++) {
270 for (
int j = 0; j < size; j++) {
271 (*this->hessian_)[i + j * size] =
272 (*this->gradient_)[i]->differentiate(
273 (*this->variables_)[j]);
284 bool Problem::prepareVariables() {
285 if (this->variables_ == NULL) {
286 int size = this->variablesSet_->size();
287 VariableSet::iterator variableRowPosition;
288 this->variables_ =
new TermVariablePointerVector(size);
289 variableRowPosition = this->variablesSet_->begin();
290 for (
int i = 0; i < size; ++i) {
291 (*this->variables_)[i] = *variableRowPosition;
292 ++variableRowPosition;