| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : MatEncode.tol |
|---|
| 4 | // PURPOSE: Defines Package MatEncode |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | NameBlock MatEncode = |
|---|
| 9 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 10 | [[ |
|---|
| 11 | |
|---|
| 12 | #Require HashMap; |
|---|
| 13 | #Require MatQuery; |
|---|
| 14 | |
|---|
| 15 | Text _.autodoc.description = "Handling and storing generic features, " |
|---|
| 16 | "numerical or not, in an efficient way by means of encoded matrices."; |
|---|
| 17 | Text _.autodoc.url = ""; |
|---|
| 18 | Set _.autodoc.keys = [["encode","decode","category","feature"]]; |
|---|
| 19 | Set _.autodoc.authors = [[ |
|---|
| 20 | "vdebuen@tol-project.org" ]]; |
|---|
| 21 | |
|---|
| 22 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 23 | Class @Feature |
|---|
| 24 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 25 | { |
|---|
| 26 | Static Text _.autodoc.description = "Class for handling with abstract " |
|---|
| 27 | "features of arbitrary entities, numerical or not, that will be stored in " |
|---|
| 28 | "an external matrix with a row for each entity."; |
|---|
| 29 | //The name of the feature |
|---|
| 30 | Text _.name; |
|---|
| 31 | //The number of the feature in the full set of features |
|---|
| 32 | Real _.position; |
|---|
| 33 | |
|---|
| 34 | Real is.numeric(Real void); |
|---|
| 35 | |
|---|
| 36 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 37 | Static Text _.autodoc.member.get = "Returns the stored value corresponding " |
|---|
| 38 | "to this feature related to an individual entity."; |
|---|
| 39 | Anything get(Matrix store, Real entity) |
|---|
| 40 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 44 | Class @Feature.Numeric : @Feature |
|---|
| 45 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 46 | { |
|---|
| 47 | Static Text _.autodoc.description = "Class for handling with numerical " |
|---|
| 48 | "numerical features of arbitrary entities that will be stored in an " |
|---|
| 49 | "external matrix with a row for each entity."; |
|---|
| 50 | |
|---|
| 51 | Real is.numeric(Real void) { True }; |
|---|
| 52 | |
|---|
| 53 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 54 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 55 | "Class @Feature.Numeric"; |
|---|
| 56 | Static @Feature.Numeric New(Text name, Real position) |
|---|
| 57 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 58 | { |
|---|
| 59 | @Feature.Numeric aux = |
|---|
| 60 | [[ |
|---|
| 61 | Text _.name = name; |
|---|
| 62 | Real _.position = position |
|---|
| 63 | ]] |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 67 | Anything get(Matrix store, Real entity) |
|---|
| 68 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | { |
|---|
| 70 | MatDat(store, entity, _.position) |
|---|
| 71 | } |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 75 | Class @Feature.NonNumeric : @Feature |
|---|
| 76 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 77 | { |
|---|
| 78 | Static Text _.autodoc.description = "Class for handling with numerical " |
|---|
| 79 | "numerical features of arbitrary entities that will be stored in an " |
|---|
| 80 | "external matrix with a row for each entity. Here the stored value is the " |
|---|
| 81 | "index of the feature stored in an arbitrary domain."; |
|---|
| 82 | |
|---|
| 83 | //The set of valid values for this feature indexed by name |
|---|
| 84 | Set _.domain; |
|---|
| 85 | |
|---|
| 86 | Real is.numeric(Real void) { False }; |
|---|
| 87 | |
|---|
| 88 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 89 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 90 | "Class @Feature.NonNumeric"; |
|---|
| 91 | Static @Feature.NonNumeric New(Text name, Real position, Set domain) |
|---|
| 92 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 93 | { |
|---|
| 94 | @Feature.NonNumeric aux = |
|---|
| 95 | [[ |
|---|
| 96 | Text _.name = name; |
|---|
| 97 | Real _.position = position; |
|---|
| 98 | Set _.domain = domain |
|---|
| 99 | ]]; |
|---|
| 100 | Real SetIndexByName(aux::_.domain); |
|---|
| 101 | aux |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 105 | Anything get(Matrix store, Real entity) |
|---|
| 106 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | { |
|---|
| 108 | Real idx = MatDat(store, entity, _.position); |
|---|
| 109 | _.domain[idx] |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 113 | Real domain.index(Text name) |
|---|
| 114 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 115 | { |
|---|
| 116 | FindIndexByName(_.domain, name) |
|---|
| 117 | } |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 121 | Class @FeatureSet |
|---|
| 122 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 123 | { |
|---|
| 124 | //Stores and index by name a set of instances od @Feature |
|---|
| 125 | Set _.feature = Copy(Empty); |
|---|
| 126 | //In each row of this matrix are stored the values or indexes of features |
|---|
| 127 | //related to an individual entity |
|---|
| 128 | Matrix _.data = Rand(0,0,0,0); |
|---|
| 129 | |
|---|
| 130 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 131 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 132 | "Class @FeatureSet"; |
|---|
| 133 | Static @FeatureSet New(Real void) |
|---|
| 134 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 135 | { |
|---|
| 136 | @FeatureSet aux |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 140 | Static Text _.autodoc.member.add.feature.numeric = |
|---|
| 141 | "Adds a new numerical feature"; |
|---|
| 142 | Real add.feature.numeric(Text name) |
|---|
| 143 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 144 | { |
|---|
| 145 | HashMap::Append.Indexed( |
|---|
| 146 | _.feature, |
|---|
| 147 | @Feature.Numeric::New(name, Card(_.feature)+1), |
|---|
| 148 | name) |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 152 | Static Text _.autodoc.member.add.feature.nonnumeric= |
|---|
| 153 | "Adds a new non numerical feature"; |
|---|
| 154 | Real add.feature.nonnumeric(Text name, Set domain) |
|---|
| 155 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 156 | { |
|---|
| 157 | HashMap::Append.Indexed( |
|---|
| 158 | _.feature, |
|---|
| 159 | @Feature.NonNumeric::New(name, Card(_.feature)+1, domain), |
|---|
| 160 | name) |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 164 | Text _.autodoc.member.feature.index = "Returns the index of a feature or 0 " |
|---|
| 165 | "if it not exists"; |
|---|
| 166 | Real feature.index(Text name) |
|---|
| 167 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 168 | { |
|---|
| 169 | FindIndexByName(_.feature, name ) |
|---|
| 170 | }; |
|---|
| 171 | |
|---|
| 172 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 173 | Text _.autodoc.member.get = "Returns the value of a feature for an entity"; |
|---|
| 174 | Anything get(Real entity, |
|---|
| 175 | Anything feature) //Real position or Text name |
|---|
| 176 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 177 | { |
|---|
| 178 | @Feature f = _.feature[feature]; |
|---|
| 179 | f::get(_.data, entity) |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 183 | Text _.autodoc.member.get.all = "Returns a set with the values of all " |
|---|
| 184 | "features for an entity"; |
|---|
| 185 | Set get.all(Real entity) |
|---|
| 186 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 187 | { |
|---|
| 188 | EvalSet(_.feature, Anything get.all.cycle(@FeatureSet f) |
|---|
| 189 | { |
|---|
| 190 | f::get(_.data, entity) |
|---|
| 191 | }) |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 195 | Text _.autodoc.member.select.feature.with.value = |
|---|
| 196 | "Returns a column matrix with True in rows where given feature has " |
|---|
| 197 | "specified value"; |
|---|
| 198 | VMatrix select.feature.with.value( |
|---|
| 199 | Text feature.name, |
|---|
| 200 | Anything value.id) //Text value identifier in domain or Real value |
|---|
| 201 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 202 | { |
|---|
| 203 | Real num = _.feature.index(feature.name); |
|---|
| 204 | @Feature f = _.feature[num]; |
|---|
| 205 | Real value = If(f::is.numeric(0), |
|---|
| 206 | value.id, |
|---|
| 207 | f::_.domain.index(value.id)); |
|---|
| 208 | MatQuery::MatchRowsWithValue(SubCol(_.data,[[num]]),value) |
|---|
| 209 | }; |
|---|
| 210 | |
|---|
| 211 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 212 | Text _.autodoc.member.intersect.feature.with.value = |
|---|
| 213 | "Returns a column matrix with True in rows where given feature has " |
|---|
| 214 | "specified value"; |
|---|
| 215 | VMatrix intersect.feature.with.value( |
|---|
| 216 | Text feature.name, |
|---|
| 217 | Anything value.id, //Text value identifier in domain or Real value |
|---|
| 218 | VMatrix match) |
|---|
| 219 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 220 | { |
|---|
| 221 | And(match,select.feature.with.value(feature.name,value.id)) |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | ]]; |
|---|