Beispiel #1
0
        private static float GetFitness(
            int[] p,
            int stepCount,
            Dictionary <StateStep, float> cDict)
        {
            float cost_total = 0.0f;

            for (int iStep = 0; iStep < stepCount; iStep++)
            {
                var id = new StateStep(p[iStep], iStep);
                cost_total += cDict[id];
            }

            return(cost_total);
        }
Beispiel #2
0
 public void UpdateState(StateStep newStateStep)
 {
     DispatcherHelper.CheckBeginInvokeOnUI((Action)(() => this.StateStep = newStateStep));
 }
        private static void SearchTask(MGInput m,
                                       long CombStart,
                                       long CombEnd,
                                       ref long globalBestComb,
                                       ref float globalLowestCost,
                                       TaskParams p,
                                       Action <int, float> progressCallback,
                                       int taskID)
        {
            // An array we use to store a candidate solution.
            int[,] u_thr_candidate = new int[m.genCount, p.stepCount];

            int[] states = new int[m.genCount];

            long  localBestComb;
            float localLowestCost = Mathf.Infinity;

            float[] dtime_norm = new float[m.genCount];
            float[] utime_norm = new float[m.genCount];
            for (int iGen = 0; iGen < m.genCount; iGen++)
            {
                dtime_norm[iGen] = m.thr_min_dtime[iGen] / p.stepSize;
                utime_norm[iGen] = m.thr_min_utime[iGen] / p.stepSize;
            }

            for (long iComb = CombStart; iComb < CombEnd; iComb++)
            {
                long  currComb = iComb;
                float c_total  = 0.0f;
                // 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1
                // We assume the state is valid until proven otherwise.
                bool isStateValid = true;

                for (int iStep = 0; iStep < p.stepCount; iStep++)
                {
                    float c_thr_sum_step = 0.0f;

                    currComb = IntToBinary(currComb, m.genCount, states);

                    for (int ithr = 0; ithr < m.genCount; ithr++)
                    {
                        // Get the binary digit representing the on/off sate
                        // for our current thr and step.
                        u_thr_candidate[ithr, iStep] = states[ithr];

                        if (!isStateValid)
                        {
                            break;
                        }

                        // The total cost incurred by the thrs over the time
                        // interval of the step.
                        c_thr_sum_step += states[ithr] * p.cost_thr_at_max[ithr]
                                          * p.stepSize;

                        // The total power the thrs can give at any t moment
                        // during the step. It's constant throughout the step
                        // because the state of units is constant throughout
                        // the time interval of the step.
                    } // END thr

                    int ii = BinaryToInt(states);
                    var id = new StateStep(ii, iStep);


                    c_total += c_thr_sum_step + p.purchaseDict[id];

                    // Pruning. If the total cost is already higher than the
                    // lowest cost then there's no way this combination can
                    // be the optimal one, so we abandon verifying it and break.
                    if (c_total > globalLowestCost)
                    {
                        break;
                    }
                } // END Step

                for (int ithr = 0; ithr < m.genCount; ithr++)
                {
                    if (!MGHelper.IsStateValid(GetRow(u_thr_candidate, ithr),
                                               dtime_norm[ithr], utime_norm[ithr], p.stepCount))
                    {
                        isStateValid = false;
                        break;
                    }
                }

                if (isStateValid)
                {
                    if (c_total < localLowestCost)
                    {
                        progressCallback(taskID,
                                         (float)((double)(iComb - CombStart)
                                                 / (double)(CombEnd - CombStart)));
                        localLowestCost = c_total;
                        localBestComb   = iComb;

                        if (localLowestCost < Volatile.Read(ref globalLowestCost))
                        {
                            Interlocked.Exchange(ref globalLowestCost, localLowestCost);
                            Interlocked.Exchange(ref globalBestComb, localBestComb);
                        }
                    }
                }
            } // END Combination
        }