Exemple #1
0
        public BatteryBalancer(string values, int cellsTotal)
        {
            Cells       = new List <BatteryCell>();
            _totalValue = 0;
            var valArray = values.Split('\n');

            if (valArray.Length % cellsTotal != 0)
            {
                throw new ArgumentOutOfRangeException("The total number of values must be divisible by the number of cells total");
            }

            _totalCells = cellsTotal;
            var totalBatteriesInEachCell = valArray.Length / cellsTotal;

            var bufferBatteryCell = new BatteryCell();

            for (int i = 0; i < valArray.Length; i++)
            {
                bufferBatteryCell.Add(i + 1, valArray[i]);

                if ((i + 1) % totalBatteriesInEachCell == 0)
                {
                    _totalValue += bufferBatteryCell.TotalValue;
                    Cells.Add(bufferBatteryCell);
                    bufferBatteryCell = new BatteryCell();
                }
            }

            _targetCellValue = _totalValue / _totalCells;
        }
Exemple #2
0
        private void swap(BatteryCell sourceCell, int sourceIndex, BatteryCell targetCell, int targetIndex, bool useActIndex)
        {
            var sourceBatteryValue = sourceCell[sourceIndex];
            var targetBatteryValue = targetCell[targetIndex];

            sourceCell.RemoveAt(sourceIndex);
            targetCell.RemoveAt(targetIndex);

            sourceCell.Add(targetBatteryValue);
            targetCell.Add(sourceBatteryValue);
        }
Exemple #3
0
        private void swap(BatteryCell sourceCell, int sourceIndex, BatteryCell targetCell, int targetIndex)
        {
            var sourceBatteryValue = sourceCell.GetWhereIndex(sourceIndex);
            var targetBatteryValue = targetCell.GetWhereIndex(targetIndex);

            sourceCell.RemoveWhereIndex(sourceIndex);
            targetCell.RemoveWhereIndex(targetIndex);

            sourceCell.Add(targetBatteryValue);
            targetCell.Add(sourceBatteryValue);
        }
Exemple #4
0
        private void BalanceCells(BatteryCell a, BatteryCell b)
        {
            var balanceAvailable = true;
            var movedList        = new List <int>();

            while (balanceAvailable)
            {
                //get the totals of each cell
                var aTotal = a.TotalValue;
                var bTotal = b.TotalValue;

                //get the total summation of all values
                var total = aTotal + bTotal;
                //get the average value
                var avg = total / 2;

                //find the difference between totals and avg val for ea cell
                var aTotalAvgDiff = avg - aTotal;
                var bTotalAvgDiff = avg - bTotal;

                var greatestDiff = (aTotalAvgDiff > bTotalAvgDiff) ? aTotalAvgDiff : bTotalAvgDiff;

                //find the difference between all swaps
                var swapDiffs = new List <KeyValuePair <KeyValuePair <int, int>, int> >();
                for (int i = 0; i < a.Count; i++)
                {
                    for (int j = 0; j < b.Count; j++)
                    {
                        swapDiffs.Add(new KeyValuePair <KeyValuePair <int, int>, int>(new KeyValuePair <int, int>(i, j), Math.Abs(a[i].Value - b[j].Value)));
                    }
                }

                //perform the swap where the difference is the closet to the diff between the total and avg
                var closestPair  = new KeyValuePair <KeyValuePair <int, int>, int>();
                var smallestPair = new KeyValuePair <KeyValuePair <int, int>, int>();

                var closestValue  = int.MaxValue;
                var smallestValue = int.MaxValue;
                foreach (var diff in swapDiffs)
                {
                    if (diff.Value < smallestValue)
                    {
                        smallestValue = diff.Value;
                        smallestPair  = diff;
                    }

                    var currentDiff = greatestDiff - diff.Value;
                    if (movedList.Contains(currentDiff))
                    {
                        continue;
                    }

                    if (currentDiff > 0 && currentDiff < closestValue)
                    {
                        closestValue = currentDiff;
                        closestPair  = diff;
                    }
                }

                if (smallestValue > greatestDiff)
                {
                    //break!
                    //repeat until the difference of the values are greater than the difference between the avg
                    balanceAvailable = false;
                }
                else
                {
                    if (!(closestPair.Value == 0 && closestPair.Key.Key == 0 && closestPair.Key.Value == 0))
                    {
                        swap(a, closestPair.Key.Key, b, closestPair.Key.Value, true);
                        movedList.Add(closestValue);
                    }
                    else
                    {
                        balanceAvailable = false;
                    }
                }
            }
        }