Buggy code:
public class WorldBuilder implements ContextBuilder < Object > { public int monthsDeclining = 0 ; public static final String PARAMETER_NUM_YEARS = "numYears" ; public static final String PARAMETER_BANKER_COUNT = "bankerCount" ; public static final String PARAMETER_CONSUMER_COUNT = "consumerCount" ; public static final String PARAMETER_REWIRING_PROBABILITY = "rewiringProbability" ; public static final String PARAMETER_MEAN_DEGREE = "meanDegree" ; public static final String PARAMETER_COST_OF_LIVING = "costOfLiving" ; public static final String PARAMETER_MAX_INCOME = "maxIncome" ; public static final String PARAMETER_MEAN_ASSETS = "meanAssets" ; public static final String PARAMETER_MEAN_INCOME = "meanForIncomeDist" ; public static final String PARAMETER_MEAN_SPENDING = "meanSpending" ; public static final String PARAMETER_CONSUMER_RISK = "meanConsumerRisk" ; public static final String PARAMETER_CONSUMER_DESIRE = "meanConsumerDesire" ; public static final String PARAMETER_BANKER_RISK = "meanBankerRisk" ; private static int uniqueId = 0 ; public static final int neighborhoodCount = 12 ; public Context < Object > build ( Context < Object > context ) { context . setId ( CONTEXT_ID ) ; final Parameters parameters = RunEnvironment . getInstance ( ) . getParameters ( ) ; final int bankerCount = ( ( Integer ) ( parameters . getValue ( WorldBuilder . PARAMETER_BANKER_COUNT ) ) ) . intValue ( ) ; final int consumerCount = ( ( Integer ) ( parameters . getValue ( WorldBuilder . PARAMETER_CONSUMER_COUNT ) ) ) . intValue ( ) ; final int numYears = ( ( Integer ) ( parameters . getValue ( WorldBuilder . PARAMETER_NUM_YEARS ) ) ) . intValue ( ) ; final double costOfLiving = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_COST_OF_LIVING ) ) ) . doubleValue ( ) ; final double maxIncome = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_MAX_INCOME ) ) ) . doubleValue ( ) ; final double meanAssets = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_MEAN_ASSETS ) ) ) . doubleValue ( ) ; final double meanIncome = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_MEAN_INCOME ) ) ) . doubleValue ( ) ; final double meanSpending = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_MEAN_SPENDING ) ) ) . doubleValue ( ) ; final double meanConsumerRisk = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_CONSUMER_RISK ) ) ) . doubleValue ( ) ; final double meanConsumerDesire = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_CONSUMER_DESIRE ) ) ) . doubleValue ( ) ; final double meanBankerRisk = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_BANKER_RISK ) ) ) . doubleValue ( ) ; final double rewiringProbability = ( ( Double ) ( parameters . getValue ( WorldBuilder . PARAMETER_REWIRING_PROBABILITY ) ) ) . doubleValue ( ) ; final int meanDegree = ( ( Integer ) ( parameters . getValue ( WorldBuilder . PARAMETER_MEAN_DEGREE ) ) ) . intValue ( ) ; int consumersPerNeighborhood = consumerCount // ( WorldBuilder . neighborhoodCount ) ; int consumersPerNeighborhoodR = ( consumersPerNeighborhood * ( WorldBuilder . neighborhoodCount ) ) - consumerCount ; int bankersPerNeighborhood = bankerCount // ( WorldBuilder . neighborhoodCount ) ; int bankersPerNeighborhoodR = ( bankersPerNeighborhood * ( WorldBuilder . neighborhoodCount ) ) - bankerCount ; int [ ] gridDim = getGridDim ( parameters ) ; int [ ] neighborhoodDim = getNeighborhoodDim ( parameters ) ; Grid < Object > grid = GridFactoryFinder . createGridFactory ( null ) . createGrid ( GRID_ID , context , new GridBuilderParameters < Object > ( new StrictBorders ( ) , new SimpleGridAdder < Object > ( ) , true , gridDim ) ) ; for ( int i = 0 ; i < ( WorldBuilder . neighborhoodCount ) ; i ++ ) { Neighborhood n = new Neighborhood ( ( "neighborhood_" + i ) , false , gridDim ) ; for ( int j = 0 ; j < ( gridDim [ 0 ] ) ; j ++ ) { for ( int k = 0 ; k < ( gridDim [ 1 ] ) ; k ++ ) { int x = ( i * ( gridDim [ 0 ] ) ) + ( j % ( neighborhoodDim [ 0 ] ) ) ; <START_BUG> int y = ( ( neighborhoodDim [ 0 ] ) // ( i * ( gridDim [ 0 ] ) ) ) + k ; <END_BUG> n . addCell ( new Cell ( x , y , n ) ) ; } } if ( i == ( ( WorldBuilder . neighborhoodCount ) - 1 ) ) { consumersPerNeighborhood = consumersPerNeighborhoodR ; } for ( int j = 0 ; j < consumersPerNeighborhood ; j ++ ) { double income = getNormalDist ( costOfLiving , maxIncome , meanIncome ) ; double spending = getNormalDist ( costOfLiving , income , meanSpending ) ; double risk = getNormalDist ( 0 , 1 , meanConsumerRisk ) ; double desire = getNormalDist ( 0 , 1 , meanConsumerDesire ) ; Consumer c = new Consumer ( WorldBuilder . stampId ( ) , income , spending , risk , desire ) ; Cell cell = n . getEmptyCell ( ) ; context . add ( c ) ; grid . moveTo ( c , cell . getCoordinates ( ) ) ; } if ( i == ( ( WorldBuilder . neighborhoodCount ) - 1 ) ) { bankersPerNeighborhood = bankersPerNeighborhoodR ; } for ( int j = 0 ; j < bankersPerNeighborhood ; j ++ ) { double riskThreshold = getNormalDist ( 0 , 1 , meanBankerRisk ) ; double assets = getNormalDist ( 100000 , 200000 , meanAssets ) ; Banker b = new Banker ( WorldBuilder . stampId ( ) , assets , riskThreshold ) ; Cell cell = n . getEmptyCell ( ) ; context . add ( b ) ; grid . moveTo ( b , cell . getCoordinates ( ) ) ; } context . add ( n ) ; }

Fix:
int y = ( ( i * ( gridDim [ 0 ] ) ) // ( neighborhoodDim [ 0 ] ) ) + k ;