Beispiel #1
0
  public void handleMoving(String[] fields) {
    String volunteerString = fields[1];
    String startString = fields[2];
    String destinationString = fields[3];
    int transitTime = Integer.parseInt(fields[4]);

    Log.i(
        "MOVING: volunteer=%s start=%s destination=%s transitTime=%d",
        volunteerString, startString, destinationString, transitTime);

    Volunteer volunteer = model.getVolunteerByName(volunteerString);
    if (volunteer == null) {
      Log.w("Haven't seen moving volunteer %s yet", volunteerString);
      return;
    }

    Location start = model.getLocationByName(startString);
    if (start == null) {
      Log.w("Haven't see start location %s yet", startString);
      return;
    }

    Location destination = model.getLocationByName(destinationString);
    if (destination == null) {
      Log.w("Haven't seen destination location %s yet", destinationString);
      return;
    }

    if (!start.getName().equals(startString))
      Log.w(
          "MOVING violation: %s not at %s but rather at %s",
          volunteerString, startString, start.getName());

    volunteer.startMoving(destination, transitTime);
  }
Beispiel #2
0
  public String getVolunteerListTextually() {
    String ret = "";

    if (volunteer != null) {
      for (Volunteer volunteer1 : volunteer) {
        ret += (volunteer1.toString() + "," + '\'');
      }
    }

    return ret;
  }
Beispiel #3
0
  public void handleVolunteer(String[] fields) {

    // First, add a volunteer to the model when a VOLUNTEER message arrived
    // from the server

    String volunteerString = fields[1];
    String locationString = fields[2];
    int score = Integer.parseInt(fields[3]);

    Log.i("VOLUNTEER: volunteer=%s location=%s score=%d", volunteerString, locationString, score);

    Volunteer volunteer = model.getVolunteerByName(volunteerString);
    Location location = model.getLocationByName(locationString);
    if (location == null) {
      Log.w("Haven't seen volunteer location %s yet", locationString);
      return;
    }

    if (volunteer != null) model.removeVolunteer(volunteer);
    volunteer = new Volunteer(model, volunteerString, score, location);

    // Secondly, if the volunteer just added is "nickname", send a command
    // to
    // the server, whether to walk or to move.
    if (volunteerString.equals(this.nickname)) {

      // use nextRequest() to decide which is the best request to carry on
      Request theNextRequest = nextRequest(volunteer);

      // if nextRequest() can't give out a request, then move to the
      // nearest location with requests
      if (theNextRequest == null) {
        moveToNearest(volunteer);
        return;
      }

      // if nextRequest() gives a request at the same location with
      // "nickname", then walk that requester, otherwise move to that
      // requester
      if (theNextRequest.getStart().equals(volunteer.getCurrentLocation())) {
        String s = "walk " + theNextRequest.getName();
        connector.writeLine(s);
      } else {
        String s = "move " + theNextRequest.getStart().getName();
        connector.writeLine(s);
      }
    }
  }
Beispiel #4
0
  // When the request returned from nextRequest() is null, call this method to
  // move to the nearest location with requests
  public void moveToNearest(Volunteer volunteer) {

    double min = Double.MAX_VALUE;
    String building = "PMU";

    for (Location location : model.getLocations()) {
      if (location.getRequests().size() > 1) {
        if (distance(volunteer.getCurrentLocation(), location) < min) {
          min = distance(volunteer.getCurrentLocation(), location);
          building = location.getName();
        }
      }
    }

    String s = "move " + building;
    connector.writeLine(s);
  }
Beispiel #5
0
  public void handleWalking(String[] fields) {
    String volunteerName = fields[1];
    String requesterName = fields[2];
    String startString = fields[3];
    String destinationString = fields[4];
    int transitTime = Integer.parseInt(fields[5]);

    Log.i(
        "WALKING: volunteer=%s requester=%s start=%s destination=%s transitTime=%d",
        volunteerName, requesterName, startString, destinationString, transitTime);

    Volunteer volunteer = model.getVolunteerByName(volunteerName);
    if (volunteer == null) {
      Log.w("Haven't seen walking volunteer %s yet", volunteerName);
      return;
    }
    Request request = model.getRequestByName(requesterName);
    if (request == null) {
      Log.w("Haven't seen requester %s yet", requesterName);
      return;
    }

    Location start = model.getLocationByName(startString);
    if (start == null) {
      Log.w("Haven't seen start location %s yet", startString);
      return;
    }

    Location destination = model.getLocationByName(destinationString);
    if (destination == null) {
      Log.w("Haven't seen destination location %s yet", destinationString);
      return;
    }

    if (!start.getName().equals(startString))
      Log.w(
          "WALKING violation: %s not at %s but rather at %s",
          volunteerName, startString, start.getName());

    volunteer.startWalking(request, transitTime);
  }
Beispiel #6
0
  // filter locations: choose those locations within a distance = 675 (from
  // ARMS to ELLT = 670.0) from the volunteer "nickname"
  public HashSet<Location> filterLocations(Volunteer volunteer) {
    HashSet<Location> nearbyLocations = new HashSet<Location>();

    for (Location location : this.model.getLocations()) {
      // if a location is nearby and is not the "nickname"'s location,
      // then, add that location to nearbyLocations
      if (distance(volunteer.getCurrentLocation(), location) < 675.0
          && !volunteer.getCurrentLocation().equals(location)) nearbyLocations.add(location);
    }

    // iterate through each nearby location to filter and sort requests
    for (Location location : nearbyLocations) {

      // count the number of other volunteers moving to the location
      // whose distance to that location are closer then "nickname"'s.
      int count = 0;
      double[] b = location.getXY();
      for (Volunteer otherVolunteer : model.getVolunteers()) {
        double[] a = otherVolunteer.getDestination();

        if (otherVolunteer.getDestination() != null
            && Math.abs(a[0] + a[1] - b[0] - b[1]) < 0.001
            && distance(otherVolunteer.getCurrentPosition(), location)
                < distance(volunteer.getCurrentLocation(), location)) {
          count++;
        }
      }

      // count the number of other volunteers already at the location
      count += location.getVolunteers().size();

      // Create an list of requests to be sorted at each nearby location
      List<Request> requestList = new ArrayList<Request>(location.getRequests());
      // sort the request in ascending order
      Collections.sort(requestList, new ScoreSorter());

      // delete the requests that might be taken
      for (int i = 0; i < Math.min(count, requestList.size()); i++) {
        location.removeRequest(requestList.get(requestList.size() - 1 - i));
      }
    }

    // return the nearby locations with filtered requests
    return nearbyLocations;
  }
Beispiel #7
0
  public Request nextRequest(Volunteer volunteer) {

    // SPD (SCORE PER DISTANCE) = total SCORE / total DISTANCE

    // max is gonna be used multiple times to compare values
    double max = 0;

    // make a list of requests (from this list, the best request will be
    // chosen) and a list of corresponding SPD.
    Request[] requestList = new Request[5];
    double[] pointList = new double[5];

    // *****STRATEGY 1**************************************************
    // Search for the request with the highest SPD at the current location
    // requestList[0] = this request;
    // pointList[0] = this request's SPD;

    for (Request request : volunteer.getCurrentLocation().getRequests()) {
      if (request != null && request.getValue() > max) {
        max = request.getValue();
        requestList[0] = request;
        double totalDistance = distance(volunteer.getCurrentLocation(), request.getDestination());
        if (totalDistance == 0.0) {
          pointList[0] = 0.0;
        } else pointList[0] = request.getValue() / totalDistance;
      }
    }
    // ***************************************************************************************
    // *****STRATEGY 2**************************************************
    // Search for the request with the highest SPD at the nearby locations
    // use filterLocations() to get nearby locations HashSet
    // requestList[1] = this request;
    // pointList[1] = this request's SPD;

    // ***************************************************************************************
    max = 0;
    for (Location location : filterLocations(volunteer)) {
      for (Request request : location.getRequests()) {
        if (request != null && request.getValue() > max) {
          max = request.getValue();
          requestList[1] = request;
          // distance from Volunteer to the requester's location
          double distance1 = distance(volunteer.getCurrentLocation(), request.getStart());
          // distance for the walking of the request
          double distance2 = distance(request.getStart(), request.getDestination());
          // total distance
          double distance = distance1 + distance2;

          if (distance == 0.0) {
            pointList[1] = 0.0;
          } else pointList[1] = request.getValue() / distance;
        }
      }
    }

    // *****************************
    // Pick the request with the highest SPD
    max = 0.0;
    int index = 0;
    for (int i = 0; i < pointList.length; i++) {
      if (pointList != null && pointList[i] > max) {
        max = pointList[i];
        index = i;
      }
    }

    return requestList[index];
    // ***************************************************************************************

  }