/// <summary> /// Creates a new action object and adds it to the list /// </summary> /// <param name="number">Action number</param> /// <param name="numStates">Number of target states for the action</param> /// <param name="isApplicable">True of actio is applicable</param> /// <param name="cost">Unit action cost</param> /// <returns>Created action object</returns> public WeibullMarkovAction AddAction(Int32 number, Int32 numStates, Boolean isApplicable, Double cost) { WeibullMarkovAction action = new WeibullMarkovAction(number, numStates, isApplicable, cost); Actions.Add(action); return(action); }
/// <summary> /// Constructs an object from XML element /// </summary> /// <param name="xml">XML element</param> /// <param name="errorMessage">out Error message</param> /// <returns>Constructed element, null on failure</returns> public static WeibullMarkovAction FromXmlElement(XmlElement xml, out String errorMessage) { WeibullMarkovAction action = null; errorMessage = null; try { if (xml.Name != ACTION) { throw new Exception("Expected a </" + ACTION + " ...> XML element"); } Int32 numStates = 0; if (xml.HasChildNodes) { XmlNode n = xml.FirstChild; if (n.Name == _TRANSITION_PROBABILITIES) { if (n.HasChildNodes) { numStates = n.ChildNodes.Count; } } else { throw new Exception("XML node <" + _TRANSITION_PROBABILITIES + "> expected."); } } action = new WeibullMarkovAction(0, numStates, false, 0.0); if (xml.HasAttributes) { foreach (XmlAttribute attr in xml.Attributes) { if (attr.Name == _NUMBER) { action.Number = Int32.Parse(attr.Value); } else if (attr.Name == _APPLICABILITY) { action.IsApplicable = attr.Value.Trim().ToUpper() == "1" || attr.Value.Trim().ToUpper() == "Y"; } else if (attr.Name == _NUM_TARGET_STATES) { numStates = Int32.Parse(attr.Value.Trim()); } else if (attr.Name == _UNIT_COST) { action.Cost = Double.Parse(attr.Value.Trim()); } else if (attr.Name == _DESCRIPTION) { action.Description = attr.Value; } } if (numStates > 0 && action.TranProb == null) { throw new Exception(String.Format("The declared number of target condition states is {0} but none are present in the XML", numStates)); } else if (action.TranProb != null && action.TranProb.Length != numStates) { throw new Exception(String.Format("The declared number of target condition states ({0}) different from the number of their XML nodes ({1})", numStates, action.TranProb.Length)); } } if (xml.HasChildNodes) { XmlNode n = xml.FirstChild; if (n.Name == _TRANSITION_PROBABILITIES) { foreach (XmlElement n2 in n.ChildNodes) { if (n2.Name == _TARGET_STATE) { if (n2.HasAttributes) { String s = n2.GetAttribute(_NUMBER); if (String.IsNullOrEmpty(s)) { throw new Exception("The '" + _NUMBER + "' attribute is missing in the Target-State XML element."); } Int32 i = Int32.Parse(s); if (i < 1 || i > numStates) { throw new Exception(String.Format("The value of the '" + _NUMBER + "' attribute ({0}) for the Target-State is out of range (1 - {1})", s, numStates)); } i--; s = n2.GetAttribute(_PROBABILITY); if (String.IsNullOrEmpty(s)) { throw new Exception("The '" + _PROBABILITY + "' attribute is missing in the Target-State XML element."); } action.TranProb[i] = Double.Parse(s.Trim()); } } } } } } catch (Exception ex) { errorMessage = ex.Message; action = null; } return(action); }
/// <summary> /// Constructs an object from the XML element /// </summary> /// <param name="xml">XML element</param> /// <param name="errorMessage">out Error message</param> /// <returns>Constructed object, null on failure</returns> public static WeibullMarkovConditionState FromXmlElement(XmlElement xml, out String errorMessage) { WeibullMarkovConditionState state = null; errorMessage = null; try { if (xml.Name != CONDITION_STATE) { throw new Exception("A <" + CONDITION_STATE + " ...> XML element expected."); } state = new WeibullMarkovConditionState(0.0, 0.0); String number = xml.GetAttribute(_NUMBER); if (number == null) { throw new Exception("The '" + _NUMBER + "' attribute is missing in the State XML element"); } state.Number = Int32.Parse(number); String eta = xml.GetAttribute(_ETA); if (eta == null) { throw new Exception("The '" + _ETA + "' attribute is missing in the State XML element"); } state.Eta = Double.Parse(eta); String beta = xml.GetAttribute(_BETA); if (beta == null) { throw new Exception("The '" + _BETA + "' attribute is missing in the State XML element"); } state.Beta = Double.Parse(beta); String doNothCost = xml.GetAttribute(_DO_NOTHING_COST); if (!String.IsNullOrEmpty(doNothCost)) { state.DoNothingCost = Double.Parse(doNothCost); } String tTabulated = xml.GetAttribute(_T_TABULATED); if (!String.IsNullOrEmpty(tTabulated)) { state.T = Double.Parse(tTabulated); } String f = xml.GetAttribute(_F_PROB_AT_T); if (!String.IsNullOrEmpty(f)) { state.f = Double.Parse(f); } String ff = xml.GetAttribute(_DFDT_AT_T); if (!String.IsNullOrEmpty(ff)) { state.ff = Double.Parse(ff); } String at50 = xml.GetAttribute(_T50); if (!String.IsNullOrEmpty(at50)) { state.t50 = Double.Parse(at50); String at9x = xml.GetAttribute(_T9X); String ax = xml.GetAttribute(_X); if (!String.IsNullOrEmpty(at9x) && !String.IsNullOrEmpty(ax)) { state.t9X = Double.Parse(at9x); state.x = ax; } } if (xml.HasChildNodes) { foreach (XmlNode n in xml.ChildNodes) { if (n.Name == _ACTIONS) { if (n.HasChildNodes) { foreach (XmlNode n1 in n.ChildNodes) { if (n1.Name == WeibullMarkovAction.ACTION) { WeibullMarkovAction action = WeibullMarkovAction.FromXmlElement(n1 as XmlElement, out errorMessage); if (action == null) { throw new Exception(errorMessage); } state.Actions.Add(action); } } } } if (n.Name == _POLICY) { foreach (XmlNode n2 in n.ChildNodes) { if (n2.Name == WeibullMarkovStatePolicyRecommendation.RECOMMENDATION) { WeibullMarkovStatePolicyRecommendation rec = WeibullMarkovStatePolicyRecommendation.FromXmlElement(n2 as XmlElement, out errorMessage); if (rec == null) { throw new Exception(errorMessage); } state.Recommendations.Add(rec); } } } } } } catch (Exception ex) { errorMessage = ex.Message; state = null; } return(state); }
public void EstimateModel(UCWeibullPane tp1, UCWeibullPane tp2, UCWeibullPane tp3, UCWeibullPane tp4, UCActionPane a1, UCActionPane a2, UCFailureCost failCost, UCDiscounting disc) { Boolean ok = true; try { String errorMessage = null; Double discRate = disc.AnnualRate; Boolean fcEstimate = failCost.Estimate; Boolean fcOverride = failCost.Override; Double? fcCost = failCost.FailureCost; UCWeibullPane[] tp = new UCWeibullPane[] { tp1, tp2, tp3, tp4 }; TWeibullMarkovLibrary.WeibullMarkovModel model = new WeibullMarkovModel(4, 3, fcEstimate, fcCost, fcOverride, discRate); // We are assuming throughout that the do nothing cost is zero TWeibullMarkovLibrary.WeibullMarkovConditionState[] state = new WeibullMarkovConditionState[4]; for (int i = 0; ok && i < 4; i++) { state[i] = model.AddConditionState(tp[i].Eta, tp[i].Beta, 0.0, tp[i].T, tp[i].f, tp[i].ff, out errorMessage); if (state[i] == null) { throw new Exception(errorMessage); } state[i].t50 = tp[i].t50; state[i].t9X = tp[i].t9x; state[i].x = tp[i].x; if (a1.IsApplicableToState(i)) { TWeibullMarkovLibrary.WeibullMarkovAction action = state[i].AddAction(1, 4, true, a1.GetCost(i)); for (Int32 j = 0; j < 4; j++) { action.TranProb[j] = a1.GetTranProb(i, j); } } if (a2.IsApplicableToState(i)) { TWeibullMarkovLibrary.WeibullMarkovAction action = state[i].AddAction(2, 4, true, a2.GetCost(i)); for (Int32 j = 0; j < 4; j++) { action.TranProb[j] = a2.GetTranProb(i, j); } } } if (ok) { String fileName = MapPath("~/App_Data/WeibullMarkovModel.xml"); ok = model.SaveToXml(fileName, out errorMessage); if (!ok) { throw new Exception(errorMessage); } String localSchemaPath = MapPath(SCHEMA_FILE_PATH); if (System.IO.File.Exists(localSchemaPath)) { ok = TWeibullMarkovLibrary.Utilities.ValidateXMLvsXSD(fileName, localSchemaPath, null, out errorMessage); if (!ok) { throw new Exception(errorMessage); } } } } catch (Exception ex) { labelError.Text = ex.Message; labelInfo.Text = String.Empty; labelError.Visible = true; labelInfo.Visible = false; } }