Skip to content

Instantly share code, notes, and snippets.

@jenetics
Created November 22, 2019 21:30
Show Gist options
  • Save jenetics/0276848c91e4b4509eaab6d9d91459a9 to your computer and use it in GitHub Desktop.
Save jenetics/0276848c91e4b4509eaab6d9d91459a9 to your computer and use it in GitHub Desktop.
Implementation of 3D-Point usable for Multi-objective optimization
import static java.lang.String.format;
import java.util.Comparator;
/**
* Sample implementation of a 3D double point.
*/
public final class Point3 implements Vec<Point3> {
private final double _x;
private final double _y;
private final double _z;
private Point3(final double x, final double y, final double z) {
_x = x;
_y = y;
_z = z;
}
public double x() {
return _x;
}
public double y() {
return _y;
}
public double z() {
return _z;
}
@Override
public Point3 data() {
return this;
}
@Override
public int length() {
return 3;
}
@Override
public ElementComparator<Point3> comparator() {
return Point3::cmp;
}
private static int cmp(final Point3 u, final Point3 v, final int i) {
switch (i) {
case 0: return Double.compare(u._x, v._x);
case 1: return Double.compare(u._y, v._y);
case 2: return Double.compare(u._z, v._z);
default: throw new IllegalArgumentException("Illegal index: " + i);
}
}
@Override
public ElementDistance<Point3> distance() {
return Point3::dst;
}
private static double dst(final Point3 u, final Point3 v, final int i) {
switch (i) {
case 0: return u._x - v._x;
case 1: return u._y - v._y;
case 2: return u._z - v._z;
default: throw new IllegalArgumentException("Illegal index: " + i);
}
}
@Override
public Comparator<Point3> dominance() {
return Point3::dom;
}
private static int dom(final Point3 u, final Point3 v) {
return Pareto.dominance(u, v, 3, Point3::cmp);
}
@Override
public int hashCode() {
int hash = 37;
hash += 31*Double.hashCode(_x) + 17;
hash += 31*Double.hashCode(_y) + 17;
hash += 31*Double.hashCode(_z) + 17;
return hash;
}
@Override
public boolean equals(final Object obj) {
return obj == this ||
obj instanceof Point3 &&
Double.compare(((Point3) obj)._x, _x) == 0 &&
Double.compare(((Point3) obj)._y, _y) == 0 &&
Double.compare(((Point3) obj)._z, _z) == 0;
}
@Override
public String toString() {
return format("[%f, %f, %f]", _x, _y, _z);
}
public static Point3 of(final double x, final double y, final double z) {
return new Point3(x, y, z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment