Crevice  0.1
 All Classes Functions Variables Pages
Problem.cc
1 /*
2  * File: Problem.cc
3  *
4  * Copyright 2012 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "Problem.h"
20 #include "TermVariable.h"
21 
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();
32 }
33 
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);
44  delete term;
45  }
46  delete this->hessian_;
47  delete this->gradient_;
48  delete this->variables_;
49  delete this->terms;
50  delete this->variablesSet_;
51 }
52 
61  Term* ret = term;
62  if (0 == this->terms->count(term)) {
63  // Term not found
64  this->terms->insert(term);
65  term->setTermIndex(this->terms->size());
66  } else {
67  // Find equivalent term
68  ret = *this->terms->find(term);
69  if (ret != term) {
70  // Delete only if not identical term
71  delete term;
72  }
73  }
74  return ret;
75 }
76 
86  TermVariable* ret = static_cast<TermVariable *> (this->addTerm(variable));
87  if (0 == this->variablesSet_->count(ret)) {
88  // Variable not found
89  this->variablesSet_->insert(ret);
90  ret->setVariableIndex(this->variablesSet_->size() - 1);
91  }
92  return ret;
93 }
94 
101  Term* ret = this->addTerm(term);
102  if (this->hessian_ != NULL) {
103  delete this->hessian_;
104  this->hessian_ = NULL;
105  }
106  if (this->gradient_ != NULL) {
107  delete this->gradient_;
108  this->gradient_ = NULL;
109  }
110  if (this->variables_ != NULL) {
111  delete this->variables_;
112  this->variables_ = NULL;
113  }
114  this->objectiveFunction_ = NULL;
115  this->objectiveFunction_ = ret;
116  return ret;
117 }
118 
124  return this->objectiveFunction_;
125 }
126 
138 TermPointerVector* Problem::getGradient() {
139  if (this->gradient_ == NULL) {
140  this->prepareGradient();
141  }
142  return this->gradient_;
143 }
144 
149 TermPointerVector* Problem::getHessian() {
150  if (this->hessian_ == NULL) {
151  this->prepareHessian();
152  }
153  return this->hessian_;
154 }
155 
160 TermVariablePointerVector* Problem::getVariables() {
161  if (this->variables_ == NULL) {
162  prepareVariables();
163  }
164  return this->variables_;
165 }
166 
174 bool Problem::gradient(const DoubleVector* values,
175  DoubleVector** gradient) {
176  if (this->gradient_ == NULL) {
177  prepareGradient();
178  }
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);
185  }
186  delete termValues;
187  return true;
188 }
189 
197 bool Problem::hessian(const DoubleVector* values,
198  DoubleVector **hessian) {
199  if (this->hessian_ == NULL) {
200  prepareHessian();
201  }
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);
208  }
209  delete termValues;
210  return true;
211 }
212 
218  return this->terms->size();
219 }
220 
226  return this->variablesSet_->size();
227 }
228 
235 bool Problem::objective(const std::vector<double>* values,
236  double* obj) {
237 
238  *obj = this->objectiveFunction_->evaluate(values);
239  return false;
240 };
241 
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] =
253  this->objectiveFunction_->differentiate(
254  (*this->variables_)[i]);
255  }
256  }
257  return true;
258 }
259 
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]);
274  }
275  }
276  }
277  return true;
278 }
279 
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;
293  }
294  }
295  return true;
296 }