The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
nLinearInequation.h
Go to the documentation of this file.
1 /**
2  * @file nLinearInequation
3  * file name: nLinearInequation.h
4  * @author Betti Oesterholz
5  * @date 09.06.2010
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This header specifies functions for linear inequation.
11  * Copyright (C) @c GPL3 2010 Betti Oesterholz
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License (GPL) as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  *
27  * This header specifies functions for linear inequation.
28  */
29 /*
30 History:
31 09.06.2010 Oesterholz created
32 */
33 
34 #ifndef ___N_LINEAR_INEQUATION_H__
35 #define ___N_LINEAR_INEQUATION_H__
36 
37 #include "version.h"
38 
39 #include "cRangeFactor.h"
40 #include "cInequation.h"
41 #include "cLinearConstrainFix.h"
42 
43 #include <vector>
44 #include <set>
45 #include <ostream>
46 
47 using namespace std;
48 
49 
50 namespace fib{
51 
52 namespace algorithms{
53 
54 namespace nLinearInequation{
55 
56 
57 
58  /**
59  * This functions converts the given vector of inequation into a diagonal
60  * form. In this form ther exsists only one factor in the liniar formular
61  * (@see cLinearConstrainFix::vecFactors) in every inequation. For the
62  * first n inequation this factor is the n'th factor. For the remaining
63  * inequation, this factor is the last factor. (n is the number of
64  * factors @see cLinearConstrainFix::ulNumberOfFactors)
65  * The convertion is done by multiplying inequation with a number
66  * (@see cLinearConstrainFix::mult()) or adding two inequation of the
67  * vector of inequations (@see cLinearConstrainFix::operator+()).
68  *
69  * @param vecOfInequations a reference to the vector of inequation, which
70  * to convert into the triangle form (this is changed)
71  * @return a pointer to the converted vector of inequations
72  */
73  template <class tFactors>
74  vector< cLinearConstrainFix<tFactors> > & crateDiagonalForm(
75  vector< cLinearConstrainFix<tFactors> > & vecOfInequations );
76 
77  /**
78  * This function nulls all factors in the liniar formular
79  * (@see cLinearConstrainFix::vecFactors) which x values are constant.
80  * (Constant x values have a range size of 0, respectivly ther bounderies
81  * are equal.)
82  * The ranges for the x values are given (vecXRanges), wher the i'th
83  * factor range correspondents to the i'th factor in the liniar formular.
84  * The factors are nulled, by multiplying the factor in the liniar
85  * formular with it's x value (the value the range for the x -factor contains).
86  *
87  * @param vecInputInequations a reference to the vector with the
88  * inequations, which x -factors should be nulled
89  * @param vecXRanges a vector with the ranges of the x -factors
90  * @param setOpenAFactors a refernce to the set of x -factors, which can be
91  * nulled; from this set the nulled factors will be removed;
92  * the set contains the numbers of the x -factors (counting begins with 0)
93  * @return a reference to the vector with the inequations, which x
94  * factors wher nulled
95  */
96  template <class tFactors>
97  vector< cLinearConstrainFix<tFactors> > & nullConstantXFactors(
98  vector< cLinearConstrainFix<tFactors> > & vecInputInequations,
99  const vector< cRangeFactor<tFactors> > & vecXRanges,
100  set<unsigned long> & setOpenAFactors );
101 
102  /**
103  * This function nulls all constrain/ bounderie factors in the inequations
104  * (@see cLinearConstrainFix::vecBounderyFactors) which y values are constant.
105  * (Constant y values have a range size of 0, respectivly ther bounderies
106  * are equal.)
107  * The ranges for the y values are given (vecYRanges), wher the i'th
108  * factor range correspondents to the i'th constrain/ bounderie factor in
109  * the inequation.
110  * The factors are nulled, by multiplying the bounderie factor in the
111  * inequation with it's y value (the value the range of the y -factor contains).
112  *
113  * @param vecInputInequations a reference to the vector with the
114  * inequations, of which the y -factors should be nulled
115  * @param vecYRanges a vector with the ranges of the y -factors
116  * @return a reference to the vector with the inequations, which y
117  * -factors wher nulled
118  */
119  template <class tFactors> vector< cLinearConstrainFix<tFactors> > &
120  nullConstantYFactors( vector< cLinearConstrainFix<tFactors> > & vecInputInequations,
121  const vector< cRangeFactor<tFactors> > & vecYRanges );
122 
123  /**
124  * This function elemitats all nullfactors from the given vector of
125  * inequations. Every -factor, that is 0 in all inequations, will be
126  * removed from all inequations. Later factors will decrase ther position.
127  * It is possible to set one factor as the last factor in the inequations.
128  *
129  * @param vecInputInequations the vector of the inequation, wher to
130  * remove the nullfactors
131  * @param vecFactorXMappings the new x -factor (factors in the liniar
132  * formular) mappings, for the inequations;
133  * on position i in this vector stands the position, the i'th x -factor
134  * in the returned inequations, has in the given inequations
135  * vecInputInequations (returned[k].vecFactors[ i ] ==
136  * vecInputInequations[k].vecFactors[ vecFactorXMappings[i] ])
137  * @param vecFactorYMappings the new y -factor (bounderie factor)
138  * mappings, for the inequations;
139  * on position i in this vector stands the position, the i'th y -factor
140  * in the returned inequations, has in the given inequations
141  * vecInputInequations (returned[k].vecBounderyFactors[ i ] ==
142  * vecInputInequations[k].vecBounderyFactors[ vecFactorYMappings[i] ])
143  * @param uiLastFactor the x -factor (@see cLinearConstrainFix::vecFactors
144  * counting begins with 0) which should be last in the returned inequations
145  * @return a vector with the inequations wher the nullfactors are removed
146  */
147  template <class tFactors>
148  vector< cLinearConstrainFix<tFactors> > reduceNullFactors(
149  const vector< cLinearConstrainFix<tFactors> > & vecInputInequations,
150  vector< long > & vecFactorXMappings, vector< long > & vecFactorYMappings,
151  unsigned long uiLastFactor = 0 );
152 
153  /**
154  * This function converts the given @see cLinearConstrainFix inequations
155  * to an equivallent set of @see cInequation inequations.
156  * For every cLinearConstrainFix two cInequation will be crated.
157  * All factors, which are 0, will be eleminated
158  *
159  * vecBounderyFactors[0] * yu_0 + ... + vecBounderyFactors[uiNumberOfDataPoints-1] * yu_{uiNumberOfDataPoints-1}
160  * (bGreaterEqual ? <= : =>)
161  * constant + vecFactors[0] * x_0 + ... + vecFactors[uiNumberOfFactors-1] * x_[uiNumberOfFactors-1}
162  * (bGreaterEqual ? <= : =>)
163  * vecBounderyFactors[0] * yo_0 + ... + vecBounderyFactors[uiNumberOfDataPoints-1] * yo_{uiNumberOfDataPoints-1}
164  * =>
165  * -1 * constant <= vecFactors[0] * z_0 + ... + vecFactors[n] * z_n
166  * constant <= -1 * vecFactors[0] * z_0 - ... - vecFactors[n] * z_n
167  *
168  * Wher:
169  * - vecFactors[i] is the i'th factor not 0 of the cLinearConstrainFix
170  * ( vecBounderyFactors[0] or vecFactors[0] )
171  * - constant is the constant of the formular
172  *
173  * The identifiers (@see cInequation::vecIdentifiers) of the cInequation
174  * will be set to a positiv i (0 <= i), if the correspondending original
175  * factor, was the i'th factor for the inequation (x -factor,
176  * original.vecFactors[ i ] or vecOfInequations.vecFactors[ a ] with
177  * i = vecFactorXMappings[ a ] ).
178  * The identifiers will be set to a negativ i ( i < 0), if the
179  * correspondending original factor, was the (-1 * i - 1)'th bounderie
180  * factor (y -factor, original.vecBounderyFactors[ (-1 * i - 1) ] or
181  * vecOfInequations.vecBounderyFactors[ a ] with (-1 * i - 1) = vecFactorYMappings[ a ] ).
182  *
183  * @param vecOfInequations a vector with the (not original) inequations
184  * to convert
185  * @param vecFactorXMappings the mappings for the x -factors in
186  * vecOfInequations to the original x -factors
187  * @param vecFactorYMappings the mappings for the y -factors in
188  * vecOfInequations to the original y -factors
189  * @return a vector with the cInequation, which is equivallent to the
190  * given vecOfInequations
191  */
192  template <class tFactors>
193  vector<cInequation<tFactors> > fixInequationsToInequations(
194  const vector< cLinearConstrainFix<tFactors> > & vecOfInequations,
195  const vector< long > & vecFactorXMappings,
196  const vector< long > & vecFactorYMappings );
197 
198  /**
199  * This function reduce the ranges for the given factors with the help of
200  * the given formulars.
201  * The reduced ranges of the factors are subsets of the given ranges.
202  * The reduction is done by checking all inequations as long as ranges change.
203  * For the check of one inequation, all it's elements except one are
204  * maximized by setting the factors to one of ther bounderies.
205  * For the not set element it is checked, wich minimum value it can have,
206  * If it's range is to big for this value, the range will be adapted to
207  * the minimal range, wich is consistent with the this value element.
208  *
209  * example inequation:
210  * constant <= vecFactors[0] * z_0 + ... + vecFactors[n] * z_n
211  *
212  * (vecFactors[0] * z_0) is an element
213  *
214  * In ever inequation no more than one factor with a infinity boundery
215  * should exists.
216  *
217  * The factor for an element is identified by the element identifier
218  * (@see cInequation::vecIdentifiers).
219  * If the identifiers (@see cInequation::vecIdentifiers) of the factor
220  * is a positiv i (0 <= i) number, the correspondending original
221  * factor, is the i'th factor for the inequation (x -factor,
222  * vecXRanges[ i ] or vecOfInequations.vecFactors[ a ] with
223  * i = vecFactorXMappings[ a ] ).
224  * If the identifiers is a negativ i ( i < 0) number, the correspondending
225  * original factor, is the (-1 * i - 1)'th bounderie
226  * factor (y -factor, vecYRanges[ (-1 * i - 1) ] or
227  * vecOfInequations.vecBounderyFactors[ a ] with (-1 * i - 1) = vecFactorYMappings[ a ] ).
228  *
229  * @see fixInequationsToInequations()
230  * @param vecOfInequations the inequations, with which to reduce the
231  * given ranges of the factors
232  * @param vecXRanges the x -factors ranges to reduce
233  * @param vecYRanges the y -factors ranges to reduce
234  * @return true if a boudery was chaged, else false
235  */
236  template <class tFactors>
237  bool reduceBounderies( const vector<cInequation<tFactors> > & vecOfInequations,
238  vector< cRangeFactor<tFactors> > & vecXRanges, vector< cRangeFactor<tFactors> > & vecYRanges);
239 
240  /**
241  * This function implements a hillclimbing algorithm for finding good
242  * values for the x -factors.
243  * This function will try to minimize the error of the
244  * @see evalueErrorForFactors() function for the factors.
245  * It will choose random values for the factors which are in the
246  * correspondending range given with vecXRanges. Than it will check near
247  * points (factor values), to the best found point, if they are better.
248  * Beware: In the vecOfInequations just the i'the y -factor is 1, every
249  * other y -factor should have the value 0 .(@see evalueErrorForFactors)
250  * This is a implicite assumption, the y -factors won't be checked.
251  *
252  * @see evalueErrorForFactors
253  * @param vecOfInequations the inequations, for which to find the
254  * factor values
255  * @param vecXRanges the x -factors ranges, in which to find the factor
256  * values
257  * @param vecYRanges the y -factors ranges
258  * @param ulMaxIteration the maximal number of iterations / neibourpoints
259  * to check
260  * @return a pair:
261  * - the first element is the actual point/ values for the x -factors in
262  * the x ranges with the lowest found error
263  * - the second element is the error of this point
264  */
265  template <class tFactors>
266  pair< vector<tFactors>, tFactors> hillClimbingInRanges(
267  const vector< cInequation<tFactors> > & vecOfInequations,
268  const vector< cRangeFactor<tFactors> > & vecXRanges,
269  const vector< cRangeFactor<tFactors> > & vecYRanges,
270  unsigned long ulMaxIteration = 256 * 256 );
271 
272  /**
273  * This function will evalue the error for the given x -factors
274  * vecActualPoint.
275  * For this the factors are inserted into the given inequations
276  * and it is checkt, if the evalued value is outside the given
277  * y -factor bounderies, if so the difference will be added to the error.
278  * Beware: In the vecOfInequations just the i'the y -factor is 1, every
279  * other y -factor should have the value 0 .
280  * This is a implicite assumption, the y -factors won't be checked.
281  *
282  * @param vecActualPoint the x -factors values, for which to evalue the error
283  * @param vecOfInequations the inequations for which to evalue the error
284  * for the vecActualPoint
285  * @param vecYRanges the y -factors ranges
286  * @return the error of the vecActualPoint
287  */
288  template <class tFactors>
289  tFactors evalueErrorForFactors( const vector<tFactors> & vecActualPoint,
290  const vector< cLinearConstrainFix<tFactors> > & vecOfInequations,
291  const vector< cRangeFactor<tFactors> > & vecYRanges );
292 
293  /**
294  * This function prints the given inequations.
295  * It simply calls @see cPolyConstrainFix::print() for all given
296  * inequations.
297  *
298  * @param vecOfInequations a vector with the inequations to output
299  * @param outputStream a stream, wher to print the inequations to
300  */
301  template <class tFactors> void printInequations(
302  vector< cLinearConstrainFix<tFactors> > vecOfInequations,
303  ostream & outputStream );
304 
305 
306  /**
307  * This function solves the given vector of inequiations.
308  *
309  * @param vecOfInequationsInput the vector of linear inequiations to solve
310  * @return a vector with the range factors, in which contain the factors
311  * which solve the given linear inequiations
312  */
313  template <class tY> vector< cRangeFactor<tY> > solve(
314  const vector< cLinearConstrainFix<tY> > & vecOfInequationsInput );
315 
316 };//end namespace nLinearInequation
317 };//end namespace algorithms
318 };//end namespace fib
319 
320 //include template implementation
321 #include "../src/nLinearInequation.cpp"
322 
323 
324 #endif //___N_LINEAR_INEQUATION_H__