Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_sumofweights.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONSUMOFWEIGHTS_H
2#define FSTCRITERIONSUMOFWEIGHTS_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include "error.hpp"
53#include "global.hpp"
54#include "criterion.hpp"
55
56/*============== Template parameter type naming conventions ==============
57--------- Numeric types: -------------------------------------------------
58DATATYPE - data sample values - usually real numbers (but may be integers
59 in text processing etc.)
60REALTYPE - must be real numbers - for representing intermediate results of
61 calculations like mean, covariance etc.
62IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
63 extent depends on numbers of samples in data
64DIMTYPE - index values for enumeration of features (dimensions), or classes (not
65 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
66 but be aware of expressions like _classes*_features*_features !
67 in linearized representations of feature matrices for all classes
68BINTYPE - feature selection marker type - represents ca. <10 different feature
69 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
70RETURNTYPE - criterion value: real value, but may be extended in future to support
71 multiple values
72--------- Class types: ---------------------------------------------------
73SUBSET - class of class type Subset
74CLASSIFIER - class implementing interface defined in abstract class Classifier
75EVALUATOR - class implementing interface defined in abstract class Sequential_Step
76DISTANCE - class implementing interface defined in abstract class Distance
77DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
78INTERVALCONTAINER - class of class type Data_Intervaller
79CONTAINER - STL container of class type Data_Interval
80========================================================================== */
81
82namespace FST {
83
96template<class RETURNTYPE, typename DIMTYPE, class SUBSET>
97class Criterion_Sum_Of_Weights : public Clonable_Impl<Criterion_Sum_Of_Weights<RETURNTYPE,DIMTYPE,SUBSET>,Criterion<RETURNTYPE,SUBSET> > { // adapter class
98public:
100 typedef std::shared_ptr<SUBSET> PSubset;
101 typedef std::shared_ptr<RETURNTYPE> PWeights;
102 Criterion_Sum_Of_Weights() : _noofweights(0) {notify("Criterion_Sum_Of_Weights constructor.");}
103 virtual ~Criterion_Sum_Of_Weights() {notify("Criterion_Sum_Of_Weights destructor.");}
104
110 virtual void initialize(const DIMTYPE noofweights, const PWeights weights) {_noofweights=noofweights; _weights=weights;}
113 virtual void initialize(const DIMTYPE noofweights, const RETURNTYPE weights[]);
114
118 virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override;
119
120
121
122 virtual std::ostream& print(std::ostream& os) const override {os << "Criterion_Sum_Of_Weights()"; return os;}
123private:
124 friend parent_impl;
126 Criterion_Sum_Of_Weights(const Criterion_Sum_Of_Weights& css);
127protected:
129 DIMTYPE _noofweights;
130};
131
132template<class RETURNTYPE, typename DIMTYPE, class SUBSET>
133Criterion_Sum_Of_Weights<RETURNTYPE,DIMTYPE,SUBSET>::Criterion_Sum_Of_Weights(const Criterion_Sum_Of_Weights& css) :
135{
136 notify("Criterion_Sum_Of_Weights copy-constructor.");
137 if(css._weights)
138 {
139 assert(css._noofweights>0);
140 _weights.reset(new RETURNTYPE[css._noofweights], std::default_delete<RETURNTYPE[]>());
141 for(DIMTYPE i=0;i<css._noofweights;i++)_weights.get()[i]=css._weights.get()[i];
142 }
143}
144
145template<class RETURNTYPE, typename DIMTYPE, class SUBSET>
146void Criterion_Sum_Of_Weights<RETURNTYPE,DIMTYPE,SUBSET>::initialize(const DIMTYPE noofweights, const RETURNTYPE weights[])
147{
148 assert(noofweights>0);
149 _noofweights=noofweights;
150 _weights.reset(new RETURNTYPE[_noofweights], std::default_delete<RETURNTYPE[]>());
151 for(DIMTYPE i=0;i<_noofweights;i++) _weights.get()[i]=weights[i];
152}
153
154
155
156template<class RETURNTYPE, typename DIMTYPE, class SUBSET>
158{
159 assert(_weights);
160 assert(sub);
161 assert(_noofweights==sub->get_n());
162
163 result=0;
164 for(DIMTYPE fidx : sub->selected_features()) result+=_weights.get()[fidx];
165 return true;
166}
167
168} // namespace
169#endif // FSTCRITERIONSUMOFWEIGHTS_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Returns sum of pre-specified feature weights for features in the evaluated subset.
Definition criterion_sumofweights.hpp:97
DIMTYPE _noofweights
number of entries in _weights; must equal the evaluated subset's get_n()
Definition criterion_sumofweights.hpp:129
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_sumofweights.hpp:100
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition criterion_sumofweights.hpp:122
std::shared_ptr< RETURNTYPE > PWeights
shared pointer to a raw array of noofweights per-feature weights
Definition criterion_sumofweights.hpp:101
PWeights _weights
per-feature weights, indexed by raw feature index (size _noofweights)
Definition criterion_sumofweights.hpp:128
Clonable_Impl< Criterion_Sum_Of_Weights, Criterion< RETURNTYPE, SUBSET > > parent_impl
implements clone(); sharing/stateless_clone throw in Criterion
Definition criterion_sumofweights.hpp:99
virtual void initialize(const DIMTYPE noofweights, const PWeights weights)
Adopts an existing weight array: SHARES ownership with the caller, nothing is copied.
Definition criterion_sumofweights.hpp:110
virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override
Sums up the weights of the features selected in sub; always returns true. Weights are indexed by RAW ...
Definition criterion_sumofweights.hpp:157
Defines interface for feature selection criterion classes.
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.