Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Distribution.cs
1using System.Text;
2using System.Threading.Tasks;
3using System.Xml;
4using Waher.Content;
6
8{
12 public abstract class Distribution : SimulationNode, IDistribution
13 {
14 private string id;
15 private double n;
16 private double timeCycleUnits;
17
24 : base(Parent, Model)
25 {
26 }
27
31 public string Id => this.id;
32
36 public double N => this.n;
37
41 public double TimeCycleUnits => this.timeCycleUnits;
42
47 public override Task FromXml(XmlElement Definition)
48 {
49 this.id = XML.Attribute(Definition, "id");
50 this.n = XML.Attribute(Definition, "N", 0.0);
51
52 return Task.CompletedTask;
53 }
54
58 public override Task Initialize()
59 {
60 this.Model.Register(this);
61 this.timeCycleUnits = this.Model.TimeCycleUnits;
62
63 return base.Initialize();
64 }
65
73 public virtual int CheckTrigger(double t1, double t2, int NrCycles)
74 {
75 double p;
76
77 if (t1 < t2)
78 p = this.GetCumulativeProbability(t2, NrCycles) - this.GetCumulativeProbability(t1, NrCycles);
79 else
80 {
81 p = this.GetCumulativeProbability(this.Model.TimeCycleUnits, NrCycles - 1) - this.GetCumulativeProbability(t1, NrCycles - 1);
82 p += this.GetCumulativeProbability(t2, NrCycles) - this.GetCumulativeProbability(0, NrCycles);
83 }
84
85 if (p <= 0)
86 return 0;
87
88 p *= this.N;
89
90 int Nr = (int)p;
91 if (Nr > 0)
92 p -= Nr;
93
94 if (p > 0 && this.Model.GetRandomDouble() <= p)
95 Nr++;
96
97 return Nr;
98 }
99
106 protected abstract double GetCumulativeProbability(double t, int NrCycles);
107
112 public void ExportPdfOnceOnly(StringBuilder Output)
113 {
114 if (!this.exported)
115 {
116 this.exported = true;
117 this.ExportPdf(Output);
118 }
119 }
120
121 private bool exported;
122
127 public virtual void ExportPdf(StringBuilder Output)
128 {
129 Output.Append(this.id);
130 Output.Append("PDF(t):=");
131 if (this.n != 1)
132 {
133 Output.Append(CommonTypes.Encode(this.n));
134 Output.Append('*');
135 }
136 Output.Append('(');
137 this.ExportPdfBody(Output);
138 Output.AppendLine(");");
139 }
140
145 public abstract void ExportPdfBody(StringBuilder Output);
146 }
147}
Root node of a simulation model
Definition: Model.cs:49
void Register(IDistribution Distribution)
Registers a distribution with the runtime environment of the model.
Definition: Model.cs:299
double TimeCycleUnits
Time cycle, in number of TimeUnit.
Definition: Model.cs:145
double GetRandomDouble()
Generates a new floating-point value between 0 and 1, using a cryptographic random number generator.
Definition: Model.cs:784
Abstract base class for distributions
Definition: Distribution.cs:13
virtual void ExportPdf(StringBuilder Output)
Exports the PDF function.
virtual int CheckTrigger(double t1, double t2, int NrCycles)
Check if distribution has a sample within the time period.
Definition: Distribution.cs:73
abstract void ExportPdfBody(StringBuilder Output)
Exports the PDF function body.
abstract double GetCumulativeProbability(double t, int NrCycles)
The Cumulative Distribution Function (CDF) of the distribution, excluding intensity (N).
double TimeCycleUnits
Time cycle, in number of Model.TimeUnit.
Definition: Distribution.cs:41
Distribution(ISimulationNode Parent, Model Model)
Abstract base class for distributions
Definition: Distribution.cs:23
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: Distribution.cs:47
void ExportPdfOnceOnly(StringBuilder Output)
Exports the PDF function, if not already exported.
override Task Initialize()
Initialized the node before simulation.
Definition: Distribution.cs:58
Abstract base class for simulation nodes
ISimulationNode Parent
Parent node in the simulation model.
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...