Package com.ike.rwdccalc.Utilities

Source Code of com.ike.rwdccalc.Utilities.Iterate

package com.ike.rwdccalc.Utilities;

import java.util.ArrayList;

import com.ike.rwdccalc.Objects.DistancePath;
import com.ike.rwdccalc.Objects.MissionPath;
import com.ike.rwdccalc.Objects.RadiusIteration;
// This will handle the brute force aspect of the calculation
// It will run CalculationTools automagically to find the best
// distances for each plane
public class Iterate {

  // Initiate a new static ArrayList so we can use it between methods
  static ArrayList<RadiusIteration> distance = new ArrayList<RadiusIteration> ();

  public static RadiusIteration doIterations(double footprintWidth, double pricePerHour, double groundSpeed, double increment) {

    // Initiate a new RadiusIteration object, this will be used to find the quickest path
    RadiusIteration lowest = new RadiusIteration();
    // Only look for two planes, we know for out circumstances that
    // two planes is the best option, and three or four planes just
    // takes too long
    for (int x = 2; x <= 2; x++) {
      // Find the best path based on the information given
      lowest = getLowest(getRadiusDifference(x, footprintWidth,
          pricePerHour, groundSpeed, increment));
    }
    // Return the answer
    return lowest;

  }

  /////////////////////////
  // getRadiusDifference //
  /////////////////////////
  public static ArrayList<RadiusIteration> getRadiusDifference(int planes,
      double footprintWidth, double pricePerHour, double groundSpeed, double increment) {

    DistancePath small = new DistancePath()// Farthest inside plane
    DistancePath middle = new DistancePath()// Middle inside plane
    DistancePath bigger = new DistancePath()// Middle outside plane
    DistancePath big = new DistancePath();    // Farthest outside plane

    double time = 0;   // Time will be calculated based on speed and distance0
    double price = 0// Price will be calculated based on price per hour and time
    double speed = groundSpeed * 5280; // Convert miles per hour to feet per hour

    int max = 2 * 5280// Set the outside radius
    int min = 0;    // Set the inside
    int mid = 0;    // Set the middle
    int first = 0;    // Used as place holders
    int second = 0;    // Used as place holders

    // Only calculate for the number of planes that we have
    switch (planes) {
    // Two planes
    case 2:
      for (double x = 1; x < max; x+=increment) {
        // Not much to be done, find the inside distnace
        small = CalculatingTools.calculate(true, true, 0, x,
            footprintWidth);
        // Find the outside distance
        big = CalculatingTools.calculate(true, true, x, max,
            footprintWidth);

        // Find out which plane takes longer
        time = Math.max(small.getAnswerInFeet(), big.getAnswerInFeet());
        // Base the time calculation off of that
        time = time / speed;
        time -= 0.1;

        // Find out the price from how long it took
        price = time * pricePerHour * 2;

        // Add a new object to distnace
        distance.add(new RadiusIteration(
          price,
          new String[] {
            "Plane One: " + x, "Plane Two: " + max
          },
          new MissionPath[] {
            new MissionPath(
              small.getDistanceFromCenter(),
              small.getAngleFromCenter(),
              small.getSegmentType()),
            new MissionPath(
              big.getDistanceFromCenter(),
              big.getAngleFromCenter(),
              big.getSegmentType())
          },
          x, time
        ));
       
      }
      break;
    // Three planes
    case 3:
      for (int x = 1; x < max; x+=increment) {
        first = max - x;
        small = CalculatingTools.calculate(true, true, 0, x,
            footprintWidth);
        for (int y = x + 1; y < first; y+=10) {
          min = x + y;
          middle = CalculatingTools.calculate(true, true, x, min,
              footprintWidth);
          big = CalculatingTools.calculate(true, true, min, max,
              footprintWidth);

          time = Math.max(small.getAnswerInFeet(),
              middle.getAnswerInFeet());
          time = Math.max(time, big.getAnswerInFeet());
          time = time / speed;

          price = time * pricePerHour * 3;

          distance.add(new RadiusIteration(
            price,
            new String[] {
              "Plane One: " + x, "Plane Two: " + min, "Plane Three " + max
            },
            new MissionPath[] {
              new MissionPath(
                small.getDistanceFromCenter(),
                small.getAngleFromCenter(),
                small.getSegmentType()),
              new MissionPath(
                middle.getDistanceFromCenter(),
                middle.getAngleFromCenter(),
                middle.getSegmentType()),
              new MissionPath(
                big.getDistanceFromCenter(),
                big.getAngleFromCenter(),
                big.getSegmentType())
            },
            x, time
          ));
        }
      }
      break;
    // Fource planes
    case 4:
      for (int x = 1; x < max; x+=increment) {
        first = max - x;
        small = CalculatingTools.calculate(true, true, 0, x,
            footprintWidth);
        for (int y = x + 1; y < first; y+=10) {
          min = x + y;
          second = max - min;
          middle = CalculatingTools.calculate(true, true, x, min,
              footprintWidth);
          for (int z = y + 1; z < second; z+=10) {
            mid = min + z;
            bigger = CalculatingTools.calculate(true, true, min,
                mid, footprintWidth);
            big = CalculatingTools.calculate(true, true, mid, max,
                footprintWidth);

            time = Math.max(small.getAnswerInFeet(),
                middle.getAnswerInFeet());
            time = Math.max(time, bigger.getAnswerInFeet());
            time = Math.max(time, big.getAnswerInFeet());

            time = time / speed;

            price = time * pricePerHour * 4;

            distance.add(new RadiusIteration(
              price,
              new String[] {
                "Plane One: " + x, "Plane Two: " + min, "Plane Three " + mid, "Plane Four: " + max
              },
              new MissionPath[] {
                new MissionPath(
                  small.getDistanceFromCenter(),
                  small.getAngleFromCenter(),
                  small.getSegmentType()),
                new MissionPath(
                  middle.getDistanceFromCenter(),
                  middle.getAngleFromCenter(),
                  middle.getSegmentType()),
                new MissionPath(
                  bigger.getDistanceFromCenter(),
                  bigger.getAngleFromCenter(),
                  bigger.getSegmentType()),
                new MissionPath(
                  big.getDistanceFromCenter(),
                  big.getAngleFromCenter(),
                  big.getSegmentType())
              },
              x, time
            ));
          }
        }
      }
      break;
    default:
      break;
    }

    return distance;
  }

  ///////////////
  // getLowest //
  ///////////////
  private static RadiusIteration getLowest(ArrayList<RadiusIteration> difference) {

    RadiusIteration lowest = new RadiusIteration();
    // We need to go through the whole array
    for (int x = 0; x < difference.size() - 1; x++) {
      if (x == 0) {
        // Since this is the first go, let's compare the first two prices together
        if (Math.min(
            difference.get(x).getPrice(),
            difference.get(x + 1).getPrice()) == difference.get(x).getPrice()) {
          // Set lowest to the lower price
          lowest = difference.get(x);
        } else {
          // Set lowest to the lower price
          lowest = difference.get(x + 1);
        }
      } else {
        // This isn't the first go, so pick up where we left off
        if (Math.min(lowest.getPrice(),
            difference.get(x + 1).getPrice()) == difference.get(x + 1).getPrice()) {
          // Only update if the lowest price has changed
          lowest = difference.get(x + 1);
        }
      }
    }

    // Return the lowest answer
    return lowest;
  }
}
TOP

Related Classes of com.ike.rwdccalc.Utilities.Iterate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.