main.io.github.moonlightsuite.moonlight.space.ManhattanDistanceStructure Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moonlight-engine Show documentation
Show all versions of moonlight-engine Show documentation
MoonLight is a light-weight Java-tool for monitoring temporal, spatial and spatio-temporal properties of distributed complex systems, such as Cyber-Physical Systems and Collective Adaptive Systems.
The newest version!
package io.github.moonlightsuite.moonlight.space;
import io.github.moonlightsuite.moonlight.core.space.DistanceDomain;
import io.github.moonlightsuite.moonlight.core.space.DistanceStructure;
import io.github.moonlightsuite.moonlight.core.space.SpatialModel;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
public class ManhattanDistanceStructure
implements DistanceStructure {
private final Function distanceFunction;
private final DistanceDomain distanceDomain;
private final M lowerBound;
private final M upperBound;
private final RegularGridModel model;
public ManhattanDistanceStructure(
@NotNull Function distanceFunction,
@NotNull DistanceDomain distanceDomain,
@NotNull M lowerBound,
@NotNull M upperBound,
@NotNull RegularGridModel model) {
this.distanceFunction = distanceFunction;
this.distanceDomain = distanceDomain;
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.model = model;
}
@Override
public boolean areWithinBounds(int from, int to) {
return isWithinBounds(getDistance(from, to));
}
@Override
public M getDistance(int from, int to) {
int[] fPair = model.toCoordinates(from);
int[] tPair = model.toCoordinates(to);
return computeManhattanDistance(fPair, tPair);
}
private M computeManhattanDistance(int[] from,
int[] to) {
int distX = Math.abs(from[0] - to[0]);
int distY = Math.abs(from[1] - to[1]);
int dist = distX + distY;
return distanceToMetric(dist);
}
private M distanceToMetric(int distance) {
M weight = distanceFunction.apply(model.getWeight());
return distanceDomain.multiply(weight, distance);
}
@Override
public boolean isWithinBounds(M d) {
return distanceDomain.lessOrEqual(lowerBound, d) &&
distanceDomain.lessOrEqual(d, upperBound);
}
@Override
public SpatialModel getModel() {
return model;
}
@Override
public Function getDistanceFunction() {
return distanceFunction;
}
@Override
public DistanceDomain getDistanceDomain() {
return distanceDomain;
}
}