Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed

### Fixed
* Fixed "constructor throws" in Bin class detected by SpotBugs
* Fixed "constructor throws" in InfoDialog class detected by SpotBugs
* Fixed "constructor throws" in Bin class detected by SpotBugs.
* Fixed "constructor throws" in InfoDialog class detected by SpotBugs.
* Refactored MenuBar, UI, and other classes to eliminate class cycles based on RefactorFirst report.
* Refactored SolutionValidator.checkInstance based on RefactorFirst reporting as a brain method.

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cicirello/ibp/InteractiveBinPacking.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ public static void main(String[] args) {
theFrame.setResizable(false);
theFrame.setLocationRelativeTo(null);
theFrame.setVisible(true);
About about = new About(theFrame);
new About(theFrame);
}
}
107 changes: 62 additions & 45 deletions src/main/java/org/cicirello/ibp/SolutionValidator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Interactive Bin Packing.
* Copyright (C) 2021-2023 Vincent A. Cicirello
* Copyright (C) 2021-2026 Vincent A. Cicirello
*
* This file is part of Interactive Bin Packing.
*
Expand Down Expand Up @@ -188,67 +188,84 @@ boolean checkItemOrder(int[] sizes, String[] items, int modeNum) {
return true;
}

boolean checkInstance(int[] sizes, String[] items, String instance) {
boolean goodInstance = true;
HashMap<String, Integer> foundItems = new HashMap<String, Integer>();
for (int i = 0; i < sizes.length; i++) {
if (foundItems.containsKey(items[i])) {
alertList.add("Duplicate items in solution.");
goodInstance = false;
private boolean checkDefaultInstance(HashMap<String, Integer> foundItems, boolean goodInstance) {
int[] weights = {36, 33, 39, 43, 7, 19, 37, 8, 29, 28, 37, 23, 29, 10, 22, 11, 33, 9, 17, 30};
char c = 'A';
for (int i = 0; i < weights.length; i++, c = (char) (c + 1)) {
if (foundItems.containsKey("" + c)) {
int w = foundItems.get("" + c);
if (w != weights[i]) {
alertList.add("Wrong item size found in solution of default instance.");
goodInstance = false;
}
} else {
foundItems.put(items[i], sizes[i]);
alertList.add("Unknown items found in solution of default instance.");
goodInstance = false;
}
}
if (instance.equals("Default")) {
int[] weights = {36, 33, 39, 43, 7, 19, 37, 8, 29, 28, 37, 23, 29, 10, 22, 11, 33, 9, 17, 30};
char c = 'A';
for (int i = 0; i < weights.length; i++, c = (char) (c + 1)) {
if (foundItems.containsKey("" + c)) {
int w = foundItems.get("" + c);
if (w != weights[i]) {
alertList.add("Wrong item size found in solution of default instance.");
goodInstance = false;
}
} else {
alertList.add("Unknown items found in solution of default instance.");
return goodInstance;
}

private boolean checkRandomInstance(
HashMap<String, Integer> foundItems, boolean goodInstance, int numItems) {
char c = 'A';
for (int i = 0; i < numItems; i++, c = (char) (c + 1)) {
if (foundItems.containsKey("" + c)) {
int w = foundItems.get("" + c);
if (w < 20 || w > 50) {
alertList.add("Wrong item size found in solution of random instance.");
goodInstance = false;
}
} else {
alertList.add("Unknown items found in solution of random instance.");
goodInstance = false;
}
} else if (instance.equals("Random")) {
}
return goodInstance;
}

private boolean checkSeededInstance(
HashMap<String, Integer> foundItems, boolean goodInstance, String instance) {
try {
long seed = Long.parseLong(instance.substring(1));
int[] weights = ApplicationState.createRandomItemSizes(20, 50, 20, new Random(seed));
char c = 'A';
for (int i = 0; i < items.length; i++, c = (char) (c + 1)) {
for (int i = 0; i < weights.length; i++, c = (char) (c + 1)) {
if (foundItems.containsKey("" + c)) {
int w = foundItems.get("" + c);
if (w < 20 || w > 50) {
alertList.add("Wrong item size found in solution of random instance.");
if (w != weights[i]) {
alertList.add("Wrong item size found in solution of instance: " + instance);
goodInstance = false;
}
} else {
alertList.add("Unknown items found in solution of random instance.");
alertList.add("Unknown items found in solution of instance: " + instance);
goodInstance = false;
}
}
} else if (instance.startsWith("#")) {
try {
long seed = Long.parseLong(instance.substring(1));
int[] weights = ApplicationState.createRandomItemSizes(20, 50, 20, new Random(seed));
char c = 'A';
for (int i = 0; i < weights.length; i++, c = (char) (c + 1)) {
if (foundItems.containsKey("" + c)) {
int w = foundItems.get("" + c);
if (w != weights[i]) {
alertList.add("Wrong item size found in solution of instance: " + instance);
goodInstance = false;
}
} else {
alertList.add("Unknown items found in solution of instance: " + instance);
goodInstance = false;
}
}
} catch (NumberFormatException ex) {
alertList.add("Malformed instance number in solution.");
} catch (NumberFormatException ex) {
alertList.add("Malformed instance number in solution.");
goodInstance = false;
}
return goodInstance;
}

boolean checkInstance(int[] sizes, String[] items, String instance) {
boolean goodInstance = true;
HashMap<String, Integer> foundItems = new HashMap<String, Integer>();
for (int i = 0; i < sizes.length; i++) {
if (foundItems.containsKey(items[i])) {
alertList.add("Duplicate items in solution.");
goodInstance = false;
} else {
foundItems.put(items[i], sizes[i]);
}
}
if (instance.equals("Default")) {
goodInstance = checkDefaultInstance(foundItems, goodInstance);
} else if (instance.equals("Random")) {
goodInstance = checkRandomInstance(foundItems, goodInstance, items.length);
} else if (instance.startsWith("#")) {
goodInstance = checkSeededInstance(foundItems, goodInstance, instance);
} else {
alertList.add("Unknown instance type found in solution.");
goodInstance = false;
Expand Down
Loading