File size: 12,206 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public abstract class Helpers {
static Comparator<Individual> edgeValueComparator = new Comparator<Individual>() {
public int compare(Individual i, Individual o) {
return (int) (i.getEdgeValue() - o.getEdgeValue());
}
};
static Comparator<Individual> overallDeviationComparator = new Comparator<Individual>() {
public int compare(Individual i, Individual o) {
return (int) (i.getOverallDeviation() - o.getOverallDeviation());
}
};
static Comparator<Individual> crowdingDistanceComparator = new Comparator<Individual>() {
public int compare(Individual i, Individual o) {
return (int) (o.getCrowdingDistance() - i.getCrowdingDistance());
}
};
static double PlanarEuclideanDistance(double x0, double y0, double x1, double y1) {
return Math.sqrt((Math.pow(x0 - x1, 2)) + Math.pow(y0 - y1, 2));
}
public static ArrayList<Individual> crowdingDistance(ArrayList<Individual> individuals, int num_individuals_to_keep) {
//Sort individuals by edge value and get best and worst
ArrayList<Individual> edgeValueRanks = new ArrayList<Individual>(individuals);
Collections.sort(edgeValueRanks, edgeValueComparator);
edgeValueRanks.get(0).setCrowdingDistance(Integer.MAX_VALUE);
edgeValueRanks.get(edgeValueRanks.size() - 1).setCrowdingDistance(Integer.MAX_VALUE);
//Sort individuals by overall deviation and get best and worst
ArrayList<Individual> overallDeviationRanks = new ArrayList<Individual>(individuals);
Collections.sort(overallDeviationRanks, overallDeviationComparator);
overallDeviationRanks.get(0).setCrowdingDistance(Integer.MAX_VALUE);
overallDeviationRanks.get(overallDeviationRanks.size() - 1).setCrowdingDistance(Integer.MAX_VALUE);
//Establish FMin and FMax values
double edgeValueFMax = edgeValueRanks.get(edgeValueRanks.size() - 1).getEdgeValue();
double edgeValueFMin = edgeValueRanks.get(0).getEdgeValue();
double overallDeviationFMax = overallDeviationRanks.get(overallDeviationRanks.size() - 1).getOverallDeviation();
double overallDeviationFMin = overallDeviationRanks.get(0).getOverallDeviation();
//Add edge value crowding distance
for (int i = 1; i < edgeValueRanks.size() - 1; i++) {
double distance = (edgeValueRanks.get(i - 1).getEdgeValue() - edgeValueRanks.get(i + 1).getEdgeValue()) / (edgeValueFMax - edgeValueFMin);
edgeValueRanks.get(i).setCrowdingDistance(edgeValueRanks.get(i).getCrowdingDistance() + distance);
}
//Add overall deviation crowding distance
for (int i = 1; i < overallDeviationRanks.size() - 1; i++) {
double distance = (overallDeviationRanks.get(i - 1).getOverallDeviation() - overallDeviationRanks.get(i + 1).getOverallDeviation()) / (overallDeviationFMax - overallDeviationFMin);
edgeValueRanks.get(i).setCrowdingDistance(edgeValueRanks.get(i).getCrowdingDistance() + distance);
}
//Sort by crowding distance
Collections.sort(individuals, crowdingDistanceComparator);
//return new ArrayList<Individual>(individuals.subList(0, num_individuals_to_keep));
return individuals;
}
static double ColorEuclideanDistance(Color c0, Color c1) {
return Math.sqrt((Math.pow(c0.getRed() - c1.getRed(), 2)) + (Math.pow(c0.getGreen() - c1.getGreen(), 2)) + (Math.pow(c0.getBlue() - c1.getBlue(), 2)));
}
/*static void setAvgColor(ArrayList<Centroid> centroids){
for(Centroid c : centroids){
double r = 0;
double g = 0;
double b = 0;
for(Node n : c.getcurrentlyAssignedNodes()){
r += n.getColor().getRed();
g += n.getColor().getGreen();
b += n.getColor().getBlue();
}
r /= c.getcurrentlyAssignedNodes().size();
g /= c.getcurrentlyAssignedNodes().size();
b /= c.getcurrentlyAssignedNodes().size();
Color color = new Color((int)r, (int) g, (int) b);
c.setAvgColor(color);
}
}*/
public static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[][] result = new int[height][width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
argb += ((int) pixels[pixel + 1] & 0xff); // blue
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += -16777216; // 255 alpha
argb += ((int) pixels[pixel] & 0xff); // blue
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
}
return result;
}
public static int[] getRGBFromInt(int color) {
int blue = color & 0xff;
int green = (color & 0xff00) >> 8;
int red = (color & 0xff0000) >> 16;
int[] t = new int[3];
t[0] = red;
t[1] = green;
t[2] = blue;
return t;
}
public static double rgbDistance(int argb1, int argb2) {
int r1 = (argb1 >> 16) & 255;
int g1 = (argb1 >> 8) & 255;
int b1 = (argb1) & 255;
int r2 = (argb2 >> 16) & 255;
int g2 = (argb2 >> 8) & 255;
int b2 = (argb2) & 255;
return ColorEuclideanDistance(new Color(r1, g1, b1), new Color(r2, g2, b2));
//return Math.sqrt(r1 * r2 + g1 * g2 + b1 * b2);
}
public static ArrayList<ArrayList<Node>> initNodes(int[][] img) {
ArrayList<ArrayList<Node>> nodes = new ArrayList<>();
final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
executorService.execute(() -> {
for (int i = 0; i < img.length; i++) {
nodes.add(new ArrayList<>());
for (int j = 0; j < img[0].length; j++) {
final int c = img[i][j];
nodes.get(i).add(new Node(i, j, c));
}
}
});
executorService.shutdown();
while(!executorService.isTerminated()){}
return nodes;
}
static public ArrayList<Centroid> initCentroids(int[][] img, int num_centroids) {
Random r = new Random();
HashSet<String> selected = new HashSet<>();
ArrayList<Centroid> centroids = new ArrayList<>();
for (int n = 0; n < num_centroids; n++) {
int x = r.nextInt(img.length);
int y = r.nextInt(img[0].length);
String s = x + "" + y;
int counter = 0;
while (selected.contains(s) && counter < 1000) {
x = r.nextInt(img.length);
y = r.nextInt(img[0].length);
s = x + "" + y;
counter += 1;
}
if (counter >= 999) {
break;
}
selected.add(s);
int c = img[x][y];
centroids.add(new Centroid(x, y, c));
}
return centroids;
}
/*public static ArrayList<ArrayList<Node>> resetNodes(ArrayList<ArrayList<Node>> nodes){
for (int i = 0; i < nodes.size(); i++) {
for (int j = 0; j < nodes.get(i).size(); j++) {
nodes.get(i).get(j).children = new ArrayList<>();
nodes.get(i).get(j).setParent(null);
nodes.get(i).get(j).setCost(Integer.MAX_VALUE);
nodes.get(i).get(j).setRoot(false);
nodes.get(i).get(j).neighbours = new ArrayList<>();
}
}
return nodes;
}*/
public static ArrayList<Node> initRootNodes(ArrayList<ArrayList<Node>> nodes, int numSegments) {
final ArrayList<Node> rootNodes = new ArrayList<>();
final Random r = new Random();
for (int n = 0; n < numSegments; n++) {
final int x = r.nextInt(nodes.size());
final int y = r.nextInt(nodes.get(0).size());
nodes.get(x).get(y).setRoot(true);
rootNodes.add(nodes.get(x).get(y));
}
return rootNodes;
}
static ArrayList<Edge> setNodeEdges(Node node, ArrayList<ArrayList<Node>> nodes) {
final ArrayList<Edge> edges = new ArrayList<>();
try {
final Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY() - 1));
final double g = rgbDistance(node.getColor(), neighbour.getColor());
final Edge edge = new Edge(node.getX(),neighbour.getX() , node.getY(), neighbour.getY(), g);
edges.add(edge);
} catch (Exception ignored) {
}
try {
final Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY() + 1));
final double g = rgbDistance(node.getColor(), neighbour.getColor());
final Edge edge = new Edge(node.getX(),neighbour.getX() , node.getY(), neighbour.getY(), g);
edges.add(edge);
} catch (Exception ignored) {
}
try {
final Node neighbour = nodes.get((int) node.getX() - 1).get((int) (node.getY()));
final double g = rgbDistance(node.getColor(), neighbour.getColor());
final Edge edge = new Edge(node.getX(),neighbour.getX() , node.getY(), neighbour.getY(), g);
edges.add(edge);
} catch (Exception ignored) {
}
try {
final Node neighbour = nodes.get((int) node.getX() + 1).get((int) (node.getY()));
final double g = rgbDistance(node.getColor(), neighbour.getColor());
final Edge edge = new Edge(node.getX(),neighbour.getX() , node.getY(), neighbour.getY(), g);
edges.add(edge);
} catch (Exception ignored) {
}
return edges;
}
public static ArrayList<Node> getNodeNeighbours(Node node, ArrayList<ArrayList<Node>> nodes){
ArrayList<Node> neighbours = new ArrayList<>();
try{
Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY()-1));
neighbours.add(neighbour);
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()-1).get((int) (node.getY()));
neighbours.add(neighbour);
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY()+1));
neighbours.add(neighbour);
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()+1).get((int) (node.getY()));
neighbours.add(neighbour);
}
catch(Exception ignored){
}
return neighbours;
}
}
|