Crevice  0.1
 All Classes Functions Variables Pages
crevice_1.cc
1 /*
2  * File: crevice.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 <cstdlib>
20 #include <iostream>
21 #include "TermAbs.h"
22 #include "TermConstant.h"
23 #include "TermVariable.h"
24 #include "TermSum.h"
25 #include "TermProduct.h"
26 #include "Problem.h"
27 #include <string>
28 #include <sstream>
29 #include <time.h>
30 using namespace std;
31 
32 const int n = 2;
33 const int w = 7;
34 
35 string var(string name, int index1, int index2, int index3) {
36  ostringstream os;
37  os << name << "["
38  << index1 << ","
39  << index2 << ","
40  << index3 << "]";
41  return os.str();
42 }
43 
44 int defineProblem(Problem *p, const int n, const int w) {
45  srand(time(NULL));
46  try {
47  TermVariable *x, *y, *z;
48  Term *obj, *t1, *t2, *t3, *t4, *t5, *t6;
49 
50  obj = TermConstant::create(p, 0.);
51 
52  for (int i = 0; i < n; i++) {
53  for (int k = 0; k < n; k++) {
54  for (int g = 0; g < n; g++) {
55  for (int h = 0; h < n; h++) {
56  for (int r = 0; r < n; r++) {
57  for (int s = 0; s < n; s++) {
58  t6 = TermConstant::create(p, 0.);
59  for (int t = 0; t < w; t++) {
60  x = TermVariable::create(p, var("x", g, h, t));
61  x->setLowerBound(-1);
62  x->setUpperBound(1);
63  y = TermVariable::create(p, var("y", r, s, t));
64  y->setLowerBound(-1);
65  y->setUpperBound(1);
66  z = TermVariable::create(p, var("z", i, k, t));
67  z->setLowerBound(-1);
68  z->setUpperBound(1);
69  t1 = TermProduct::create(p, z, x);
70  t2 = TermProduct::create(p, t1, y);
71  t6 = TermSum::create(p, t6, t2);
72  }
73  if (i == g && h == r && s == k) {
74  t3 = TermConstant::create(p, -1);
75  } else {
76  t3 = TermConstant::create(p, 0);
77  }
78  t4 = TermSum::create(p, t3, t6);
79  t5 = TermAbs::create(p, t4);
80  obj = TermSum::create(p, obj, t5);
81 
82  }
83  }
84  }
85  }
86  }
87  }
88  p->setObjectiveFunction(obj);
89 
90  } catch (ProblemMismatchException) {
91  cout << "Problem mismatch" << endl;
92  return EXIT_FAILURE;
93  }
94  return EXIT_SUCCESS;
95 }
96 
97 int initializeVariableValues(Problem *p, vector<double>* variableValues) {
98  int numberofVariables;
99  numberofVariables = p->numberOfVariables();
100  for (int i = 0; i < numberofVariables; i++) {
101  double value;
102  value = 1. - 2. * (double) rand() / (double) RAND_MAX;
103  variableValues->insert(variableValues->end(), value);
104  }
105 }
106 
107 int main() {
108  int ret;
109  Problem *p;
110  vector<double>* variableValues;
111  DoubleVector *hessian;
112 
113  srand(time(NULL));
114 
115  p = new Problem();
116 
117  ret = defineProblem(p, n, w);
118  if (ret != EXIT_SUCCESS) {
119  return ret;
120  }
121  cout << "Number of variables: " << p->numberOfVariables() << endl;
122  cout << "Number of terms: " << p->numberOfTerms() << endl;
123 
124 
125  variableValues = new vector<double>();
126  initializeVariableValues(p, variableValues);
127 
128  cout << p->getObjectiveFunction()->evaluate(variableValues) << endl;
129 
130  if (p->hessian(variableValues, &hessian)) {
131  cout << "Size of Hessian " << hessian->size() << endl;
132  delete hessian;
133  }
134 
135  cout << "Number of terms: " << p->numberOfTerms() << endl;
136 
137 
138  delete(variableValues);
139  delete p;
140 
141  return EXIT_SUCCESS;
142 }