Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I construct an algorithm to calculate the values of objects based on a set of formulas?
    primarykey
    data
    text
    <p>So let's say I have a collection of the following Field objects:</p> <pre><code>public class Field { public string Formula { get; set; } public string Name { get; set; } public double Value { get; set; } } </code></pre> <p>Every Field has a Name that is unique. Some fields have a Formula. The formula is a mathematical expression that is used to compute the Field's value where the inputs are other fields in the collection represented by their Name. I given collection might look something like this:</p> <ul> <li>Name: "A"; Formula: ""; Value: 0.0</li> <li>Name: "B"; Formula: ""; Value: 0.0</li> <li>Name: "C"; Formula: "A + B"; Value: 0.0</li> <li>Name: "D"; Formula: "E + F"; Value: 0.0</li> <li>Name: "E"; Formula: "C + 14"; Value: 0.0</li> <li>Name: "F"; Formula: ""; Value: 0.0</li> <li>Name: "G"; Formula "D * .05"; Value: 0.0</li> </ul> <p>Now let's say the Value of the Field where Name == "A" changes to 25.0. When that occurs, C would need to be calculated as '25.0 + 0.0", which would then require E to be calculated. But calculating E first requires that D be calculated, and calculating D would require G to be calculated as well.</p> <p>Is there a standard algorithm or data structure that would make determining a) which field values should be calculated and in what order when the Value of any given Field changes, and b) calculating those fields in the proper order necessary?</p> <p>As a bonus it might be nice to determine if there are any infinite (left?) recursion in a configuration of the formulas in a collection in a scenario like the following:</p> <ul> <li>Name: "A"; Formula: "B"; Value: 0.0</li> <li>Name: "B"; Formula: "A * 10"; Value: 0.0</li> <li>Name: "C"; Formula: "A + B"; Value: 0.0</li> <li>Name: "D"; Formula: "B + C"; Value: 0.0</li> </ul> <p>It's been awhile since I've done something like this (not since college) and would appreciate any help to set me off on the right foot. Let me know if anything I have written needs clarification.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload