private bool CheckThreshold(double th, double ll, double ul, HH hh = null) { if (th <= ul && th >= ll) { return(true); } string hhID = hh == null ? string.Empty : $" HH {infoStore.GetIDHH(hh)}:"; if (th > ul) { infoStore.communicator.ReportError(new Communicator.ErrorInfo() { isWarning = true, message = $"{description.Get()}:{hhID} {th} > {ul}, threshold is higher than upper limit, threshold is ignored", runTimeErrorId = hh == null ? string.Empty : description.funID }); } if (th < ll) { infoStore.communicator.ReportError(new Communicator.ErrorInfo() { isWarning = true, message = $"{description.Get()}:{hhID} {th} < {ll}, threshold is lower than lower limit, threshold is ignored", runTimeErrorId = hh == null ? string.Empty : description.funID }); } return(false); }
internal override double GetValue(HH hh, List <Person> tu, Person person) { GetAgeLimits(out double ageMin, out double ageMax); double headID = hh.GetPersonValue(indexPersonIdVar, person.indexInHH), partnerID = hh.GetPersonValue(indexPartnerIdVar, person.indexInHH); double n = 0.0; foreach (Person p in tu) { if (p.isDepChild || p.isLooseDepChild) { if (hh.GetPersonValue(indexPersonIdVar, p.indexInHH) != headID && hh.GetPersonValue(indexPersonIdVar, p.indexInHH) != partnerID) { if (hh.GetPersonValue(indexFatherIdVar, p.indexInHH) == headID || (partnerID > 0 && hh.GetPersonValue(indexFatherIdVar, p.indexInHH) == partnerID) || hh.GetPersonValue(indexMotherIdVar, p.indexInHH) == headID || (partnerID > 0 && hh.GetPersonValue(indexMotherIdVar, p.indexInHH) == partnerID) || (hh.GetPersonValue(indexFatherIdVar, p.indexInHH) == 0 && hh.GetPersonValue(indexMotherIdVar, p.indexInHH) == 0)) { if (hh.GetPersonValue(indexAgeVar, p.indexInHH) >= ageMin && hh.GetPersonValue(indexAgeVar, p.indexInHH) <= ageMax) { ++n; } } } } } return(n); }
private void CheckLimits(ref double ll, ref double ul, HH hh = null) { if (ll <= ul) { return; } if (limPri == DefPar.Value.NA) { string hhID = hh == null ? string.Empty : $" HH {infoStore.GetIDHH(hh)}:"; infoStore.communicator.ReportError(new Communicator.ErrorInfo() { isWarning = true, message = $"{description.Get()}:{hhID} {ll} > {ul}, upper limit overwrites lower limit", // to be precise one would need a runTimeErrorId per formula/condition-operand // but I think one can neglect this unlikely and not harmful impreciseness runTimeErrorId = hh == null ? string.Empty : description.parID }); ll = ul; } else { if (limPri == DefPar.Value.LIMPRI_LOWER) { ul = ll; } else { ll = ul; } } }
/// <summary> /// get whether the condition is true for a specific person /// note: this is strictly individual, that means that e.g. incomelists and monetary-variables are evaluated for the person /// </summary> internal bool GetPersonValue(HH hh, Person person) { return(parFormula.GetValue(hh, new List <Person>() { person }, person) != 0); }
internal override void Run(HH hh, List <Person> tu) // note: DefVar does not have a TAX_UNIT parameter { // therefore tu is DUMMY_INDIVIDUAL_TU, i.e. this is called for each person if (!IsRunCondMet(hh)) { return; // see note in PrepareNonCommonPar } // initialise the variables (see notes above) foreach (List <VarDef> condGroup in varDefs.Values) { double init = double.MaxValue; foreach (VarDef vd in condGroup.OrderBy(x => x.groupNo)) // make sure you preserve group order in applying conditions! { if (vd.condition != null && !vd.condition.GetPersonValue(hh, tu[0])) { continue; } init = vd.initFormula.GetValue(hh, tu); // usually this line would be enough for assessing the init-value, the rest is just for DefVar with Conditions } if (init == double.MaxValue) { infoStore.communicator.ReportError(new Communicator.ErrorInfo() { isWarning = true, runTimeErrorId = description.funID, message = $"{description.Get()}: is initialised with zero for idperson={infoStore.GetIDPerson(hh, tu[0].indexInHH)}, because no suitable condition was found" }); init = 0; } hh.SetTUValue(init, condGroup[0].varSpec.index, tu); } }
private void RunUnitLoop(HH hh, List <Person> tu) { // store the "running" variables (yem_unit, ils_earns_unit), and take the level into account foreach (StoreVar v in vars) { if (v.iteration != NOTAP) { continue; // is handled below } if (hh.GetPersonValue(varIndexCurElig, tu[0].indexInHH) <= 0) { continue; // store only for the (head of the) "currently elig" unit } List <Person> altTU = hh.GetAlternativeTU(v.level, tu, description); // on may consider performance here, but let's first get it right double levVal = v.unitLoopILPar == null?hh.GetTUValue(v.origVar.index, altTU) : v.unitLoopILPar.GetValue(hh, altTU); hh.SetPersonValue(levVal, v.storeVar.index, tu[0].indexInHH); } // store the "on demand" variables (e.g. yem_unit1) - they are stored the same way as for a normal loops (i.e. no level-stuff) foreach (StoreVar v in vars) { if (v.iteration != NOTAP && v.iteration == hh.GetPersonValue(varIndexLoopCounter, 0)) { hh.SetTUValue(hh.GetTUValue(v.origVar.index, tu), v.storeVar.index, tu); } } }
private void CheckLimits(ref double ll, ref double ul, HH hh = null) { if (ll <= ul) { return; } if (limPri == DefPar.Value.NA) { string hhID = hh == null ? string.Empty : $" HH {infoStore.GetIDHH(hh)}:"; infoStore.communicator.ReportError(new Communicator.ErrorInfo() { isWarning = true, message = $"{description.Get()}:{hhID} {ll} > {ul}, upper limit overwrites lower limit", runTimeErrorId = hh == null ? string.Empty : description.funID }); ll = ul; } else { if (limPri == DefPar.Value.LIMPRI_LOWER) { ul = ll; } else { ll = ul; } } }
protected double GetCounter(HH hh) { if (hh == null) { return(infoStore.hhAdmin.GlobalGetVar(varIndexLoopCounter)); // sequential run: get counter from 1st hh } return(hh.GetPersonValue(varIndexLoopCounter, 0)); // parallel run: get counter from 1st person of the currently processed hh }
private void SetCurElig(HH hh, double curIter) { foreach (List <Person> tu in hh.GetTUs(eligUnit)) // set the variable for each TU in the HH { double eligInIter = hh.GetTUValue(varIndexIsEligInIter, tu); // assess in which iteration the TU is "elig" hh.SetTUValue(eligInIter == curIter ? 1 : 0, varIndexIsCurElig, tu); } }
private bool IsOutputElig(HH hh, List <Person> tu) { if (parWho == null) { return(true); } return(FunInSpineBase.IsCondMetByTU(hh, tu, indexEligVar, parWho.GetCateg())); }
protected virtual void SetOutVars(double val, HH hh, List <Person> tu) { hh.SetTUValue(value: val, varIndex: coParOutVar.Item1.index, tu: tu, addTo: coParOutVar.Item2); if (coParResultVar != null) { hh.SetTUValue(value: val, varIndex: coParResultVar.index, tu: tu); } }
private static bool IsCondMetByTU(HH hh, List <Person> tu, string who, ParCond parCond = null, int iEligVar = -1) { int i = 0; if (who == DefPar.Value.WHO_NOBODY) { return(true); } if (parCond != null) { foreach (var pe in parCond.GetTUValues(hh, tu)) { bool elig = pe.Value; Person p = tu[i++]; bool?ew = EvalWho(elig, p); if (ew != null) { return(ew == true ? true : false); } } } else { foreach (Person p in tu) { bool elig = hh.GetPersonValue(iEligVar, p.indexInHH) != 0; bool?ew = EvalWho(elig, p); if (ew != null) { return(ew == true ? true : false); } } } return(who == DefPar.Value.WHO_ALL || who == DefPar.Value.WHO_ALL_ADULTS); bool?EvalWho(bool elig, Person p) { if (who == DefPar.Value.WHO_ONE && elig) { return(true); } if (who == DefPar.Value.WHO_ALL && !elig) { return(false); } if (who == DefPar.Value.WHO_ONE_ADULT && !p.isDepChild && elig) { return(true); } if ((who == DefPar.Value.WHO_ALL_ADULTS) && !p.isDepChild && !elig) { return(false); } if (who == DefPar.Value.WHO_NOBODY) { return(true); } return(null); } }
private Dictionary <string, double> AssessOperands(HH hh, List <Person> tu, Person person) { Dictionary <string, double> ops = new Dictionary <string, double>(); foreach (var op in operands) { ops.Add(op.Key, op.Value.GetValue(hh, tu, person)); } return(ops); }
protected bool IsRunCondMet(HH hh = null) { if (coParRunCond == null) { return(true); } return((hh == null) ? coParRunCond.GetGlobalValue() // for non-spine functions return values of first person in data : coParRunCond.GetPersonValue(hh, new Person(0))); // for spine functions return values of first person in HH } // (allows for using loopcounters in global condition)
// person != null: value is requested for a specific person // person == null: value is requested for tu internal override double GetValue(HH hh, List <Person> tu, Person person = null) { if (alternativeTU != null) { tu = hh.GetAlternativeTU(alternativeTU, tu, description); } double value = getComponentValue(hh, tu, person, content); return(ApplyLimits(value, hh, tu, person)); }
protected override double DoFunWork(HH hh, List <Person> tu) { double max = double.MinValue; foreach (ParFormula val in vals) { max = Math.Max(max, val.GetValue(hh, tu)); } return(max); }
internal override bool DoBreak(HH hh = null) { double iterationsDone = GetCounter(hh); if (IsFinished(hh, iterationsDone, true)) { return(true); } SetCounter(hh, iterationsDone + 1); return(false); }
internal virtual bool DoBreak(HH hh = null) // note: hh is only available in parallel runs (see setting counter below) { double iterationsDone = GetCounter(hh); if (IsFinished(hh, iterationsDone, true)) { return(true); } SetCounter(hh, iterationsDone + 1); // increase loop-counter return(false); }
internal override void Run(HH hh, List <Person> tu) // note: IlArithOp does not have a TAX_UNIT parameter { // therefore tu is DUMMY_INDIVIDUAL_TU, i.e. this is called for each person if (!IsRunCondMet(hh)) { return; } foreach (OutSpecification os in outSpecifications) { hh.SetTUValue(os.parFormula.GetValue(hh, tu), os.varSpec.index, tu); } }
internal virtual bool DoJumpOver(bool init, HH hh = null) // note: hh is only available in parallel runs (see setting counter below) { if (!init) { return(false); // do nothing on jump-back, as the work was done at the loop-end: loop-counter was increased and checked } // any (global) condition was checked and cannot have changed meanwhile // in fact updates are only necessary for UnitLoops, which override this function SetCounter(hh, 1); // initialise loop-counter return(IsFinished(hh, 0, false)); // check if the (global) conition is never fulfilled }
void GetValues(HH hh, List <Person> tu, out OpItem selItem, out bool noAction) { double formulaOperandValue = 0; selItem = null; noAction = false; if (opTask == OPERAND_TYPE.FORMULA) { formulaOperandValue = formulaOperand.GetValue(hh, tu); } double max = double.MinValue, min = double.MaxValue; foreach (OpItem oi in opItems) { oi.numOperator = hh.GetPersonValue(oi.varOperator.index, tu[0].indexInHH); if (opTask == OPERAND_TYPE.FORMULA) { oi.numOperand = formulaOperandValue; } if (opTask == OPERAND_TYPE.IL) { oi.numOperand = hh.GetPersonValue(oi.varOperand.index, tu[0].indexInHH); } // remark: the behaviour of the old executable cannot be completely reproduced if there are equal mins/maxs // the old executable does the operation only on one item, but which depends on the order of content ... switch (selVar) { case DefPar.Value.ILVAROP_ALL: continue; case DefPar.Value.ILVAROP_MAX: if (oi.numOperator > max) { max = oi.numOperator; selItem = oi; } ; break; case DefPar.Value.ILVAROP_MIN: if (oi.numOperator < min) { min = oi.numOperator; selItem = oi; } ; break; case DefPar.Value.ILVAROP_MINPOS: if (oi.numOperator <= min && oi.numOperator > 0) { min = oi.numOperator; selItem = oi; } ; break; } } if (selVar == DefPar.Value.ILVAROP_MINPOS && selItem == null) { noAction = true; } }
internal override void Run(HH hh, List <Person> tu) // is called for each person in HH, i.e. with individual TU { if (!IsRunCondMet(hh)) { return; // see note on run-cond above } foreach (Default d in defaults) { double val = d.parFormula.GetValue(hh, tu, tu[0]); hh.SetPersonValue(val, d.varSpec.index, tu[0].indexInHH); } }
internal override void Run(HH hh, List <Person> tu) // note: Scale does not have a TAX_UNIT parameter { // therefore tu is DUMMY_INDIVIDUAL_TU, i.e. this is called for each person if (factorVar == 1 || !IsRunCondMet(hh)) { return; } foreach (int varIndex in infoStore.operandAdmin.GetMonetaryVarIndices()) { hh.SetTUValue(hh.GetTUValue(varIndex, tu) * factorVar, varIndex, tu); } }
protected double ApplyLimits(double value, HH hh, List <Person> tu, Person person) { double ll = pLowLim != null?pLowLim.GetValue(hh, tu, person) : lowLim; double ul = pUpLim != null?pUpLim.GetValue(hh, tu, person) : upLim; if (doRunTimeLimCheck) { CheckLimits(ref ll, ref ul, hh); } return(Math.Min(ul, Math.Max(ll, value))); }
protected override double DoFunWork(HH hh, List <Person> tu) { if (tuName == null) { infoStore.hhAdmin.UpdateAllTUs(hh); } else { hh.UpdateTU(tuName); } return(0.0); }
private double ApplyLowUpLimits(double val, HH hh, List <Person> tu, out double ll, out double ul) { ll = coParLowLim != null?coParLowLim.GetValue(hh, tu) : lowLim; ul = coParUpLim != null?coParUpLim.GetValue(hh, tu) : upLim; if (doRunTimeLimCheck) { CheckLimits(ref ll, ref ul, hh); } return(Math.Min(ul, Math.Max(ll, val))); }
internal override double GetValue(HH hh, List <Person> tu, Person person) { double fatherId = hh.GetPersonValue(indexFatherIdVar, person.indexInHH); for (byte i = 0; i < hh.GetPersonCount(); i++) { if (fatherId == hh.GetPersonValue(indexPersonIdVar, i)) { return(parIncome.GetValue(hh, tu, new Person(i))); } } return(0.0); }
internal override double GetValue(HH hh, List <Person> tu, Person person) { double n = 0.0; foreach (Person p in tu) { if (p.isDepParent) { ++n; } } return(n); }
private bool IsFinished(HH hh, double iterationsDone, bool checkRunCond) { if (checkRunCond && !IsRunCondMet(hh)) { return(true); // mimics the old exe, which does a do-while, i.e. check RunCond at end only } if (nIterations == -1) { return(hh == null?breakCond.GetGlobalValue() // loop is terminated by condition : breakCond.GetPersonValue(hh, new Person(0))); // condition is global: any person is ok as } return(iterationsDone >= nIterations); // loop has a fixed number of iteration }
protected void SetCounter(HH hh, double cnt) { if (hh == null) { infoStore.hhAdmin.GlobalSetVar(varIndexLoopCounter, cnt); // sequential run: set counter for all households } else // parallel run: set counter only for the currently processed household { for (int personIndex = 0; personIndex < hh.GetPersonCount(); ++personIndex) { hh.SetPersonValue(cnt, varIndexLoopCounter, personIndex); } } }