Skip to content

Commit

Permalink
refactor: O(1) getDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
teal0range committed Apr 9, 2021
1 parent 95c88ef commit 7ae2ab5
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 79 deletions.
1 change: 0 additions & 1 deletion src/main/java/Algorithm/VNSALS.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public OptimizeJob(Solution solution) {

@Override
public void run() {

}
}
}
20 changes: 19 additions & 1 deletion src/main/java/Common/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ public class Solution {
public List<Node> unassignedCustomer;
public Problem problem;
private int autoIncrement;
private double distance;

public Solution(List<Route> routes, Problem problem, List<Node> unassignedCustomer) {
this.routes = routes;
this.unassignedCustomer = unassignedCustomer;
this.problem = problem;
this.autoIncrement = 0;
this.distance = 0;
for (Route route : routes) {
for (int i = 0; i < route.length(); i++) {
distance += problem.getDistance(route.getNode(i - 1), route.getNode(i));
}
distance += problem.getDistance(route.getNode(route.length() - 1), route.getNode(route.length()));
}
}

public Solution(List<Route> routes, Problem problem) {
Expand All @@ -31,6 +39,7 @@ public Solution(Solution other) {
this.problem = other.problem;
this.unassignedCustomer = new ArrayList<>(other.unassignedCustomer);
this.autoIncrement = other.getAutoIncrement();
this.distance = other.getDistance();
}

public void shuffle() {
Expand All @@ -45,14 +54,23 @@ public int getAutoIncrement() {
return autoIncrement;
}

public double getDistance() {
public double getDistance(){
return distance;
}

public void updateDistance(double delta){
this.distance += delta;
}

public double refreshDistance() {
double distance = 0;
for (Route route : routes) {
for (int i = 0; i < route.length(); i++) {
distance += problem.getDistance(route.getNode(i - 1), route.getNode(i));
}
distance += problem.getDistance(route.getNode(route.length() - 1), route.getNode(route.length()));
}
this.distance = distance;
return distance;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public static void main(String[] args) throws IOException {
bestSol = new Solution(solution);
}
}
logger.info(bestSol.getDistance());
logger.info(bestSol.refreshDistance());
}
}
1 change: 1 addition & 0 deletions src/main/java/Operators/Insertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void singleOperate(Solution solution, OperationContext context) {
bestContext.mainRoute.addNode(bestContext.operatePos[0], bestContext.operateNodes[0]);
}
}
solution.refreshDistance();
solution.unassignedCustomer.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Operators/OperationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public OperationContext copy() {
}

public enum operatorType {
INSERT, SUBSTITUTE, Shift10, Shift20, Swap11, Swap21, Swap22, TwoOpt, TwoOptStar1, TwoOptStar2
INSERT, Shift10, Shift20, Swap11, Swap21, Swap22, TwoOpt, TwoOptStar1, TwoOptStar2
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Operators/Shift10.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
if (i >= mainRoute.length()) break; // shift 结点后,路径可能变短
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Operators/Shift20.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
if (i >= mainRoute.length() - 1) break; // shift 结点后,路径可能变短
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/Operators/Swap11.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand All @@ -56,6 +57,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/Operators/Swap21.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand All @@ -44,6 +45,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
if (i >= mainRoute.length() - 1) break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/Operators/Swap22.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand All @@ -44,6 +45,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Operators/TwoOpt.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Operators/TwoOptStar1.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void doOperateAll(Solution solution) {
double costChg = softConstraintManager.fulfilled(context);
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
solution.updateDistance(costChg);
if (i >= mainRoute.length() - 1 || j >= sideRoute.length() - 1) break;
refreshOperateVal(context);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Operators/TwoOptStar2.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void doOperateAll(Solution solution) {
singleOperate(solution, context);
if (i >= mainRoute.length() - 1 || j >= sideRoute.length() - 1) break;
refreshOperateVal(context);
solution.updateDistance(costChg);
}
}
weight[1] = 0;
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/Utils/RandomController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package Utils;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.*;

public class RandomController {
private static Random random = new Random(System.currentTimeMillis());
Expand All @@ -18,4 +16,13 @@ public static void shuffle(List<?> list) {
public static int nextInt(int bound) {
return random.nextInt(bound);
}

public static ArrayList<Integer> randIndex(int bound){
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < bound; i++) {
res.set(i, i);
}
Collections.shuffle(res);
return res;
}
}
14 changes: 5 additions & 9 deletions src/main/java/Utils/TimeController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package Utils;

public class TimeController{
private long startTime;
private final long timeLimit;
private static long startTime;

public TimeController(long timeLimit) {
this.timeLimit = timeLimit;
public static void setTimeLimit(long timeLimit) {
TimeController.timeLimit = timeLimit;
}

public TimeController(int minutes) {
this(minutes * 60000L);
startTime = System.currentTimeMillis();
}
private static long timeLimit;

public void reset(){
startTime = System.currentTimeMillis();
}

public boolean timeIsUp(){
public static boolean timeIsUp(){
return System.currentTimeMillis() - startTime >= timeLimit;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import Constraints.SoftConstraintManager;
import IO.CourdeauInstanceReader;
import Operators.OperationContext;
import Utils.RandomController;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class SoftCostConstraintImplTest {

Expand All @@ -42,6 +39,7 @@ public void fulfilled() throws IOException {
context.setMainRoute(mainRoute);
for (Route sideRoute : solution.getRoutes()) {
sideRoute.shuffle();
solution.refreshDistance();
context.setSideRoute(sideRoute);
for (int i = 0; i < mainRoute.length(); ++i) {
context.setOperatePos(0, i);
Expand All @@ -53,7 +51,8 @@ public void fulfilled() throws IOException {
double costBefore = solution.getDistance();
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
Assert.assertEquals(costBefore + costChg, solution.getDistance(), 0.001);
solution.updateDistance(costChg);
Assert.assertEquals(costBefore + costChg, solution.refreshDistance(), 0.001);
if (i >= mainRoute.length()) break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import IO.CourdeauInstanceReader;
import Operators.OperationContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class SoftCostConstraintImplTest {

public void singleOperate(Solution solution, OperationContext context) {
Expand All @@ -42,6 +39,7 @@ public void fulfilled() throws IOException {
for (Route sideRoute:solution.getRoutes()) {
context.setSideRoute(sideRoute);
sideRoute.shuffle();
solution.refreshDistance();
for (int i = 0; i < mainRoute.length() - 1; i++) {
context.setOperatePos(0, i);
for (int j = -1; j < sideRoute.length(); j++) { //插入在指定节点之后
Expand All @@ -52,7 +50,8 @@ public void fulfilled() throws IOException {
double costBefore = solution.getDistance();
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
singleOperate(solution, context);
Assert.assertEquals(costBefore + costChg, solution.getDistance(), 0.001);
solution.updateDistance(costChg);
Assert.assertEquals(costBefore + costChg, solution.refreshDistance(), 0.001);
if (i >= mainRoute.length() - 1) break; // shift 结点后,路径可能变短
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void fulfilled() throws IOException {
context.setMainRoute(mainRoute);
for (Route sideRoute : solution.getRoutes()) {
sideRoute.shuffle();
solution.refreshDistance();
context.setSideRoute(sideRoute);
if (mainRoute==sideRoute){
for (int i = 0; i < mainRoute.length(); i++) {
Expand All @@ -42,7 +43,8 @@ public void fulfilled() throws IOException {
double costBefore = solution.getDistance();
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0){
context.mainRoute.swap11(context.sideRoute, context.operatePos[0], context.operatePos[1]);
Assert.assertEquals(costBefore + costChg, solution.getDistance(), 0.001);
solution.updateDistance(costChg);
Assert.assertEquals(costBefore + costChg, solution.refreshDistance(), 0.001);
}
}
}
Expand All @@ -56,7 +58,8 @@ public void fulfilled() throws IOException {
double costBefore = solution.getDistance();
if (status == HardConstraint.ConsStatus.FULFILLED && costChg < 0) {
context.mainRoute.swap11(context.sideRoute, context.operatePos[0], context.operatePos[1]);
Assert.assertEquals(costBefore + costChg, solution.getDistance(), 0.001);
solution.updateDistance(costChg);
Assert.assertEquals(costBefore + costChg, solution.refreshDistance(), 0.001);
}
}
}
Expand Down
Loading

0 comments on commit 7ae2ab5

Please sign in to comment.