Below I want to calculate a ratio x:5, meaning for every Xminutes you are on, you are off or wait 5 mins to turn back on. On/Off is a complete cycle. Below are three cycles. I am trying to come up with a good way to do this using linq. Basically I am going to return how much time you have to wait or stay off before you start another cycle if that make sense.
So below you have a
ArrayOnOff[0]= "1:00AM On";
ArrayOnOff[1]= "1:01AM Off";
ArrayOnOff[2]= "1:02AM On";
ArrayOnOff[3]= "1:03AM Off";
ArrayOnOff[4]= "1:04AM On";
ArrayOnOff[5]= "1:05AM Off";
CycleId StateOnOFF
1 1:00AM -on
2 1:01AM -off
3 1:02AM -on
4 1:03AM -off
5 1:04AM -on
6 1:05AM -off
Loading
VulpesPosted Jul 2, 2013, 8:31 AM
Cycle[] cycles = new Cycle[ArrayOnOff.Length];
for(int i = 0; i < 6; i++) cycles[i] = GetCycleInfo(ArrayOnOff[i]);
double totalTimeUsed = 0;
for(int i = 1; i < cycles.Length; i++)
{
double diff = (cycles[i].TimeTag - cycles[i- 1].TimeTag).TotalSeconds;
if (i % 2 == 1)
totalTimeUsed += diff;
else
totalTimeUsed -= diff/5.0;
if (totalTimeUsed < 0) totalTimeUsed = 0;
}
totalTimeUsed -= (DateTime.Now - cycles[cycles.Length - 1].TimeTag).TotalSeconds / Ratio;
This isn't an ideal case for using LINQ but, if you must use it, then I'd try something like this:
double totalTimeUsed = 0;
Cycle prev = null;
ArrayOnOff.Select(aof => GetCycleInfo(aof)).Select((c, i) =>
{
if (i > 0)
{
double diff = (c.TimeTag - prev.TimeTag).TotalSeconds;
if (i % 2 == 1)
totalTimeUsed += diff;
else
totalTimeUsed -= diff/5.0;
if (totalTimeUsed < 0) totalTimeUsed = 0;
}
prev = c;
return 0.0; // need to return something although we don't use it
}).ToArray(); // add ToArray() to execute immediately
totalTimeUsed -= (DateTime.Now - prev.TimeTag).TotalSeconds / Ratio;
David SmithPosted Jul 2, 2013, 11:16 AM
VulpesPosted Jul 2, 2013, 10:31 AM
Here, I used 'for' because I needed the index of the element in the array to check whether it was odd or even and hence whether it was an 'off' or an 'on'.
David SmithPosted Jul 2, 2013, 10:23 AM
VulpesPosted Jul 2, 2013, 10:06 AM
David SmithPosted Jul 2, 2013, 9:48 AM
David SmithPosted Jul 2, 2013, 4:06 AM
The input file will populate the array below. The logic I want to simplify and come up with a better way of doing the logic below using linq if possible.
ArrayOnOff[0]= "1:00AM On";
ArrayOnOff[1]= "1:01AM Off";
ArrayOnOff[2]= "1:02AM On";
ArrayOnOff[3]= "1:03AM Off";
ArrayOnOff[4]= "1:04AM On";
ArrayOnOff[5]= "1:05AM Off";
Formula: The below formula calculates the amount of time you have left to stay on before exceeding the max. Everything needs to be in seconds
Max = 10min
Ratio = 5 (off/on)
TimeAvailable = Max - [OffTime - OnTime] + [DateTime.Now - OffTime] / Ratio
IEnumerator enumerator = ArrayOnOff.GetEnumerator();
enumerator.MoveNext();
previousOn = GetCycleInfo((string)enumerator.Current);
enumerator.MoveNext();
previousOff = GetCycleInfo((string)enumerator.Current);
double totalTimeUsed = (previousOff.TimeTag - previousOn.TimeTag).TotalSeconds;
for (int i = 0; i < (ArrayOnOff.Count() / 2) - 1; ++i)
{
enumerator.MoveNext();
Cycle newOn = GetCycleInfo((string)enumerator.Current);
en.MoveNext();
Cycle newOff = GetCycleInfo((string)enumerator.Current);
double timeWaited = (newOn.TimeTag - previousOff.TimeTag).TotalSeconds;
totalTimeUsed -= (newOn.TimeTag - previousOff.TimeTag).TotalSeconds / Ratio;
// bought back all time?
if (totalTimeUsed < 0.0)
{
totalTimeUsed = 0.0;
}
previousOn = newOn;
previousOff = newOff;
totalTimeUsed += (previousOff.TimeTag - previousOn.TimeTag).TotalSeconds;
}
totalTimeUsed -= (DateTime.Now - previousOff.TimeTag).TotalSeconds / Ratio;
Kunal VaishyaPosted Jul 2, 2013, 3:10 AM
Can you provide more details so i can help you.