Skip to content

Commit

Permalink
only call connect_voxels if splitpoint is not specified to split func…
Browse files Browse the repository at this point in the history
…tion in PottsLocation
  • Loading branch information
Jannetty committed Sep 19, 2024
1 parent 29c20c0 commit cd7e4ac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 61 deletions.
83 changes: 31 additions & 52 deletions src/arcade/potts/env/location/PottsLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ public void update(int id, int[][][] ids, int[][][] regions) {
* Splits the location voxels into two lists.
* <p>
* The split occurs along the direction with the shortest diameter, or a
* specified direction. The lists of voxels are guaranteed to be connected,
* and generally will be balanced in size. One of the splits is assigned
* to the current location and the other is returned.
* specified direction. One of the splits is assigned to the current
* location and the other is returned.
*
* @param random the seeded random number generator
* @return a location with the split voxels
Expand All @@ -276,8 +275,6 @@ public Location split(MersenneTwisterFast random) {
/**
* Splits the location voxels into two lists at the point specified by
* offsetPercents and along the shortest diameter.
* <p>
* The lists of voxels are guaranteed to be connected and balanced.
*
* @param random the seeded random number generator
* @param offsetPercents the percentage offset in each direction for the split point
Expand All @@ -294,85 +291,67 @@ public Location split(MersenneTwisterFast random, ArrayList<Integer> offsetPerce
* <p>
* The lists of voxels are guaranteed to be connected and balanced if no
* direction is provided. If a direction is provided, the split occurs
* without balancing.
* without connecting or balancing.
*
* @param random the seeded random number generator
* @param offsetPercents the percentage offset in each direction for the split point
* @param direction the direction of the split, or null if using shortest diameter
* @return a location with the split voxels
*/
public Location split(MersenneTwisterFast random, ArrayList<Integer> offsetPercents, Direction direction) {

Check failure on line 301 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L301 <LineLength>

Line is longer than 100 characters (found 111).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:301:0: error: Line is longer than 100 characters (found 111). (LineLength)
Voxel splitpoint = (offsetPercents != null) ? getSplitpoint(offsetPercents) : getSplitpoint();

// Check if the direction is specified, if not use default behavior with balancing
if (direction == null) {
return performSplit(random, splitpoint); // This will balance voxels
} else {
return performSplit(random, splitpoint, direction); // This will skip balancing
}
}
Voxel splitpoint;
boolean shouldBalance;

/**
* Performs the voxel split based on a given split point.
* <p>
* Splits the voxels of the location into two balanced, connected groups on
* either side of split point. Split occurs along the direction with
* the shortest diameter.
*
* @param random the seeded random number generator
* @param splitpoint the voxel that determines where the split occurs
* @return a {@code Location} containing the split voxels that are not assigned to the current location
*/
Location performSplit(MersenneTwisterFast random, Voxel splitpoint) {
return performSplit(random, splitpoint, getDirection(random), true);
}
// Case 1: If offsetPercents are provided, don't balance the voxels
if (offsetPercents != null) {
splitpoint = getSplitpoint(offsetPercents);
shouldBalance = false;
}

Check failure on line 309 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L309 <com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck>

'}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:309:9: error: '}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally). (com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck)
// Case 2: If direction is provided and no offsetPercents, balance the voxels
else if (direction != null) {
splitpoint = getSplitpoint();
shouldBalance = true;
}

Check failure on line 314 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L314 <com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck>

'}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:314:9: error: '}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally). (com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck)
// Default Case: Neither offsetPercents nor direction are provided, balance the voxels
else {
splitpoint = getSplitpoint();
shouldBalance = true; // This can be changed if needed
}

/**
* Performs the voxel split based on a given split point and direction.
* <p>
* Splits the voxels of the location into two connected groups on
* either side of split point. Balancing is skipped.
*
* @param random the seeded random number generator
* @param splitpoint the voxel that determines where the split occurs
* @param direction the direction of the split
* @return a {@code Location} containing the split voxels that are not assigned to the current location
*/
Location performSplit(MersenneTwisterFast random, Voxel splitpoint, Direction direction) {
// Calls performSplit without balancing
return performSplit(random, splitpoint, direction, false);
return performSplit(random, splitpoint, direction, shouldBalance);
}

/**
* Performs the voxel split based on a given split point, direction,
* and whether to balance the voxels.
* <p>
* Splits the voxels of the location into two connected groups on
* either side of split point. Balancing is optional based on the
* either side of split point. Balancing and connecting is optional based on the
* shouldBalance parameter.
*
* @param random the seeded random number generator
* @param random the seeded random number generator
* @param splitpoint the voxel that determines where the split occurs
* @param direction the direction of the split
* @param direction the direction of the split (can be null)
* @param shouldBalance indicates whether voxels should be balanced
* @return a {@code Location} containing the split voxels that are not assigned to the current location

Check failure on line 336 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L336 <LineLength>

Line is longer than 100 characters (found 107).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:336:0: error: Line is longer than 100 characters (found 107). (LineLength)
*/
Location performSplit(MersenneTwisterFast random, Voxel splitpoint, Direction direction, boolean shouldBalance) {

Check failure on line 338 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L338 <LineLength>

Line is longer than 100 characters (found 117).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:338:0: error: Line is longer than 100 characters (found 117). (LineLength)
// Initialize lists of split voxels
ArrayList<Voxel> voxelsA = new ArrayList<>();
ArrayList<Voxel> voxelsB = new ArrayList<>();

// Perform the split along the specified direction
if (direction == null) {
direction = getDirection(random);
}
splitVoxels(direction, voxels, voxelsA, voxelsB, splitpoint, random);

// Ensure that voxel split is connected
connectVoxels(voxelsA, voxelsB, this, random);


// Only balance if 'shouldBalance' is true
if (shouldBalance) {
connectVoxels(voxelsA, voxelsB, this, random);
balanceVoxels(voxelsA, voxelsB, this, random);
}

// Select one split to keep for this location and return the other
return (random.nextDouble() < 0.5) ? separateVoxels(voxelsA, voxelsB, random) : separateVoxels(voxelsB, voxelsA, random);

Check failure on line 356 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L356 <LineLength>

Line is longer than 100 characters (found 129).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:356:0: error: Line is longer than 100 characters (found 129). (LineLength)

Check failure on line 356 in src/arcade/potts/env/location/PottsLocation.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] src/arcade/potts/env/location/PottsLocation.java#L356 <LineLengthTest>

Line is longer than 120 characters (found 129).
Raw output
/github/workspace/./src/arcade/potts/env/location/PottsLocation.java:356:0: error: Line is longer than 120 characters (found 129). (LineLengthTest)
}
Expand Down
29 changes: 20 additions & 9 deletions test/arcade/potts/env/location/PottsLocationTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package arcade.potts.env.location;

import java.lang.reflect.Array;

Check failure on line 3 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L3 <com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck>

Unused import - java.lang.reflect.Array.
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:3:8: error: Unused import - java.lang.reflect.Array. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import arcade.potts.util.PottsEnums.Direction;
import arcade.potts.util.PottsEnums.Region;
import ec.util.MersenneTwisterFast;

Check failure on line 12 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L12 <com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck>

Wrong order for 'ec.util.MersenneTwisterFast' import.
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:12:1: error: Wrong order for 'ec.util.MersenneTwisterFast' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static arcade.core.ARCADETestUtilities.*;
Expand Down Expand Up @@ -1307,28 +1312,27 @@ public void separateVoxels_validLists_updatesCenter() {
@Test
public void split_noOffsets_callsPerformSplit() {
PottsLocation spy = spy(new PottsLocationMock(voxelListAB));
doCallRealMethod().when(spy).performSplit(any(MersenneTwisterFast.class), any(Voxel.class));
doCallRealMethod().when(spy).performSplit(any(MersenneTwisterFast.class), any(Voxel.class), any(Direction.class), anyBoolean());

Check failure on line 1315 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L1315 <LineLengthTest>

Line is longer than 120 characters (found 136).
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:1315:0: error: Line is longer than 120 characters (found 136). (LineLengthTest)
spy.split(randomDoubleZero);
verify(spy).getSplitpoint();
verify(spy).performSplit(eq(randomDoubleZero), any(Voxel.class));
verify(spy).performSplit(eq(randomDoubleZero), Mockito.any(Voxel.class), eq(null), eq(true));
}

@Test
public void split_withOffsets_callsPerformSplit() {
ArrayList<Integer> offsets = new ArrayList<>(Arrays.asList(50, 50, 50));
PottsLocation spy = spy(new PottsLocationMock(voxelListAB));
doCallRealMethod().when(spy).performSplit(any(MersenneTwisterFast.class), any(Voxel.class));
doCallRealMethod().when(spy).performSplit(any(MersenneTwisterFast.class), any(Voxel.class), any(Direction.class), anyBoolean());

Check failure on line 1325 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L1325 <LineLengthTest>

Line is longer than 120 characters (found 136).
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:1325:0: error: Line is longer than 120 characters (found 136). (LineLengthTest)
spy.split(randomDoubleZero, offsets);
verify(spy).getSplitpoint(offsets);
verify(spy).performSplit(eq(randomDoubleZero), any(Voxel.class));
}
verify(spy).performSplit(eq(randomDoubleZero), Mockito.any(Voxel.class), eq(null), eq(false)); }

@Test
public void performSplit_noOffsets_splitsVoxelsCorrectly() {
PottsLocation location = new PottsLocationMock(voxelListAB);
MersenneTwisterFast random = new MersenneTwisterFast(12345);
Voxel splitpoint = location.getSplitpoint();
PottsLocation splitLocation = (PottsLocation) location.performSplit(random, splitpoint);
PottsLocation splitLocation = (PottsLocation) location.performSplit(random, splitpoint, null, true);
assertNotNull(splitLocation);
assertTrue(location.voxels.size() > 0);
assertTrue(splitLocation.voxels.size() > 0);
Expand All @@ -1341,17 +1345,24 @@ public void performSplit_noOffsets_splitsVoxelsCorrectly() {
public void performSplit_withOffsets_splitsVoxelsCorrectly() {
PottsLocation location = new PottsLocationMock(voxelListAB);
MersenneTwisterFast random = new MersenneTwisterFast(12345);
// Add two voxels to the location bringing total to 9
location.add(3, 3, 3);
location.add(4, 4, 4);
ArrayList<Integer> offsets = new ArrayList<>(Arrays.asList(33, 33, 33)); // 33% offsets
Voxel splitpoint = location.getSplitpoint(offsets);
PottsLocation splitLocation = (PottsLocation) location.performSplit(random, splitpoint);
PottsLocation splitLocation = (PottsLocation) location.performSplit(random, splitpoint, null, false);
assertNotNull(splitLocation);
assertTrue(location.voxels.size() > 0);
assertTrue(splitLocation.voxels.size() > 0);
assertEquals(voxelListAB.size(), location.voxels.size() + splitLocation.voxels.size());
assertEquals(voxelListAB.size() + 2, location.voxels.size() + splitLocation.voxels.size());
// Check that one location is approximately 1/3 the size of the other
int locationSize = location.voxels.size();
int splitLocationSize = splitLocation.voxels.size();
int totalSize = locationSize + splitLocationSize;
assertTrue(Math.abs(locationSize - totalSize / 3) <= 1 || Math.abs(splitLocationSize - totalSize / 3) <= 1);
double expectedOneThird = totalSize / 3.0;
double expectedTwoThirds = 2 * totalSize / 3.0;
boolean locationIsOneThird = Math.abs(locationSize - expectedOneThird) <= 1 && Math.abs(splitLocationSize - expectedTwoThirds) <= 1;

Check failure on line 1364 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L1364 <LineLengthTest>

Line is longer than 120 characters (found 140).
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:1364:0: error: Line is longer than 120 characters (found 140). (LineLengthTest)
boolean locationIsTwoThirds = Math.abs(locationSize - expectedTwoThirds) <= 1 && Math.abs(splitLocationSize - expectedOneThird) <= 1;

Check failure on line 1365 in test/arcade/potts/env/location/PottsLocationTest.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] test/arcade/potts/env/location/PottsLocationTest.java#L1365 <LineLengthTest>

Line is longer than 120 characters (found 141).
Raw output
/github/workspace/./test/arcade/potts/env/location/PottsLocationTest.java:1365:0: error: Line is longer than 120 characters (found 141). (LineLengthTest)
assertTrue(locationIsOneThird || locationIsTwoThirds);
}
}

0 comments on commit cd7e4ac

Please sign in to comment.