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

VulpesRank #55 · 29.2K reputationPosted Jul 2, 2013, 4:31 AM EDT
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;
Kunal VaishyaRank #990 · 2K reputationPosted Jul 1, 2013, 11:10 PM EDT
Can you provide more details so i can help you.
David SmithRank #999 · 2K reputationPosted Jul 2, 2013, 12:06 AM EDT
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;
David SmithRank #999 · 2K reputationPosted Jul 2, 2013, 5:48 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 2, 2013, 6:06 AM EDT
David SmithRank #999 · 2K reputationPosted Jul 2, 2013, 6:23 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 2, 2013, 6:31 AM EDT
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 SmithRank #999 · 2K reputationPosted Jul 2, 2013, 7:16 AM EDT