Example #1
0
        //Backbone to be written now.//Two arrays ....both need to give a right combination against required weight.
        private static RowsBalanceResult BalanceWeight(Tuple rowPair, DistributionMatrix bMatrix)
        {
            BalanceAction balAction          = new BalanceAction();
            long          weightToMove       = 0;
            long          primaryRowWeight   = bMatrix.WeightPercentMatrix[rowPair.First, 0];
            long          secondaryRowWeight = bMatrix.WeightPercentMatrix[rowPair.Second, 0];

            if (primaryRowWeight < bMatrix.PercentWeightToSacrifice) //
            {
                weightToMove = bMatrix.PercentWeightToSacrifice - primaryRowWeight;
                weightToMove = (weightToMove * bMatrix.TotalWeight) / 100;
                balAction    = BalanceAction.GainWeight;
            }
            else
            {
                weightToMove = primaryRowWeight - bMatrix.PercentWeightToSacrifice;
                weightToMove = (weightToMove * bMatrix.TotalWeight) / 100;
                balAction    = BalanceAction.LoseWeight;
            }

            long[] primaryRowData   = new long[bMatrix.MatrixDimension.Cols];
            long[] secondaryRowData = new long[bMatrix.MatrixDimension.Cols];

            //Fills the local copy of two rows to be manipulated
            for (int i = 0; i < bMatrix.MatrixDimension.Cols; i++)
            {
                primaryRowData[i]   = bMatrix.Matrix[rowPair.First, i];
                secondaryRowData[i] = bMatrix.Matrix[rowPair.Second, i];
            }
            RowsBalanceResult rbResult = null;

            switch (balAction)
            {
            case BalanceAction.GainWeight:
                rbResult = RowBalanceGainWeight(rowPair, primaryRowData, secondaryRowData, weightToMove, bMatrix);
                break;

            case BalanceAction.LoseWeight:
                rbResult = RowBalanceLoseWeight(rowPair, weightToMove, bMatrix);
                break;

            default:
                break;
            }

            return(rbResult);
        }
Example #2
0
        /*  public static RowsBalanceResult BucketSelectionFromTuples(ArrayList allTuples,DistributionMatrix bMatrix)
          {
              int primaryRow; //This row would be modified to acomodae required weight.
              int secondaryRow; //This row would be consulted to make primaryRow workable
              for (int i = 0; i < allTuples.Count; i++)
              {
                  Tuple tempTuple = allTuples[i];
                  int firstWeight = bMatrix.WeightMatrix[tempTuple.First][0];
                  int secondWeight = bMatrix.WeightMatrix[tempTuple.second][0];
            
                  // Calculate which Move is cost-effective, Moving data from first to second or moving from second to first
                  if (CalcMoveCost(firstWeight, bMatrix.PercentWeightToSacrifice) <= CalcMoveCost(secondWeight, bMatrix.PercentWeightToSacrifice))
                  {
                      primaryRow = tempTuple.First;
                      secondarayRow = tempTuple.Second;
                  }
                  else
                  {
                      primaryRow = tempTuple.Second;
                      secondarayRow = tempTuple.First;
                  }
              }    
          }    */


        /*// Calculates cost to make sourceweight to be equal to desired weight. This can be in + and -. We take absolute value though.
        public static int CalcMoveCost(int sourceWeight,int toMoveWeight)
        {
            if (sourceWeight < toMoveWeight) //
                return (toMoveWeight - sourceWeight);

            if (sourceWeight > toMoveWeight)
                return (sourceWeight - toMoveWeight);

            if (sourceWeight == toMoveWeight)
                return 0;
        }*/

        //Backbone to be written now.//Two arrays ....both need to give a right combination against required weight.
        private static RowsBalanceResult BalanceWeight(Tuple rowPair, DistributionMatrix bMatrix)
        {
            BalanceAction balAction = new BalanceAction();
            long weightToMove = 0;
            long primaryRowWeight = bMatrix.WeightPercentMatrix[rowPair.First, 0];
            long secondaryRowWeight = bMatrix.WeightPercentMatrix[rowPair.Second, 0];

            if (primaryRowWeight < bMatrix.PercentWeightToSacrifice) //
            {
                weightToMove = bMatrix.PercentWeightToSacrifice - primaryRowWeight;
                weightToMove = (weightToMove * bMatrix.TotalWeight) / 100;
                balAction = BalanceAction.GainWeight;
            }
            else
            {
                weightToMove = primaryRowWeight - bMatrix.PercentWeightToSacrifice;
                weightToMove = (weightToMove * bMatrix.TotalWeight) / 100;
                balAction = BalanceAction.LoseWeight;
            }

            long[] primaryRowData = new long[bMatrix.MatrixDimension.Cols];
            long[] secondaryRowData = new long[bMatrix.MatrixDimension.Cols];

            //Fills the local copy of two rows to be manipulated
            for (int i = 0; i < bMatrix.MatrixDimension.Cols; i++)
            {
                primaryRowData[i] = bMatrix.Matrix[rowPair.First, i];
                secondaryRowData[i] = bMatrix.Matrix[rowPair.Second, i];
            }
            RowsBalanceResult rbResult= null;
            switch (balAction)
            {
                case BalanceAction.GainWeight:                    
                    rbResult = RowBalanceGainWeight(rowPair, primaryRowData, secondaryRowData, weightToMove, bMatrix);
                    break;
                case BalanceAction.LoseWeight:
                    rbResult = RowBalanceLoseWeight(rowPair, weightToMove, bMatrix);
                    break;
                default:
                    break;
            }

            return rbResult;
        }
Example #3
0
 protected void AddFire(BalanceAction <TUnit> action)
 {
     action.Init(SelfUnit, this, BalanceActionType.Fire);
     FireData.Add(action);
 }