All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rinde.rinsim.examples.pdptw.gradientfield.GradientModel Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/*
 * Copyright (C) 2011-2014 Rinde van Lon, iMinds DistriNet, KU Leuven
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rinde.rinsim.examples.pdptw.gradientfield;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.annotation.Nullable;

import com.github.rinde.rinsim.core.model.Model;
import com.github.rinde.rinsim.core.model.ModelProvider;
import com.github.rinde.rinsim.core.model.ModelReceiver;
import com.github.rinde.rinsim.core.model.pdp.PDPModel;
import com.github.rinde.rinsim.core.model.pdp.Parcel;
import com.github.rinde.rinsim.core.model.road.RoadModel;
import com.github.rinde.rinsim.geom.Point;
import com.github.rinde.rinsim.util.StochasticSupplier;
import com.github.rinde.rinsim.util.StochasticSuppliers;
import com.github.rinde.rinsim.util.StochasticSuppliers.AbstractStochasticSupplier;
import com.google.common.collect.ImmutableList;

/**
 * 
 * @author David Merckx
 * @author Rinde van Lon 
 */
public class GradientModel implements Model, ModelReceiver {

  private final List emitters;
  private double minX;
  private double maxX;
  private double minY;
  private double maxY;
  private PDPModel pdpModel;
  ImmutableList bounds;

  public GradientModel() {

    emitters = new CopyOnWriteArrayList();
  }

  public List getEmitters() {
    return emitters;
  }

  public List getTruckEmitters() {
    final List trucks = new ArrayList();

    for (final FieldEmitter emitter : emitters) {
      if (emitter instanceof Truck) {
        trucks.add((Truck) emitter);
      }
    }

    return trucks;
  }

  /**
   * Possibilities (-1,1) (0,1) (1,1) (-1,0) (1,0 (-1,-1) (0,-1) (1,-1)
   */
  private final int[] x = { -1, 0, 1, 1, 1, 0, -1, -1 };
  private final int[] y = { 1, 1, 1, 0, -1, -1, -1, 0 };

  @Nullable
  public Point getTargetFor(Truck element) {
    float maxField = Float.NEGATIVE_INFINITY;
    Point maxFieldPoint = null;

    for (int i = 0; i < x.length; i++) {
      final Point p = new Point(element.getPosition().x + x[i],
          element.getPosition().y + y[i]);

      if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) {
        continue;
      }

      final float field = getField(p, element);
      if (field >= maxField) {
        maxField = field;
        maxFieldPoint = p;
      }
    }

    return maxFieldPoint;
  }

  public float getField(Point in, Truck truck) {
    float field = 0.0f;
    for (final FieldEmitter emitter : emitters) {
      field += emitter.getStrength()
          / Point.distance(emitter.getPosition(), in);
    }

    for (final Parcel p : pdpModel.getContents(truck)) {
      field += 2 / Point.distance(p.getDestination(), in);
    }
    return field;
  }

  @Override
  public boolean register(FieldEmitter element) {
    emitters.add(element);
    element.setModel(this);
    return true;
  }

  @Override
  public boolean unregister(FieldEmitter element) {
    emitters.remove(element);
    return false;
  }

  @Override
  public Class getSupportedType() {
    return FieldEmitter.class;
  }

  public Map getFields(Truck truck) {
    final Map fields = new HashMap();

    for (int i = 0; i < x.length; i++) {
      final Point p = new Point(truck.getPosition().x + x[i],
          truck.getPosition().y + y[i]);

      if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) {
        continue;
      }

      fields.put(new Point(x[i], y[i]), getField(p, truck));
    }

    float avg = 0;
    for (final Point p : fields.keySet()) {
      avg += fields.get(p);
    }
    avg /= fields.size();
    for (final Point p : fields.keySet()) {
      fields.put(p, fields.get(p) - avg);
    }
    return fields;
  }

  @Override
  public void registerModelProvider(ModelProvider mp) {
    pdpModel = mp.getModel(PDPModel.class);
    final ImmutableList bounds = mp.getModel(RoadModel.class)
        .getBounds();

    minX = bounds.get(0).x;
    maxX = bounds.get(1).x;
    minY = bounds.get(0).y;
    maxY = bounds.get(1).y;
  }

  public static StochasticSupplier supplier() {
    return new StochasticSuppliers.AbstractStochasticSupplier() {
      @Override
      public GradientModel get(long seed) {
        return new GradientModel();
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy