| 1 |
/* OTMulti.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2005-2025 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* pb 2005/06/11 the very beginning of computational bidirectional multi-level OT |
| 21 |
* pb 2006/05/16 guarded against cells with many violations |
| 22 |
* pb 2006/05/17 draw disharmonies above tableau |
| 23 |
* pb 2007/05/19 decision strategies |
| 24 |
* pb 2007/08/12 wchar |
| 25 |
* pb 2007/10/01 leak and constraint plasticity |
| 26 |
* pb 2007/10/01 can write as encoding |
| 27 |
* pb 2007/11/14 drawTableau: corrected direction of arrows for positive satisfactions |
| 28 |
* pb 2008/04/14 OTMulti_getConstraintIndexFromName |
| 29 |
* pb 2009/03/18 modern enums |
| 30 |
* pb 2010/06/05 corrected colours |
| 31 |
* pb 2011/03/01 multiple update rules; more decision strategies |
| 32 |
* pb 2011/03/29 C++ |
| 33 |
* pb 2011/04/27 Melder_debug 41 and 42 |
| 34 |
* pb 2011/05/15 prevented zero sums of probabilities in MaxEnt |
| 35 |
*/ |
| 36 |
|
| 37 |
#include "OTMulti.h" |
| 38 |
|
| 39 |
#include "oo_DESTROY.h" |
| 40 |
#include "OTMulti_def.h" |
| 41 |
#include "oo_COPY.h" |
| 42 |
#include "OTMulti_def.h" |
| 43 |
#include "oo_EQUAL.h" |
| 44 |
#include "OTMulti_def.h" |
| 45 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 46 |
#include "OTMulti_def.h" |
| 47 |
#include "oo_WRITE_BINARY.h" |
| 48 |
#include "OTMulti_def.h" |
| 49 |
#include "oo_READ_BINARY.h" |
| 50 |
#include "OTMulti_def.h" |
| 51 |
#include "oo_DESCRIPTION.h" |
| 52 |
#include "OTMulti_def.h" |
| 53 |
|
| 54 |
void structOTMulti :: v1_info () { |
| 55 |
structDaata :: v1_info (); |
| 56 |
integer numberOfViolations = 0; |
| 57 |
for (integer icand = 1; icand <= our numberOfCandidates; icand ++) |
| 58 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) |
| 59 |
numberOfViolations += our candidates [icand]. marks [icons]; |
| 60 |
MelderInfo_writeLine (U"Decision strategy: ", kOTGrammar_decisionStrategy_getText (decisionStrategy)); |
| 61 |
MelderInfo_writeLine (U"Number of constraints: ", our numberOfConstraints); |
| 62 |
MelderInfo_writeLine (U"Number of candidates: ", our numberOfCandidates); |
| 63 |
MelderInfo_writeLine (U"Number of violation marks: ", numberOfViolations); |
| 64 |
} |
| 65 |
|
| 66 |
void structOTMulti :: v1_writeText (MelderFile file) { |
| 67 |
MelderFile_write (file, U"\n<", kOTGrammar_decisionStrategy_getText (decisionStrategy), |
| 68 |
U">\n", leak, U" ! leak\n", our numberOfConstraints, U" constraints"); |
| 69 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) { |
| 70 |
OTConstraint constraint = & our constraints [icons]; |
| 71 |
MelderFile_write (file, U"\n\t\""); |
| 72 |
for (const char32 *p = & constraint -> name [0]; *p != U'\0'; p ++) { |
| 73 |
if (*p == U'\"') |
| 74 |
MelderFile_writeCharacter (file, U'\"'); // double any quotes within quotes |
| 75 |
MelderFile_writeCharacter (file, *p); |
| 76 |
} |
| 77 |
MelderFile_write (file, U"\" ", constraint -> ranking, |
| 78 |
U" ", constraint -> disharmony, U" ", constraint -> plasticity); |
| 79 |
} |
| 80 |
MelderFile_write (file, U"\n\n", our numberOfCandidates, U" candidates"); |
| 81 |
for (integer icand = 1; icand <= our numberOfCandidates; icand ++) { |
| 82 |
OTCandidate candidate = & our candidates [icand]; |
| 83 |
MelderFile_write (file, U"\n\t\""); |
| 84 |
for (const char32 *p = & candidate -> string [0]; *p != U'\0'; p ++) { |
| 85 |
if (*p == U'\"') |
| 86 |
MelderFile_writeCharacter (file, U'\"'); // double any quotes within quotes |
| 87 |
MelderFile_writeCharacter (file, *p); |
| 88 |
} |
| 89 |
MelderFile_write (file, U"\" "); |
| 90 |
for (integer icons = 1; icons <= candidate -> numberOfConstraints; icons ++) |
| 91 |
MelderFile_write (file, U" ", candidate -> marks [icons]); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void OTMulti_checkIndex (OTMulti me) { |
| 96 |
if (my index.size != 0) |
| 97 |
return; |
| 98 |
my index = to_INTVEC (my numberOfConstraints); |
| 99 |
OTMulti_sort (me); |
| 100 |
} |
| 101 |
|
| 102 |
void structOTMulti :: v1_readText (MelderReadText text, int formatVersion) { |
| 103 |
OTMulti_Parent :: v1_readText (text, formatVersion); |
| 104 |
if (formatVersion >= 1) { |
| 105 |
try { |
| 106 |
decisionStrategy = (kOTGrammar_decisionStrategy) texgete8 (text, (enum_generic_getValue) kOTGrammar_decisionStrategy_getValue); |
| 107 |
} catch (MelderError) { |
| 108 |
Melder_throw (U"Decision strategy not read."); |
| 109 |
} |
| 110 |
} |
| 111 |
if (formatVersion >= 2) { |
| 112 |
try { |
| 113 |
leak = texgetr64 (text); |
| 114 |
} catch (MelderError) { |
| 115 |
Melder_throw (U"Trying to read leak."); |
| 116 |
} |
| 117 |
} |
| 118 |
if ((our numberOfConstraints = texgeti32 (text)) < 1) |
| 119 |
Melder_throw (U"No constraints."); |
| 120 |
our constraints = newvectorzero <structOTConstraint> (our numberOfConstraints); |
| 121 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) { |
| 122 |
OTConstraint constraint = & our constraints [icons]; |
| 123 |
constraint -> name = texgetw16 (text); |
| 124 |
constraint -> ranking = texgetr64 (text); |
| 125 |
constraint -> disharmony = texgetr64 (text); |
| 126 |
if (formatVersion < 2) { |
| 127 |
constraint -> plasticity = 1.0; |
| 128 |
} else { |
| 129 |
try { |
| 130 |
constraint -> plasticity = texgetr64 (text); |
| 131 |
} catch (MelderError) { |
| 132 |
Melder_throw (U"Plasticity of constraint ", icons, U" not read."); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if ((numberOfCandidates = texgeti32 (text)) < 1) |
| 137 |
Melder_throw (U"No candidates."); |
| 138 |
our candidates = newvectorzero <structOTCandidate> (numberOfCandidates); |
| 139 |
for (integer icand = 1; icand <= numberOfCandidates; icand ++) { |
| 140 |
OTCandidate candidate = & our candidates [icand]; |
| 141 |
candidate -> string = texgetw16 (text); |
| 142 |
candidate -> numberOfConstraints = our numberOfConstraints; // redundancy, needed for writing binary |
| 143 |
candidate -> marks = raw_INTVEC (candidate -> numberOfConstraints); |
| 144 |
for (integer icons = 1; icons <= candidate -> numberOfConstraints; icons ++) |
| 145 |
candidate -> marks [icons] = texgeti16 (text); |
| 146 |
} |
| 147 |
OTMulti_checkIndex (this); |
| 148 |
} |
| 149 |
|
| 150 |
Thing_implement (OTMulti, Daata, 2); |
| 151 |
|
| 152 |
integer OTMulti_getConstraintIndexFromName (OTMulti me, conststring32 name) { |
| 153 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 154 |
if (Melder_equ (my constraints [icons]. name.get(), name)) |
| 155 |
return icons; |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
void OTMulti_sort (OTMulti me) { |
| 160 |
std::sort (my index.begin(), my index.end(), |
| 161 |
[me] (integer icons, integer jcons) { |
| 162 |
OTConstraint ci = & my constraints [icons], cj = & my constraints [jcons]; |
| 163 |
/* |
| 164 |
Sort primarily by disharmony. |
| 165 |
*/ |
| 166 |
if (ci -> disharmony > cj -> disharmony) |
| 167 |
return true; |
| 168 |
if (ci -> disharmony < cj -> disharmony) |
| 169 |
return false; |
| 170 |
/* |
| 171 |
Tied constraints are sorted alphabetically. |
| 172 |
*/ |
| 173 |
return str32cmp (my constraints [icons]. name.get(), my constraints [jcons]. name.get()) < 0; |
| 174 |
} |
| 175 |
); |
| 176 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 177 |
const OTConstraint constraint = & my constraints [my index [icons]]; |
| 178 |
constraint -> tiedToTheLeft = ( icons > 1 && |
| 179 |
my constraints [my index [icons - 1]]. disharmony == constraint -> disharmony ); |
| 180 |
constraint -> tiedToTheRight = ( icons < my numberOfConstraints && |
| 181 |
my constraints [my index [icons + 1]]. disharmony == constraint -> disharmony ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void OTMulti_newDisharmonies (OTMulti me, double evaluationNoise) { |
| 186 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 187 |
const OTConstraint constraint = & my constraints [icons]; |
| 188 |
constraint -> disharmony = constraint -> ranking + NUMrandomGauss (0, evaluationNoise); |
| 189 |
} |
| 190 |
OTMulti_sort (me); |
| 191 |
} |
| 192 |
|
| 193 |
int OTMulti_compareCandidates (OTMulti me, integer icand1, integer icand2) { |
| 194 |
INTVEC marks1 = my candidates [icand1]. marks.get(); |
| 195 |
INTVEC marks2 = my candidates [icand2]. marks.get(); |
| 196 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 197 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 198 |
integer numberOfMarks1 = marks1 [my index [icons]]; |
| 199 |
integer numberOfMarks2 = marks2 [my index [icons]]; |
| 200 |
/* |
| 201 |
Count tied constraints as one. |
| 202 |
*/ |
| 203 |
while (my constraints [my index [icons]]. tiedToTheRight) { |
| 204 |
icons ++; |
| 205 |
numberOfMarks1 += marks1 [my index [icons]]; |
| 206 |
numberOfMarks2 += marks2 [my index [icons]]; |
| 207 |
} |
| 208 |
if (numberOfMarks1 < numberOfMarks2) |
| 209 |
return -1; // candidate 1 is better than candidate 2 |
| 210 |
if (numberOfMarks1 > numberOfMarks2) |
| 211 |
return +1; // candidate 2 is better than candidate 1 |
| 212 |
} |
| 213 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 214 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY) |
| 215 |
{ |
| 216 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 217 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 218 |
disharmony1 += my constraints [icons]. disharmony * marks1 [icons]; |
| 219 |
disharmony2 += my constraints [icons]. disharmony * marks2 [icons]; |
| 220 |
} |
| 221 |
if (disharmony1 < disharmony2) |
| 222 |
return -1; // candidate 1 is better than candidate 2 |
| 223 |
if (disharmony1 > disharmony2) |
| 224 |
return +1; // candidate 2 is better than candidate 1 |
| 225 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) { |
| 226 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 227 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 228 |
if (my constraints [icons]. disharmony > 0.0) { |
| 229 |
disharmony1 += my constraints [icons]. disharmony * marks1 [icons]; |
| 230 |
disharmony2 += my constraints [icons]. disharmony * marks2 [icons]; |
| 231 |
} |
| 232 |
} |
| 233 |
if (disharmony1 < disharmony2) |
| 234 |
return -1; // candidate 1 is better than candidate 2 |
| 235 |
if (disharmony1 > disharmony2) |
| 236 |
return +1; // candidate 2 is better than candidate 1 |
| 237 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 238 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY) |
| 239 |
{ |
| 240 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 241 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 242 |
disharmony1 += exp (my constraints [icons]. disharmony) * marks1 [icons]; |
| 243 |
disharmony2 += exp (my constraints [icons]. disharmony) * marks2 [icons]; |
| 244 |
} |
| 245 |
if (disharmony1 < disharmony2) |
| 246 |
return -1; // candidate 1 is better than candidate 2 |
| 247 |
if (disharmony1 > disharmony2) |
| 248 |
return +1; // candidate 2 is better than candidate 1 |
| 249 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG) { |
| 250 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 251 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 252 |
double constraintDisharmony = std::max (my constraints [icons]. disharmony, 1.0); |
| 253 |
disharmony1 += constraintDisharmony * marks1 [icons]; |
| 254 |
disharmony2 += constraintDisharmony * marks2 [icons]; |
| 255 |
} |
| 256 |
if (disharmony1 < disharmony2) |
| 257 |
return -1; // candidate 1 is better than candidate 2 |
| 258 |
if (disharmony1 > disharmony2) |
| 259 |
return +1; // candidate 2 is better than candidate 1 |
| 260 |
} else { |
| 261 |
Melder_crash (U"Unimplemented decision strategy."); |
| 262 |
} |
| 263 |
return 0; // none of the comparisons found a difference between the two candidates; hence, they are equally good |
| 264 |
} |
| 265 |
|
| 266 |
int OTMulti_candidateMatches (OTMulti me, integer icand, conststring32 form1, conststring32 form2) { |
| 267 |
conststring32 string = my candidates [icand]. string.get(); |
| 268 |
return (form1 [0] == U'\0' || str32str (string, form1)) && (form2 [0] == U'\0' || str32str (string, form2)); |
| 269 |
} |
| 270 |
|
| 271 |
static void _OTMulti_fillInHarmonies (OTMulti me, conststring32 form1, conststring32 form2) { |
| 272 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) return; |
| 273 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 274 |
OTCandidate candidate = & my candidates [icand]; |
| 275 |
INTVEC marks = candidate -> marks.get(); |
| 276 |
double disharmony = 0.0; |
| 277 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 278 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY) |
| 279 |
{ |
| 280 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 281 |
disharmony += my constraints [icons]. disharmony * marks [icons]; |
| 282 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 283 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY) |
| 284 |
{ |
| 285 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 286 |
disharmony += exp (my constraints [icons]. disharmony) * marks [icons]; |
| 287 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) { |
| 288 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 289 |
if (my constraints [icons]. disharmony > 0.0) |
| 290 |
disharmony += my constraints [icons]. disharmony * marks [icons]; |
| 291 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG) { |
| 292 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 293 |
double constraintDisharmony = std::max (my constraints [icons]. disharmony, 1.0); |
| 294 |
disharmony += constraintDisharmony * marks [icons]; |
| 295 |
} |
| 296 |
} else { |
| 297 |
Melder_crash (U"_OTMulti_fillInHarmonies: unimplemented decision strategy."); |
| 298 |
} |
| 299 |
candidate -> harmony = - disharmony; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static void _OTMulti_fillInProbabilities (OTMulti me, conststring32 form1, conststring32 form2) { |
| 304 |
double maximumHarmony = -1e308; |
| 305 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 306 |
OTCandidate candidate = & my candidates [icand]; |
| 307 |
if (candidate -> harmony > maximumHarmony) |
| 308 |
maximumHarmony = candidate -> harmony; |
| 309 |
} |
| 310 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 311 |
OTCandidate candidate = & my candidates [icand]; |
| 312 |
candidate -> probability = exp (candidate -> harmony - maximumHarmony); |
| 313 |
Melder_assert (candidate -> probability >= 0.0 && candidate -> probability <= 1.0); |
| 314 |
} |
| 315 |
double sumOfProbabilities = 0.0; |
| 316 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 317 |
OTCandidate candidate = & my candidates [icand]; |
| 318 |
sumOfProbabilities += candidate -> probability; |
| 319 |
} |
| 320 |
Melder_assert (sumOfProbabilities > 0.0); // Because at least one of them is 1.0. |
| 321 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 322 |
OTCandidate candidate = & my candidates [icand]; |
| 323 |
candidate -> probability /= sumOfProbabilities; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
class MelderError_OTMulti_NoMatchingCandidate: public MelderError {}; |
| 328 |
|
| 329 |
integer OTMulti_getWinner (OTMulti me, conststring32 form1, conststring32 form2) { |
| 330 |
try { |
| 331 |
integer icand_best = 0; |
| 332 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY || |
| 333 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY || |
| 334 |
my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) |
| 335 |
{ |
| 336 |
_OTMulti_fillInHarmonies (me, form1, form2); |
| 337 |
_OTMulti_fillInProbabilities (me, form1, form2); |
| 338 |
const double cutOff = NUMrandomUniform (0.0, 1.0); |
| 339 |
longdouble sumOfProbabilities = 0.0; |
| 340 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 341 |
sumOfProbabilities += my candidates [icand]. probability; |
| 342 |
if (sumOfProbabilities > cutOff) { |
| 343 |
icand_best = icand; |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
} else { |
| 348 |
integer numberOfBestCandidates = 0; |
| 349 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 350 |
if (icand_best == 0) { |
| 351 |
icand_best = icand; |
| 352 |
numberOfBestCandidates = 1; |
| 353 |
} else { |
| 354 |
int comparison = OTMulti_compareCandidates (me, icand, icand_best); |
| 355 |
if (comparison == -1) { |
| 356 |
icand_best = icand; // the current candidate is the unique best candidate found so far |
| 357 |
numberOfBestCandidates = 1; |
| 358 |
} else if (comparison == 0) { |
| 359 |
numberOfBestCandidates += 1; // the current candidate is equally good as the best found before |
| 360 |
/* |
| 361 |
Give all candidates that are equally good an equal chance to become the winner. |
| 362 |
*/ |
| 363 |
if (Melder_debug == 41) |
| 364 |
icand_best = icand_best; // keep first |
| 365 |
else if (Melder_debug == 42) |
| 366 |
icand_best = icand; // take last |
| 367 |
else if (NUMrandomUniform (0.0, numberOfBestCandidates) < 1.0) // default: take random |
| 368 |
icand_best = icand; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
if (icand_best == 0) { |
| 374 |
Melder_appendError (U"The forms ", form1, U" and ", form2, U" do not match any candidate."); |
| 375 |
throw MelderError_OTMulti_NoMatchingCandidate (); // BUG: NYI |
| 376 |
} |
| 377 |
return icand_best; |
| 378 |
} catch (MelderError) { |
| 379 |
Melder_throw (me, U": winner not determined."); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
static void OTMulti_modifyRankings (OTMulti me, integer iwinner, integer iloser, |
| 384 |
kOTGrammar_rerankingStrategy updateRule, |
| 385 |
double plasticity, double relativePlasticityNoise) |
| 386 |
{ |
| 387 |
bool *grammarHasChanged = nullptr; // to be implemented |
| 388 |
bool warnIfStalled = false; // to be implemented |
| 389 |
if (iwinner == iloser) |
| 390 |
return; |
| 391 |
OTCandidate winner = & my candidates [iwinner], loser = & my candidates [iloser]; |
| 392 |
const double step = relativePlasticityNoise == 0.0 ? plasticity : NUMrandomGauss (plasticity, relativePlasticityNoise * plasticity); |
| 393 |
bool multiplyStepByNumberOfViolations = |
| 394 |
my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 395 |
my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || |
| 396 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY || |
| 397 |
my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG || |
| 398 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 399 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY || |
| 400 |
my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY; |
| 401 |
if (Melder_debug != 0) { |
| 402 |
/* |
| 403 |
Perhaps override the standard update rule. |
| 404 |
*/ |
| 405 |
if (Melder_debug == 26) |
| 406 |
multiplyStepByNumberOfViolations = false; // OT-GLA |
| 407 |
else if (Melder_debug == 27) |
| 408 |
multiplyStepByNumberOfViolations = true; // HG-GLA |
| 409 |
} |
| 410 |
if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ONE) { |
| 411 |
const integer icons = NUMrandomInteger (1, my numberOfConstraints); |
| 412 |
const OTConstraint constraint = & my constraints [icons]; |
| 413 |
double constraintStep = step * constraint -> plasticity; |
| 414 |
const integer winnerMarks = winner -> marks [icons]; |
| 415 |
const integer loserMarks = loser -> marks [icons]; |
| 416 |
if (loserMarks > winnerMarks) { |
| 417 |
if (multiplyStepByNumberOfViolations) |
| 418 |
constraintStep *= loserMarks - winnerMarks; |
| 419 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 420 |
if (grammarHasChanged) |
| 421 |
*grammarHasChanged = true; |
| 422 |
} |
| 423 |
if (winnerMarks > loserMarks) { |
| 424 |
if (multiplyStepByNumberOfViolations) |
| 425 |
constraintStep *= winnerMarks - loserMarks; |
| 426 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 427 |
if (grammarHasChanged) |
| 428 |
*grammarHasChanged = true; |
| 429 |
} |
| 430 |
} else if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ALL) { |
| 431 |
bool changed = false; |
| 432 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 433 |
const OTConstraint constraint = & my constraints [icons]; |
| 434 |
double constraintStep = step * constraint -> plasticity; |
| 435 |
const integer winnerMarks = winner -> marks [icons]; |
| 436 |
const integer loserMarks = loser -> marks [icons]; |
| 437 |
if (loserMarks > winnerMarks) { |
| 438 |
if (multiplyStepByNumberOfViolations) |
| 439 |
constraintStep *= loserMarks - winnerMarks; |
| 440 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 441 |
changed = true; |
| 442 |
} |
| 443 |
if (winnerMarks > loserMarks) { |
| 444 |
if (multiplyStepByNumberOfViolations) |
| 445 |
constraintStep *= winnerMarks - loserMarks; |
| 446 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 447 |
changed = true; |
| 448 |
} |
| 449 |
} |
| 450 |
if (changed && my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG) { |
| 451 |
longdouble sumOfWeights = 0.0; |
| 452 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 453 |
sumOfWeights += my constraints [icons]. ranking; |
| 454 |
const double averageWeight = double (sumOfWeights) / my numberOfConstraints; |
| 455 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 456 |
my constraints [icons]. ranking -= averageWeight; |
| 457 |
} |
| 458 |
if (grammarHasChanged) |
| 459 |
*grammarHasChanged = changed; |
| 460 |
} else if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ALL_SKIPPABLE) { |
| 461 |
bool changed = false; |
| 462 |
integer winningConstraints = 0, losingConstraints = 0; |
| 463 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 464 |
const integer winnerMarks = winner -> marks [icons]; |
| 465 |
const integer loserMarks = loser -> marks [icons]; |
| 466 |
if (loserMarks > winnerMarks) |
| 467 |
losingConstraints ++; |
| 468 |
if (winnerMarks > loserMarks) |
| 469 |
winningConstraints ++; |
| 470 |
} |
| 471 |
if (winningConstraints != 0) for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 472 |
const OTConstraint constraint = & my constraints [icons]; |
| 473 |
double constraintStep = step * constraint -> plasticity; |
| 474 |
const integer winnerMarks = winner -> marks [icons]; |
| 475 |
const integer loserMarks = loser -> marks [icons]; |
| 476 |
if (loserMarks > winnerMarks) { |
| 477 |
if (multiplyStepByNumberOfViolations) |
| 478 |
constraintStep *= loserMarks - winnerMarks; |
| 479 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 480 |
changed = true; |
| 481 |
} |
| 482 |
if (winnerMarks > loserMarks) { |
| 483 |
if (multiplyStepByNumberOfViolations) |
| 484 |
constraintStep *= winnerMarks - loserMarks; |
| 485 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 486 |
changed = true; |
| 487 |
} |
| 488 |
} |
| 489 |
if (changed && my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG) { |
| 490 |
longdouble sumOfWeights = 0.0; |
| 491 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 492 |
sumOfWeights += my constraints [icons]. ranking; |
| 493 |
const double averageWeight = double (sumOfWeights) / my numberOfConstraints; |
| 494 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 495 |
my constraints [icons]. ranking -= averageWeight; |
| 496 |
} |
| 497 |
if (grammarHasChanged) *grammarHasChanged = changed; |
| 498 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_UNCANCELLED) { |
| 499 |
integer winningConstraints = 0, losingConstraints = 0; |
| 500 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 501 |
const integer winnerMarks = winner -> marks [icons]; |
| 502 |
const integer loserMarks = loser -> marks [icons]; |
| 503 |
if (loserMarks > winnerMarks) |
| 504 |
losingConstraints ++; |
| 505 |
if (winnerMarks > loserMarks) |
| 506 |
winningConstraints ++; |
| 507 |
} |
| 508 |
if (winningConstraints != 0) { |
| 509 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 510 |
const OTConstraint constraint = & my constraints [icons]; |
| 511 |
double constraintStep = step * constraint -> plasticity; |
| 512 |
const integer winnerMarks = winner -> marks [icons]; |
| 513 |
const integer loserMarks = loser -> marks [icons]; |
| 514 |
if (loserMarks > winnerMarks) { |
| 515 |
if (multiplyStepByNumberOfViolations) |
| 516 |
constraintStep *= loserMarks - winnerMarks; |
| 517 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) / losingConstraints; |
| 518 |
//constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) * winningConstraints; |
| 519 |
if (grammarHasChanged) |
| 520 |
*grammarHasChanged = true; |
| 521 |
} |
| 522 |
if (winnerMarks > loserMarks) { |
| 523 |
if (multiplyStepByNumberOfViolations) |
| 524 |
constraintStep *= winnerMarks - loserMarks; |
| 525 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / winningConstraints; |
| 526 |
//constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * losingConstraints; |
| 527 |
if (grammarHasChanged) |
| 528 |
*grammarHasChanged = true; |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL) { |
| 533 |
integer winningConstraints = 0, losingConstraints = 0; |
| 534 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 535 |
const integer winnerMarks = winner -> marks [icons]; |
| 536 |
const integer loserMarks = loser -> marks [icons]; |
| 537 |
if (loserMarks > 0) |
| 538 |
losingConstraints ++; |
| 539 |
if (winnerMarks > 0) |
| 540 |
winningConstraints ++; |
| 541 |
} |
| 542 |
if (winningConstraints != 0) for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 543 |
const OTConstraint constraint = & my constraints [icons]; |
| 544 |
double constraintStep = step * constraint -> plasticity; |
| 545 |
const integer winnerMarks = winner -> marks [icons]; |
| 546 |
const integer loserMarks = loser -> marks [icons]; |
| 547 |
if (loserMarks > 0) { |
| 548 |
if (multiplyStepByNumberOfViolations) |
| 549 |
constraintStep *= loserMarks - winnerMarks; |
| 550 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) / losingConstraints; |
| 551 |
if (grammarHasChanged) |
| 552 |
*grammarHasChanged = true; |
| 553 |
} |
| 554 |
if (winnerMarks > 0) { |
| 555 |
if (multiplyStepByNumberOfViolations) |
| 556 |
constraintStep *= winnerMarks - loserMarks; |
| 557 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / winningConstraints; |
| 558 |
if (grammarHasChanged) |
| 559 |
*grammarHasChanged = true; |
| 560 |
} |
| 561 |
} |
| 562 |
} else if (updateRule == kOTGrammar_rerankingStrategy::EDCD || updateRule == kOTGrammar_rerankingStrategy::EDCD_WITH_VACATION) { |
| 563 |
/* |
| 564 |
Determine the crucial winner mark. |
| 565 |
*/ |
| 566 |
double pivotRanking; |
| 567 |
bool equivalent = true; |
| 568 |
integer icons = 1; |
| 569 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 570 |
const integer winnerMarks = winner -> marks [my index [icons]]; // order is important, so indirect |
| 571 |
const integer loserMarks = loser -> marks [my index [icons]]; |
| 572 |
if (loserMarks < winnerMarks) |
| 573 |
break; |
| 574 |
if (loserMarks > winnerMarks) |
| 575 |
equivalent = false; |
| 576 |
} |
| 577 |
if (icons > my numberOfConstraints) { // completed the loop? |
| 578 |
if (warnIfStalled && ! equivalent) |
| 579 |
Melder_warning (U"Correct output is harmonically bounded (by having strict superset violations as compared to the learner's output)! EDCD stalls.\n" |
| 580 |
U"Correct output: ", loser -> string.get(), U"\nLearner's output: ", winner -> string.get()); |
| 581 |
return; |
| 582 |
} |
| 583 |
/* |
| 584 |
Determine the stratum into which some constraints will be demoted. |
| 585 |
*/ |
| 586 |
pivotRanking = my constraints [my index [icons]]. ranking; |
| 587 |
if (updateRule == kOTGrammar_rerankingStrategy::EDCD_WITH_VACATION) { |
| 588 |
integer numberOfConstraintsToDemote = 0; |
| 589 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 590 |
const integer winnerMarks = winner -> marks [icons]; |
| 591 |
const integer loserMarks = loser -> marks [icons]; |
| 592 |
if (loserMarks > winnerMarks) { |
| 593 |
OTConstraint constraint = & my constraints [icons]; |
| 594 |
if (constraint -> ranking >= pivotRanking) |
| 595 |
numberOfConstraintsToDemote += 1; |
| 596 |
} |
| 597 |
} |
| 598 |
if (numberOfConstraintsToDemote > 0) { |
| 599 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 600 |
const OTConstraint constraint = & my constraints [icons]; |
| 601 |
if (constraint -> ranking < pivotRanking) { |
| 602 |
constraint -> ranking -= numberOfConstraintsToDemote * step * constraint -> plasticity; |
| 603 |
if (grammarHasChanged) |
| 604 |
*grammarHasChanged = true; |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
/* |
| 610 |
Demote all the uniquely violated constraints in the loser |
| 611 |
that have rankings not lower than the pivot. |
| 612 |
*/ |
| 613 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 614 |
integer numberOfConstraintsDemoted = 0; |
| 615 |
const integer winnerMarks = winner -> marks [my index [icons]]; // for the vacation version, the order is important, so indirect |
| 616 |
const integer loserMarks = loser -> marks [my index [icons]]; |
| 617 |
if (loserMarks > winnerMarks) { |
| 618 |
OTConstraint constraint = & my constraints [my index [icons]]; |
| 619 |
double constraintStep = step * constraint -> plasticity; |
| 620 |
if (constraint -> ranking >= pivotRanking) { |
| 621 |
numberOfConstraintsDemoted += 1; |
| 622 |
constraint -> ranking = pivotRanking - numberOfConstraintsDemoted * constraintStep; // this preserves the order of the demotees |
| 623 |
if (grammarHasChanged) |
| 624 |
*grammarHasChanged = true; |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
} else if (updateRule == kOTGrammar_rerankingStrategy::DEMOTION_ONLY) { |
| 629 |
/* |
| 630 |
Determine the crucial loser mark. |
| 631 |
*/ |
| 632 |
integer crucialLoserMark; |
| 633 |
OTConstraint offendingConstraint; |
| 634 |
integer icons = 1; |
| 635 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 636 |
const integer winnerMarks = winner -> marks [my index [icons]]; // order is important, so indirect |
| 637 |
const integer loserMarks = loser -> marks [my index [icons]]; |
| 638 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 639 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 640 |
if (loserMarks < winnerMarks) { |
| 641 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 642 |
Melder_throw (U"Demotion-only learning step: Loser wins! Should never happen."); |
| 643 |
} else { |
| 644 |
// do nothing; the whole demotion-only idea does not really apply very well to non-OT decision strategies |
| 645 |
} |
| 646 |
} |
| 647 |
if (loserMarks > winnerMarks) |
| 648 |
break; |
| 649 |
} |
| 650 |
if (icons > my numberOfConstraints) { // completed the loop? |
| 651 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 652 |
Melder_throw (U"(OTGrammar_step:) Loser equals correct candidate: loser \"", |
| 653 |
loser -> string.get(), U"\", winner \"", winner -> string.get(), U"\"."); |
| 654 |
} else { |
| 655 |
// do nothing |
| 656 |
} |
| 657 |
} else { |
| 658 |
crucialLoserMark = icons; |
| 659 |
/* |
| 660 |
Demote the highest uniquely violated constraint in the loser. |
| 661 |
*/ |
| 662 |
offendingConstraint = & my constraints [my index [crucialLoserMark]]; |
| 663 |
const double constraintStep = step * offendingConstraint -> plasticity; |
| 664 |
offendingConstraint -> ranking -= constraintStep; |
| 665 |
if (grammarHasChanged) |
| 666 |
*grammarHasChanged = true; |
| 667 |
} |
| 668 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGHEST_DOWN) { |
| 669 |
bool changed = false; |
| 670 |
integer numberOfUp = 0; |
| 671 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 672 |
const integer winnerMarks = winner -> marks [icons]; |
| 673 |
const integer loserMarks = loser -> marks [icons]; |
| 674 |
if (winnerMarks > loserMarks) |
| 675 |
numberOfUp ++; |
| 676 |
} |
| 677 |
if (numberOfUp > 0) { |
| 678 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 679 |
const OTConstraint constraint = & my constraints [icons]; |
| 680 |
double constraintStep = step * constraint -> plasticity; |
| 681 |
const integer winnerMarks = winner -> marks [icons]; |
| 682 |
const integer loserMarks = loser -> marks [icons]; |
| 683 |
if (winnerMarks > loserMarks) { |
| 684 |
if (multiplyStepByNumberOfViolations) |
| 685 |
constraintStep *= winnerMarks - loserMarks; |
| 686 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / numberOfUp; |
| 687 |
} |
| 688 |
} |
| 689 |
integer winnerMarks = 0, loserMarks = 0; |
| 690 |
integer icons = 1; |
| 691 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 692 |
winnerMarks = winner -> marks [my index [icons]]; // order is important, so indirect |
| 693 |
loserMarks = loser -> marks [my index [icons]]; |
| 694 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 695 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 696 |
if (loserMarks < winnerMarks) { |
| 697 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 698 |
Melder_throw (U"Demotion-only learning step: Loser wins! Should never happen."); |
| 699 |
} else { |
| 700 |
// do nothing; the whole demotion-only idea does not really apply very well to non-OT decision strategies |
| 701 |
} |
| 702 |
} |
| 703 |
if (loserMarks > winnerMarks) |
| 704 |
break; |
| 705 |
} |
| 706 |
if (icons > my numberOfConstraints) { // completed the loop? |
| 707 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 708 |
Melder_throw (U"(OTGrammar_step:) Loser equals correct candidate: loser \"", |
| 709 |
loser -> string.get(), U"\", winner \"", winner -> string.get(), U"\"."); |
| 710 |
} else { |
| 711 |
// do nothing |
| 712 |
} |
| 713 |
} else { |
| 714 |
const integer crucialLoserMark = icons; |
| 715 |
/* |
| 716 |
Demote the highest uniquely violated constraint in the loser. |
| 717 |
*/ |
| 718 |
const OTConstraint offendingConstraint = & my constraints [my index [crucialLoserMark]]; |
| 719 |
double constraintStep = step * offendingConstraint -> plasticity; |
| 720 |
if (multiplyStepByNumberOfViolations) |
| 721 |
constraintStep *= winnerMarks - loserMarks; |
| 722 |
offendingConstraint -> ranking -= /*numberOfUp **/ constraintStep * (1.0 - offendingConstraint -> ranking * my leak); |
| 723 |
} |
| 724 |
} |
| 725 |
if (grammarHasChanged) |
| 726 |
*grammarHasChanged = changed; |
| 727 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGHEST_DOWN_2012) { |
| 728 |
bool changed = false; |
| 729 |
integer numberOfUp = 0; |
| 730 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 731 |
const integer winnerMarks = winner -> marks [icons]; |
| 732 |
const integer loserMarks = loser -> marks [icons]; |
| 733 |
if (winnerMarks > loserMarks) |
| 734 |
numberOfUp ++; |
| 735 |
} |
| 736 |
if (/*true ||*/ numberOfUp > 0) { |
| 737 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 738 |
const OTConstraint constraint = & my constraints [icons]; |
| 739 |
double constraintStep = step * constraint -> plasticity; |
| 740 |
const integer winnerMarks = winner -> marks [icons]; |
| 741 |
const integer loserMarks = loser -> marks [icons]; |
| 742 |
if (winnerMarks > loserMarks) { |
| 743 |
if (multiplyStepByNumberOfViolations) |
| 744 |
constraintStep *= winnerMarks - loserMarks; |
| 745 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / (numberOfUp + 1); |
| 746 |
} |
| 747 |
} |
| 748 |
integer winnerMarks = 0, loserMarks = 0; |
| 749 |
integer icons = 1; |
| 750 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 751 |
winnerMarks = winner -> marks [my index [icons]]; /* Order is important, so indirect. */ |
| 752 |
loserMarks = loser -> marks [my index [icons]]; |
| 753 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 754 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 755 |
if (loserMarks < winnerMarks) { |
| 756 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 757 |
Melder_throw (U"Demotion-only learning step: Loser wins! Should never happen."); |
| 758 |
} else { |
| 759 |
// do nothing; the whole demotion-only idea does not really apply very well to non-OT decision strategies |
| 760 |
} |
| 761 |
} |
| 762 |
if (loserMarks > winnerMarks) |
| 763 |
break; |
| 764 |
} |
| 765 |
if (icons > my numberOfConstraints) { // completed the loop? |
| 766 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 767 |
Melder_throw (U"(OTGrammar_step:) Loser equals correct candidate: loser \"", |
| 768 |
loser -> string.get(), U"\", winner \"", winner -> string.get(), U"\"."); |
| 769 |
} else { |
| 770 |
// do nothing |
| 771 |
} |
| 772 |
} else { |
| 773 |
const integer crucialLoserMark = icons; |
| 774 |
/* |
| 775 |
Demote the highest uniquely violated constraint in the loser. |
| 776 |
*/ |
| 777 |
const OTConstraint offendingConstraint = & my constraints [my index [crucialLoserMark]]; |
| 778 |
double constraintStep = step * offendingConstraint -> plasticity; |
| 779 |
if (multiplyStepByNumberOfViolations) |
| 780 |
constraintStep *= winnerMarks - loserMarks; |
| 781 |
offendingConstraint -> ranking -= /*numberOfUp **/ constraintStep * (1.0 - offendingConstraint -> ranking * my leak); |
| 782 |
} |
| 783 |
} |
| 784 |
if (grammarHasChanged) |
| 785 |
*grammarHasChanged = changed; |
| 786 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGH_DOWN) { |
| 787 |
integer numberOfDown = 0, numberOfUp = 0, lowestDemotableConstraint = 0; |
| 788 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 789 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 790 |
const integer loserMarks = loser -> marks [my index [icons]]; |
| 791 |
if (loserMarks < winnerMarks) { |
| 792 |
numberOfUp ++; |
| 793 |
} else if (loserMarks > winnerMarks) { |
| 794 |
if (numberOfUp == 0) { |
| 795 |
numberOfDown ++; |
| 796 |
lowestDemotableConstraint = icons; |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
if (numberOfUp > 0) { |
| 801 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 802 |
const integer constraintIndex = my index [icons]; |
| 803 |
const OTConstraint constraint = & my constraints [constraintIndex]; |
| 804 |
double constraintStep = step * constraint -> plasticity; |
| 805 |
const integer winnerMarks = winner -> marks [constraintIndex]; // the order is important, therefore indirect |
| 806 |
const integer loserMarks = loser -> marks [constraintIndex]; |
| 807 |
if (my constraints [constraintIndex]. tiedToTheRight) |
| 808 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 809 |
if (loserMarks < winnerMarks) { |
| 810 |
if (multiplyStepByNumberOfViolations) |
| 811 |
constraintStep *= winnerMarks - loserMarks; |
| 812 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * numberOfDown / (numberOfUp + 0.0); |
| 813 |
} else if (loserMarks > winnerMarks) { |
| 814 |
if (icons <= lowestDemotableConstraint) { |
| 815 |
if (multiplyStepByNumberOfViolations) |
| 816 |
constraintStep *= loserMarks - winnerMarks; |
| 817 |
constraint -> ranking -= constraintStep * (1.0 - constraint -> ranking * my leak); |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
if (grammarHasChanged) |
| 822 |
*grammarHasChanged = true; |
| 823 |
} |
| 824 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGH_DOWN_2012) { |
| 825 |
integer numberOfDown = 0, numberOfUp = 0, lowestDemotableConstraint = 0; |
| 826 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 827 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 828 |
const integer loserMarks = loser -> marks [my index [icons]]; |
| 829 |
if (loserMarks < winnerMarks) { |
| 830 |
numberOfUp ++; |
| 831 |
} else if (loserMarks > winnerMarks) { |
| 832 |
if (numberOfUp == 0) { |
| 833 |
numberOfDown ++; |
| 834 |
lowestDemotableConstraint = icons; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
if (numberOfUp > 0) { |
| 839 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 840 |
const integer constraintIndex = my index [icons]; |
| 841 |
const OTConstraint constraint = & my constraints [constraintIndex]; |
| 842 |
double constraintStep = step * constraint -> plasticity; |
| 843 |
const integer winnerMarks = winner -> marks [constraintIndex]; // the order is important, therefore indirect |
| 844 |
const integer loserMarks = loser -> marks [constraintIndex]; |
| 845 |
if (my constraints [constraintIndex]. tiedToTheRight) |
| 846 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 847 |
if (loserMarks < winnerMarks) { |
| 848 |
if (multiplyStepByNumberOfViolations) |
| 849 |
constraintStep *= winnerMarks - loserMarks; |
| 850 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * numberOfDown / (numberOfUp + 1.0); |
| 851 |
} else if (loserMarks > winnerMarks) { |
| 852 |
if (icons <= lowestDemotableConstraint) { |
| 853 |
if (multiplyStepByNumberOfViolations) |
| 854 |
constraintStep *= loserMarks - winnerMarks; |
| 855 |
constraint -> ranking -= constraintStep * (1.0 - constraint -> ranking * my leak); |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
if (grammarHasChanged) |
| 860 |
*grammarHasChanged = true; |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
int OTMulti_learnOne (OTMulti me, conststring32 form1, conststring32 form2, |
| 866 |
enum kOTGrammar_rerankingStrategy updateRule, int direction, double plasticity, double relativePlasticityNoise) |
| 867 |
{ |
| 868 |
integer iloser = OTMulti_getWinner (me, form1, form2); |
| 869 |
if (direction & OTMulti_LEARN_FORWARD) { |
| 870 |
if (Melder_debug == 47) OTMulti_newDisharmonies (me, 2.0); |
| 871 |
integer iwinner = OTMulti_getWinner (me, form1, U""); |
| 872 |
if (! (Melder_debug == 47) || ! OTMulti_candidateMatches (me, iwinner, form2, U"")) |
| 873 |
OTMulti_modifyRankings (me, iwinner, iloser, updateRule, plasticity, relativePlasticityNoise); |
| 874 |
} |
| 875 |
if (direction & OTMulti_LEARN_BACKWARD) { |
| 876 |
if (Melder_debug == 47) OTMulti_newDisharmonies (me, 2.0); |
| 877 |
integer iwinner = OTMulti_getWinner (me, form2, U""); |
| 878 |
if (! (Melder_debug == 47) || ! OTMulti_candidateMatches (me, iwinner, form1, U"")) |
| 879 |
OTMulti_modifyRankings (me, iwinner, iloser, updateRule, plasticity, relativePlasticityNoise); |
| 880 |
} |
| 881 |
return 1; |
| 882 |
} |
| 883 |
|
| 884 |
static autoTable OTMulti_createHistory (OTMulti me, integer storeHistoryEvery, integer numberOfData) |
| 885 |
{ |
| 886 |
try { |
| 887 |
integer numberOfSamplingPoints = numberOfData / storeHistoryEvery; // e.g. 0, 20, 40, ... |
| 888 |
autoTable thee = Table_createWithoutColumnNames (1 + numberOfSamplingPoints, 3 + my numberOfConstraints); |
| 889 |
Table_renameColumn_e (thee.get(), 1, U"Datum"); |
| 890 |
Table_renameColumn_e (thee.get(), 2, U"Form1"); |
| 891 |
Table_renameColumn_e (thee.get(), 3, U"Form2"); |
| 892 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 893 |
Table_renameColumn_e (thee.get(), 3 + icons, my constraints [icons]. name.get()); |
| 894 |
Table_setNumericValue (thee.get(), 1, 1, 0); |
| 895 |
Table_setStringValue (thee.get(), 1, 2, U"(initial)"); |
| 896 |
Table_setStringValue (thee.get(), 1, 3, U"(initial)"); |
| 897 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 898 |
Table_setNumericValue (thee.get(), 1, 3 + icons, my constraints [icons]. ranking); |
| 899 |
return thee; |
| 900 |
} catch (MelderError) { |
| 901 |
Melder_throw (me, U": history not created."); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
static void OTMulti_updateHistory (OTMulti me, Table thee, integer storeHistoryEvery, integer idatum, conststring32 form1, conststring32 form2) |
| 906 |
{ |
| 907 |
try { |
| 908 |
if (idatum % storeHistoryEvery == 0) { |
| 909 |
integer irow = 1 + idatum / storeHistoryEvery; |
| 910 |
Table_setNumericValue (thee, irow, 1, idatum); |
| 911 |
Table_setStringValue (thee, irow, 2, form1); |
| 912 |
Table_setStringValue (thee, irow, 3, form2); |
| 913 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 914 |
Table_setNumericValue (thee, irow, 3 + icons, my constraints [icons]. ranking); |
| 915 |
} |
| 916 |
} catch (MelderError) { |
| 917 |
Melder_throw (me, U": history not updated."); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
void OTMulti_PairDistribution_learn (OTMulti me, PairDistribution thee, double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, int direction, |
| 923 |
double initialPlasticity, integer replicationsPerPlasticity, double plasticityDecrement, |
| 924 |
integer numberOfPlasticities, double relativePlasticityNoise, integer storeHistoryEvery, autoTable *history_out) |
| 925 |
{ |
| 926 |
integer idatum = 0, numberOfData = numberOfPlasticities * replicationsPerPlasticity; |
| 927 |
try { |
| 928 |
double plasticity = initialPlasticity; |
| 929 |
autoMelderMonitor monitor (U"Learning from partial pairs..."); |
| 930 |
autoTable history; |
| 931 |
if (storeHistoryEvery) |
| 932 |
history = OTMulti_createHistory (me, storeHistoryEvery, numberOfData); |
| 933 |
for (integer iplasticity = 1; iplasticity <= numberOfPlasticities; iplasticity ++) { |
| 934 |
for (integer ireplication = 1; ireplication <= replicationsPerPlasticity; ireplication ++) { |
| 935 |
conststring32 form1, form2; |
| 936 |
PairDistribution_peekPair (thee, & form1, & form2); |
| 937 |
++ idatum; |
| 938 |
if (monitor.graphics() && idatum % (numberOfData / 400 + 1) == 0) { |
| 939 |
integer numberOfDrawnConstraints = my numberOfConstraints < 14 ? my numberOfConstraints : 14; |
| 940 |
if (numberOfDrawnConstraints > 0) { |
| 941 |
longdouble sumOfRankings = 0.0; |
| 942 |
for (integer icons = 1; icons <= numberOfDrawnConstraints; icons ++) |
| 943 |
sumOfRankings += my constraints [icons]. ranking; |
| 944 |
double meanRanking = double (sumOfRankings) / numberOfDrawnConstraints; |
| 945 |
Graphics_beginMovieFrame (monitor.graphics(), nullptr); |
| 946 |
Graphics_setWindow (monitor.graphics(), 0.0, numberOfData, meanRanking - 50.0, meanRanking + 50.0); |
| 947 |
for (integer icons = 1; icons <= numberOfDrawnConstraints; icons ++) { |
| 948 |
Graphics_setGrey (monitor.graphics(), (double) icons / numberOfDrawnConstraints); |
| 949 |
Graphics_line (monitor.graphics(), |
| 950 |
idatum, my constraints [icons]. ranking, |
| 951 |
idatum, my constraints [icons]. ranking + 1); |
| 952 |
} |
| 953 |
Graphics_endMovieFrame (monitor.graphics(), 0.0); |
| 954 |
} |
| 955 |
} |
| 956 |
try { |
| 957 |
Melder_monitor ((double) idatum / numberOfData, |
| 958 |
U"Processing partial pair ", idatum, U" out of ", numberOfData, |
| 959 |
U":\n ", form1, U" ", form2); |
| 960 |
} catch (MelderError) { |
| 961 |
if (history_out) |
| 962 |
*history_out = history.move(); // so that we can inspect |
| 963 |
throw; |
| 964 |
} |
| 965 |
OTMulti_newDisharmonies (me, evaluationNoise); |
| 966 |
try { |
| 967 |
OTMulti_learnOne (me, form1, form2, updateRule, direction, plasticity, relativePlasticityNoise); |
| 968 |
} catch (MelderError) { |
| 969 |
if (history) |
| 970 |
OTMulti_updateHistory (me, history.get(), storeHistoryEvery, idatum, form1, form2); |
| 971 |
throw; |
| 972 |
} |
| 973 |
if (history) |
| 974 |
OTMulti_updateHistory (me, history.get(), storeHistoryEvery, idatum, form1, form2); |
| 975 |
} |
| 976 |
plasticity *= plasticityDecrement; |
| 977 |
} |
| 978 |
if (history_out) |
| 979 |
*history_out = history.move(); |
| 980 |
} catch (MelderError) { |
| 981 |
if (idatum > 1) |
| 982 |
Melder_appendError (U"Only ", idatum - 1, U" input-output pairs out of ", numberOfData, U" were processed."); |
| 983 |
Melder_throw (me, U" & ", thee, U": learning from partial pairs not completed."); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
static integer OTMulti_crucialCell (OTMulti me, integer icand, integer iwinner, integer numberOfOptimalCandidates, conststring32 form1, conststring32 form2) |
| 988 |
{ |
| 989 |
if (my numberOfCandidates < 2) return 0; // if there is only one candidate, all cells can be greyed |
| 990 |
if (OTMulti_compareCandidates (me, icand, iwinner) == 0) { // candidate equally good as winner? |
| 991 |
if (numberOfOptimalCandidates > 1) { |
| 992 |
/* All cells are important. */ |
| 993 |
} else { |
| 994 |
integer secondBest = 0; |
| 995 |
for (integer jcand = 1; jcand <= my numberOfCandidates; jcand ++) { |
| 996 |
if (OTMulti_candidateMatches (me, jcand, form1, form2) && OTMulti_compareCandidates (me, jcand, iwinner) != 0) { // a non-optimal candidate? |
| 997 |
if (secondBest == 0) { |
| 998 |
secondBest = jcand; // first guess |
| 999 |
} else if (OTMulti_compareCandidates (me, jcand, secondBest) < 0) { |
| 1000 |
secondBest = jcand; // better guess |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
if (secondBest == 0) return 0; // if all candidates are equally good, all cells can be greyed |
| 1005 |
return OTMulti_crucialCell (me, secondBest, iwinner, 1, form1, form2); |
| 1006 |
} |
| 1007 |
} else { |
| 1008 |
const constINTVEC candidateMarks = my candidates [icand]. marks.get(); |
| 1009 |
const constINTVEC winnerMarks = my candidates [iwinner]. marks.get(); |
| 1010 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1011 |
const integer numberOfCandidateMarks = candidateMarks [my index [icons]]; |
| 1012 |
const integer numberOfWinnerMarks = winnerMarks [my index [icons]]; |
| 1013 |
if (numberOfCandidateMarks > numberOfWinnerMarks) |
| 1014 |
return icons; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
return my numberOfConstraints; // nothing grey |
| 1018 |
} |
| 1019 |
|
| 1020 |
static double OTMulti_constraintWidth (Graphics g, OTConstraint constraint, bool showDisharmony) { |
| 1021 |
char32 text [100], *newLine; |
| 1022 |
double maximumWidth = ( showDisharmony ? 0.8 * Graphics_textWidth (g, Melder_fixed (constraint -> disharmony, 1)) : 0.0 ), |
| 1023 |
firstWidth, secondWidth; |
| 1024 |
Melder_sprint (text,100, constraint -> name.get()); |
| 1025 |
newLine = str32chr (text, U'\n'); |
| 1026 |
if (newLine) { |
| 1027 |
*newLine = U'\0'; |
| 1028 |
firstWidth = Graphics_textWidth (g, text); |
| 1029 |
if (firstWidth > maximumWidth) |
| 1030 |
maximumWidth = firstWidth; |
| 1031 |
secondWidth = Graphics_textWidth (g, newLine + 1); |
| 1032 |
if (secondWidth > maximumWidth) |
| 1033 |
maximumWidth = secondWidth; |
| 1034 |
return maximumWidth; |
| 1035 |
} |
| 1036 |
firstWidth = Graphics_textWidth (g, text); |
| 1037 |
if (firstWidth > maximumWidth) |
| 1038 |
maximumWidth = firstWidth; |
| 1039 |
return maximumWidth; |
| 1040 |
} |
| 1041 |
|
| 1042 |
void OTMulti_drawTableau (OTMulti me, Graphics g, conststring32 form1, conststring32 form2, bool vertical, bool showDisharmonies) { |
| 1043 |
integer winner, winner1 = 0, winner2 = 0; |
| 1044 |
double x, y, fontSize = Graphics_inqFontSize (g); |
| 1045 |
MelderColour colour = Graphics_inqColour (g); |
| 1046 |
char32 text [200]; |
| 1047 |
bool bidirectional = form1 [0] != U'\0' && form2 [0] != U'\0'; |
| 1048 |
try { |
| 1049 |
winner = OTMulti_getWinner (me, form1, form2); |
| 1050 |
} catch (MelderError) { |
| 1051 |
Melder_clearError (); |
| 1052 |
Graphics_setWindow (g, 0.0, 1.0, 0.0, 1.0); |
| 1053 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 1054 |
Graphics_rectangle (g, 0, 1, 0, 1); |
| 1055 |
Graphics_text (g, 0.0, 0.5, U"(no matching candidates)"); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (bidirectional) { |
| 1060 |
winner1 = OTMulti_getWinner (me, form1, U""); |
| 1061 |
winner2 = OTMulti_getWinner (me, form2, U""); |
| 1062 |
} |
| 1063 |
Graphics_setWindow (g, 0.0, 1.0, 0.0, 1.0); |
| 1064 |
const double margin = Graphics_dxMMtoWC (g, 1.0); |
| 1065 |
const double fingerWidth = Graphics_dxMMtoWC (g, 7.0) * fontSize / 12.0; |
| 1066 |
const double doubleLineDx = Graphics_dxMMtoWC (g, 0.9); |
| 1067 |
const double doubleLineDy = Graphics_dyMMtoWC (g, 0.9); |
| 1068 |
const double rowHeight = Graphics_dyMMtoWC (g, 1.5 * fontSize * 25.4 / 72); |
| 1069 |
const double descent = rowHeight * 0.5; |
| 1070 |
const double worldAspectRatio = Graphics_dyMMtoWC (g, 1.0) / Graphics_dxMMtoWC (g, 1.0); // because Graphics_textWidth measures in the x direction only |
| 1071 |
/* |
| 1072 |
Compute the height of the header row. |
| 1073 |
*/ |
| 1074 |
double headerHeight; |
| 1075 |
if (vertical) { |
| 1076 |
headerHeight = 0.0; |
| 1077 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1078 |
const OTConstraint constraint = & my constraints [icons]; |
| 1079 |
const double constraintTextWidth = Graphics_textWidth (g, constraint -> name.get()); |
| 1080 |
if (constraintTextWidth > headerHeight) |
| 1081 |
headerHeight = constraintTextWidth; |
| 1082 |
} |
| 1083 |
headerHeight += margin * 2; |
| 1084 |
headerHeight *= worldAspectRatio; |
| 1085 |
} else { |
| 1086 |
headerHeight = rowHeight; |
| 1087 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1088 |
const OTConstraint constraint = & my constraints [icons]; |
| 1089 |
if (str32chr (constraint -> name.get(), U'\n')) { |
| 1090 |
headerHeight += 0.7 * rowHeight; |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
/* |
| 1096 |
Compute longest candidate string. |
| 1097 |
Also count the number of optimal candidates (if there are more than one, the fingers will be drawn in red). |
| 1098 |
*/ |
| 1099 |
double candWidth = Graphics_textWidth_ps (g, form1, true) + Graphics_textWidth_ps (g, form2, true); |
| 1100 |
integer numberOfMatchingCandidates = 0; |
| 1101 |
integer numberOfOptimalCandidates = 0; |
| 1102 |
integer numberOfOptimalCandidates1 = 0; |
| 1103 |
integer numberOfOptimalCandidates2 = 0; |
| 1104 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) { |
| 1105 |
if ((form1 [0] != U'\0' && OTMulti_candidateMatches (me, icand, form1, U"")) || |
| 1106 |
(form2 [0] != U'\0' && OTMulti_candidateMatches (me, icand, form2, U"")) || |
| 1107 |
(form1 [0] == U'\0' && form2 [0] == U'\0')) |
| 1108 |
{ |
| 1109 |
const double width = Graphics_textWidth_ps (g, my candidates [icand]. string.get(), true); |
| 1110 |
if (width > candWidth) |
| 1111 |
candWidth = width; |
| 1112 |
numberOfMatchingCandidates ++; |
| 1113 |
if (OTMulti_compareCandidates (me, icand, winner) == 0) |
| 1114 |
numberOfOptimalCandidates ++; |
| 1115 |
if (winner1 != 0 && OTMulti_compareCandidates (me, icand, winner1) == 0) |
| 1116 |
numberOfOptimalCandidates1 ++; |
| 1117 |
if (winner2 != 0 && OTMulti_compareCandidates (me, icand, winner2) == 0) |
| 1118 |
numberOfOptimalCandidates2 ++; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
candWidth += fingerWidth * (bidirectional ? 3 : 1) + margin * 3; |
| 1122 |
/* |
| 1123 |
Compute tableau width. |
| 1124 |
*/ |
| 1125 |
longdouble tableauWidth = candWidth + doubleLineDx; |
| 1126 |
if (vertical) { |
| 1127 |
tableauWidth += rowHeight * my numberOfConstraints / worldAspectRatio; |
| 1128 |
} else { |
| 1129 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1130 |
const OTConstraint constraint = & my constraints [icons]; |
| 1131 |
tableauWidth += OTMulti_constraintWidth (g, constraint, showDisharmonies); |
| 1132 |
} |
| 1133 |
tableauWidth += margin * 2 * my numberOfConstraints; |
| 1134 |
} |
| 1135 |
/* |
| 1136 |
Draw box. |
| 1137 |
*/ |
| 1138 |
x = doubleLineDx; // left side of tableau |
| 1139 |
y = 1.0 - doubleLineDy; |
| 1140 |
if (showDisharmonies) |
| 1141 |
y -= 0.6 * rowHeight; |
| 1142 |
Graphics_rectangle (g, x, x + tableauWidth, |
| 1143 |
y - headerHeight - numberOfMatchingCandidates * rowHeight - doubleLineDy, y); |
| 1144 |
/* |
| 1145 |
Draw input. |
| 1146 |
*/ |
| 1147 |
y -= headerHeight; |
| 1148 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 1149 |
Graphics_text (g, x + 0.5 * candWidth, y + 0.5 * headerHeight, form1, form2); |
| 1150 |
Graphics_rectangle (g, x, x + candWidth, y, y + headerHeight); |
| 1151 |
/* |
| 1152 |
Draw constraint names. |
| 1153 |
*/ |
| 1154 |
x += candWidth + doubleLineDx; |
| 1155 |
if (vertical) |
| 1156 |
Graphics_setTextRotation (g, 90.0); |
| 1157 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1158 |
const OTConstraint constraint = & my constraints [my index [icons]]; |
| 1159 |
const double width = ( vertical ? rowHeight / worldAspectRatio : OTMulti_constraintWidth (g, constraint, showDisharmonies) + margin * 2 ); |
| 1160 |
if (str32chr (constraint -> name.get(), U'\n') && ! vertical) { |
| 1161 |
char32 *newLine; |
| 1162 |
Melder_sprint (text,200, constraint -> name.get()); |
| 1163 |
newLine = str32chr (text, U'\n'); |
| 1164 |
*newLine = U'\0'; |
| 1165 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_TOP); |
| 1166 |
Graphics_text (g, x + 0.5 * width, y + headerHeight, text); |
| 1167 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_BOTTOM); |
| 1168 |
Graphics_text (g, x + 0.5 * width, y, newLine + 1); |
| 1169 |
} else if (vertical) { |
| 1170 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 1171 |
Graphics_text (g, x + 0.5 * width, y + margin, constraint -> name.get()); |
| 1172 |
} else { |
| 1173 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 1174 |
Graphics_text (g, x + 0.5 * width, y + 0.5 * headerHeight, constraint -> name.get()); |
| 1175 |
} |
| 1176 |
if (showDisharmonies) { |
| 1177 |
Graphics_setFontSize (g, 0.8 * fontSize); |
| 1178 |
if (vertical) |
| 1179 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 1180 |
else |
| 1181 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_BOTTOM); |
| 1182 |
Graphics_text (g, x + 0.5 * width, y + headerHeight, Melder_fixed (constraint -> disharmony, 1)); |
| 1183 |
Graphics_setFontSize (g, fontSize); |
| 1184 |
} |
| 1185 |
Graphics_line (g, x, y, x, y + headerHeight); |
| 1186 |
Graphics_line (g, x, y, x + width, y); |
| 1187 |
x += width; |
| 1188 |
} |
| 1189 |
if (vertical) |
| 1190 |
Graphics_setTextRotation (g, 0.0); |
| 1191 |
/* |
| 1192 |
Draw candidates. |
| 1193 |
*/ |
| 1194 |
y -= doubleLineDy; |
| 1195 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) |
| 1196 |
if ((form1 [0] != U'\0' && OTMulti_candidateMatches (me, icand, form1, U"")) || |
| 1197 |
(form2 [0] != U'\0' && OTMulti_candidateMatches (me, icand, form2, U"")) || |
| 1198 |
(form1 [0] == U'\0' && form2 [0] == U'\0')) |
| 1199 |
{ |
| 1200 |
const integer crucialCell = OTMulti_crucialCell (me, icand, winner, numberOfOptimalCandidates, form1, form2); |
| 1201 |
const bool candidateIsOptimal = ( OTMulti_compareCandidates (me, icand, winner) == 0 ); |
| 1202 |
const bool candidateIsOptimal1 = ( winner1 != 0 && OTMulti_compareCandidates (me, icand, winner1) == 0 ); |
| 1203 |
const bool candidateIsOptimal2 = ( winner2 != 0 && OTMulti_compareCandidates (me, icand, winner2) == 0 ); |
| 1204 |
/* |
| 1205 |
Draw candidate transcription. |
| 1206 |
*/ |
| 1207 |
x = doubleLineDx; |
| 1208 |
y -= rowHeight; |
| 1209 |
Graphics_setTextAlignment (g, Graphics_RIGHT, Graphics_HALF); |
| 1210 |
Graphics_text (g, x + candWidth - margin, y + descent, my candidates [icand]. string.get()); |
| 1211 |
if (candidateIsOptimal) { |
| 1212 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 1213 |
Graphics_setFontSize (g, ( bidirectional ? 1.2 : 1.5 ) * fontSize); |
| 1214 |
if (numberOfOptimalCandidates > 1) |
| 1215 |
Graphics_setColour (g, Melder_RED); |
| 1216 |
Graphics_text (g, x + margin, y + descent - Graphics_dyMMtoWC (g, 0.5) * fontSize / 12.0, bidirectional ? U"\\Vr" : U"\\pf"); |
| 1217 |
Graphics_setColour (g, colour); |
| 1218 |
Graphics_setFontSize (g, fontSize); |
| 1219 |
} |
| 1220 |
if (candidateIsOptimal1) { |
| 1221 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 1222 |
Graphics_setFontSize (g, 1.5 * fontSize); |
| 1223 |
if (numberOfOptimalCandidates1 > 1) |
| 1224 |
Graphics_setColour (g, Melder_RED); |
| 1225 |
Graphics_text (g, x + margin + fingerWidth, y + descent - Graphics_dyMMtoWC (g, 0.5) * fontSize / 12.0, U"\\pf"); |
| 1226 |
Graphics_setColour (g, colour); |
| 1227 |
Graphics_setFontSize (g, fontSize); |
| 1228 |
} |
| 1229 |
if (candidateIsOptimal2) { |
| 1230 |
Graphics_setTextAlignment (g, Graphics_RIGHT, Graphics_HALF); |
| 1231 |
Graphics_setFontSize (g, 1.5 * fontSize); |
| 1232 |
if (numberOfOptimalCandidates2 > 1) |
| 1233 |
Graphics_setColour (g, Melder_RED); |
| 1234 |
Graphics_setTextRotation (g, 180); |
| 1235 |
Graphics_text (g, x + margin + fingerWidth * 2, y + descent - Graphics_dyMMtoWC (g, 0.0) * fontSize / 12.0, U"\\pf"); |
| 1236 |
Graphics_setTextRotation (g, 0); |
| 1237 |
Graphics_setColour (g, colour); |
| 1238 |
Graphics_setFontSize (g, fontSize); |
| 1239 |
} |
| 1240 |
Graphics_rectangle (g, x, x + candWidth, y, y + rowHeight); |
| 1241 |
/* |
| 1242 |
Draw grey cell backgrounds. |
| 1243 |
*/ |
| 1244 |
if (! bidirectional && my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 1245 |
x = candWidth + 2 * doubleLineDx; |
| 1246 |
Graphics_setGrey (g, 0.9); |
| 1247 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1248 |
integer index = my index [icons]; |
| 1249 |
OTConstraint constraint = & my constraints [index]; |
| 1250 |
const double width = ( vertical ? rowHeight / worldAspectRatio : OTMulti_constraintWidth (g, constraint, showDisharmonies) + margin * 2 ); |
| 1251 |
if (icons > crucialCell) |
| 1252 |
Graphics_fillRectangle (g, x, x + width, y, y + rowHeight); |
| 1253 |
x += width; |
| 1254 |
} |
| 1255 |
Graphics_setColour (g, colour); |
| 1256 |
} |
| 1257 |
/* |
| 1258 |
Draw cell marks. |
| 1259 |
*/ |
| 1260 |
x = candWidth + 2 * doubleLineDx; |
| 1261 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 1262 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1263 |
const integer index = my index [icons]; |
| 1264 |
const OTConstraint constraint = & my constraints [index]; |
| 1265 |
const double width = ( vertical ? rowHeight / worldAspectRatio : OTMulti_constraintWidth (g, constraint, showDisharmonies) + margin * 2 ); |
| 1266 |
char32 markString [40]; |
| 1267 |
markString [0] = U'\0'; |
| 1268 |
if (bidirectional && my candidates [icand]. marks [index] > 0) { |
| 1269 |
if ((candidateIsOptimal1 || candidateIsOptimal2) && ! candidateIsOptimal) |
| 1270 |
str32cat (markString, U"\\<-"); |
| 1271 |
} |
| 1272 |
if (bidirectional && my candidates [icand]. marks [index] < 0) { |
| 1273 |
if (candidateIsOptimal && ! candidateIsOptimal1) |
| 1274 |
str32cat (markString, U"\\<-"); |
| 1275 |
if (candidateIsOptimal && ! candidateIsOptimal2) |
| 1276 |
str32cat (markString, U"\\<-"); |
| 1277 |
} |
| 1278 |
/* |
| 1279 |
An exclamation mark can be drawn in this cell only if both of the following conditions are met: |
| 1280 |
1. the candidate is not optimal; |
| 1281 |
2. this is the crucial cell, i.e. the cells after it are drawn in grey. |
| 1282 |
*/ |
| 1283 |
if (! bidirectional && icons == crucialCell && ! candidateIsOptimal && |
| 1284 |
my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) |
| 1285 |
{ |
| 1286 |
const integer winnerMarks = my candidates [winner]. marks [index]; |
| 1287 |
if (winnerMarks + 1 > 5) { |
| 1288 |
str32cat (markString, Melder_integer (winnerMarks + 1)); |
| 1289 |
} else { |
| 1290 |
for (integer imark = 1; imark <= winnerMarks + 1; imark ++) |
| 1291 |
str32cat (markString, U"*"); |
| 1292 |
} |
| 1293 |
for (integer imark = my candidates [icand]. marks [index]; imark < 0; imark ++) |
| 1294 |
str32cat (markString, U"+"); |
| 1295 |
str32cat (markString, U"!"); |
| 1296 |
if (my candidates [icand]. marks [index] - (winnerMarks + 2) + 1 > 5) { |
| 1297 |
str32cat (markString, Melder_integer (my candidates [icand]. marks [index] - (winnerMarks + 2) + 1)); |
| 1298 |
} else { |
| 1299 |
for (integer imark = winnerMarks + 2; imark <= my candidates [icand]. marks [index]; imark ++) |
| 1300 |
str32cat (markString, U"*"); |
| 1301 |
} |
| 1302 |
} else { |
| 1303 |
if (my candidates [icand]. marks [index] > 5) { |
| 1304 |
str32cat (markString, Melder_integer (my candidates [icand]. marks [index])); |
| 1305 |
} else { |
| 1306 |
for (integer imark = 1; imark <= my candidates [icand]. marks [index]; imark ++) |
| 1307 |
str32cat (markString, U"*"); |
| 1308 |
for (integer imark = my candidates [icand]. marks [index]; imark < 0; imark ++) |
| 1309 |
str32cat (markString, U"+"); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
if (bidirectional && my candidates [icand]. marks [index] > 0) { |
| 1313 |
if (candidateIsOptimal && ! candidateIsOptimal1) |
| 1314 |
str32cat (markString, U"\\->"); |
| 1315 |
if (candidateIsOptimal && ! candidateIsOptimal2) |
| 1316 |
str32cat (markString, U"\\->"); |
| 1317 |
} |
| 1318 |
if (bidirectional && my candidates [icand]. marks [index] < 0) { |
| 1319 |
if ((candidateIsOptimal1 || candidateIsOptimal2) && ! candidateIsOptimal) |
| 1320 |
str32cat (markString, U"\\->"); |
| 1321 |
} |
| 1322 |
Graphics_text (g, x + 0.5 * width, y + descent, markString); |
| 1323 |
Graphics_setColour (g, colour); |
| 1324 |
Graphics_line (g, x, y, x, y + rowHeight); |
| 1325 |
Graphics_line (g, x, y + rowHeight, x + width, y + rowHeight); |
| 1326 |
x += width; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
/* |
| 1330 |
Draw box. |
| 1331 |
*/ |
| 1332 |
x = doubleLineDx; // left side of tableau |
| 1333 |
y = 1.0 - doubleLineDy; |
| 1334 |
if (showDisharmonies) |
| 1335 |
y -= 0.6 * rowHeight; |
| 1336 |
Graphics_rectangle (g, x, x + tableauWidth, |
| 1337 |
y - headerHeight - numberOfMatchingCandidates * rowHeight - doubleLineDy, y); |
| 1338 |
} |
| 1339 |
|
| 1340 |
void OTMulti_reset (OTMulti me, double ranking) { |
| 1341 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1342 |
const OTConstraint constraint = & my constraints [icons]; |
| 1343 |
constraint -> disharmony = constraint -> ranking = ranking; |
| 1344 |
} |
| 1345 |
OTMulti_sort (me); |
| 1346 |
} |
| 1347 |
|
| 1348 |
void OTMulti_setRanking (OTMulti me, integer constraint, double ranking, double disharmony) { |
| 1349 |
try { |
| 1350 |
Melder_require (constraint >= 1, |
| 1351 |
U"The constraint number should be positive, not ", constraint, U"."); |
| 1352 |
Melder_require (constraint <= my numberOfConstraints, |
| 1353 |
U"Constraint ", constraint, U" does not exist (there are only ", my numberOfConstraints, U" constraints)."); |
| 1354 |
my constraints [constraint]. ranking = ranking; |
| 1355 |
my constraints [constraint]. disharmony = disharmony; |
| 1356 |
OTMulti_sort (me); |
| 1357 |
} catch (MelderError) { |
| 1358 |
Melder_throw (me, U": ranking not set."); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
void OTMulti_setConstraintPlasticity (OTMulti me, integer constraint, double plasticity) { |
| 1363 |
try { |
| 1364 |
Melder_require (constraint >= 1, |
| 1365 |
U"The constraint number should be positive, not ", constraint, U"."); |
| 1366 |
Melder_require (constraint <= my numberOfConstraints, |
| 1367 |
U"Constraint ", constraint, U" does not exist (there are only ", my numberOfConstraints, U" constraints)."); |
| 1368 |
my constraints [constraint]. plasticity = plasticity; |
| 1369 |
} catch (MelderError) { |
| 1370 |
Melder_throw (me, U": constraint plasticity not set."); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
void OTMulti_removeConstraint (OTMulti me, conststring32 constraintName) { |
| 1375 |
try { |
| 1376 |
if (my numberOfConstraints <= 1) |
| 1377 |
Melder_throw (me, U": cannot remove last constraint."); |
| 1378 |
integer constraintToBeRemoved = OTMulti_getConstraintIndexFromName (me, constraintName); |
| 1379 |
if (constraintToBeRemoved == 0) |
| 1380 |
Melder_throw (U"No constraint \"", constraintName, U"\"."); |
| 1381 |
/* |
| 1382 |
Remove the constraint while reusing the memory space. |
| 1383 |
*/ |
| 1384 |
my constraints [constraintToBeRemoved]. destroy (); |
| 1385 |
my constraints. remove (constraintToBeRemoved); |
| 1386 |
my numberOfConstraints -= 1; // maintain invariant |
| 1387 |
Melder_assert (my numberOfConstraints == my constraints.size); |
| 1388 |
/* |
| 1389 |
Shift tableau rows. |
| 1390 |
*/ |
| 1391 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) { |
| 1392 |
const OTCandidate candidate = & my candidates [icand]; |
| 1393 |
candidate -> marks. remove (constraintToBeRemoved); |
| 1394 |
candidate -> numberOfConstraints -= 1; // maintain invariant |
| 1395 |
Melder_assert (candidate -> numberOfConstraints == candidate -> marks.size); |
| 1396 |
} |
| 1397 |
/* |
| 1398 |
Rebuild index. |
| 1399 |
*/ |
| 1400 |
my index. resize (my numberOfConstraints); |
| 1401 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1402 |
my index [icons] = icons; |
| 1403 |
OTMulti_sort (me); |
| 1404 |
} catch (MelderError) { |
| 1405 |
Melder_throw (me, U": constraint not removed."); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
autostring32 OTMulti_generateOptimalForm (OTMulti me, conststring32 form1, conststring32 form2, double evaluationNoise) { |
| 1410 |
try { |
| 1411 |
OTMulti_newDisharmonies (me, evaluationNoise); |
| 1412 |
integer winner = OTMulti_getWinner (me, form1, form2); |
| 1413 |
return Melder_dup (my candidates [winner]. string.get()); |
| 1414 |
} catch (MelderError) { |
| 1415 |
Melder_throw (me, U": optimal form not generated."); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
|
| 1419 |
autoStrings OTMulti_Strings_generateOptimalForms (OTMulti me, Strings thee, double evaluationNoise) { |
| 1420 |
try { |
| 1421 |
autoStrings outputs = Thing_new (Strings); |
| 1422 |
integer n = thy numberOfStrings; |
| 1423 |
outputs -> numberOfStrings = n; |
| 1424 |
outputs -> strings = autoSTRVEC (n); |
| 1425 |
for (integer i = 1; i <= n; i ++) |
| 1426 |
outputs -> strings [i] = OTMulti_generateOptimalForm (me, thy strings [i].get(), U"", evaluationNoise); |
| 1427 |
return outputs; |
| 1428 |
} catch (MelderError) { |
| 1429 |
Melder_throw (me, U" & ", thee, U": optimal forms not generated."); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
autoStrings OTMulti_generateOptimalForms (OTMulti me, conststring32 form1, conststring32 form2, integer numberOfTrials, double evaluationNoise) { |
| 1434 |
try { |
| 1435 |
autoStrings outputs = Thing_new (Strings); |
| 1436 |
outputs -> numberOfStrings = numberOfTrials; |
| 1437 |
outputs -> strings = autoSTRVEC (numberOfTrials); |
| 1438 |
for (integer i = 1; i <= numberOfTrials; i ++) |
| 1439 |
outputs -> strings [i] = OTMulti_generateOptimalForm (me, form1, form2, evaluationNoise); |
| 1440 |
return outputs; |
| 1441 |
} catch (MelderError) { |
| 1442 |
Melder_throw (me, U": optimal forms not generated."); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
autoDistributions OTMulti_to_Distribution (OTMulti me, conststring32 form1, conststring32 form2, |
| 1447 |
integer numberOfTrials, double evaluationNoise) |
| 1448 |
{ |
| 1449 |
try { |
| 1450 |
integer totalNumberOfOutputs = 0, iout = 0; |
| 1451 |
/* |
| 1452 |
Count the total number of outputs. |
| 1453 |
*/ |
| 1454 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) |
| 1455 |
if (OTMulti_candidateMatches (me, icand, form1, form2)) |
| 1456 |
totalNumberOfOutputs ++; |
| 1457 |
/* |
| 1458 |
Create the distribution. One row for every output form. |
| 1459 |
*/ |
| 1460 |
autoDistributions thee = Distributions_create (totalNumberOfOutputs, 1); |
| 1461 |
autoINTVEC index = raw_INTVEC (my numberOfCandidates); |
| 1462 |
/* |
| 1463 |
Set the row labels to the output strings. |
| 1464 |
*/ |
| 1465 |
iout = 0; |
| 1466 |
for (integer icand = 1; icand <= my numberOfCandidates; icand ++) { |
| 1467 |
if (OTMulti_candidateMatches (me, icand, form1, form2)) { |
| 1468 |
thy rowLabels [++ iout] = Melder_dup (my candidates [icand]. string.get()); |
| 1469 |
index [icand] = iout; |
| 1470 |
} |
| 1471 |
} |
| 1472 |
/* |
| 1473 |
Compute a number of outputs and store the results. |
| 1474 |
*/ |
| 1475 |
for (integer itrial = 1; itrial <= numberOfTrials; itrial ++) { |
| 1476 |
OTMulti_newDisharmonies (me, evaluationNoise); |
| 1477 |
integer iwinner = OTMulti_getWinner (me, form1, form2); |
| 1478 |
thy data [index [iwinner]] [1] += 1; |
| 1479 |
} |
| 1480 |
return thee; |
| 1481 |
} catch (MelderError) { |
| 1482 |
Melder_throw (me, U": distribution not computed."); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
/* End of file OTMulti.cpp */ |