Search code examples
javaarrayseclipsemultidimensional-arrayrepast-simphony

Java 2D Array, Random value is getting assigned rather than the value that is supposed to be


Edit 3: This question has been answered. I did the math incorrectly multiple times and made a large blunder, and hence didn't realize the code base itself was fine

First question here on stackoverflow.

So I am running simulations, and in my code I need to model a depreciation of resources, which is simple a 25% reduction. Simple. But for some reason everytime when I change try to change the value to be 75% of the original value held in the 2D array, it changes it to be 100, and not whatever number. When I print out the value I am setting it equal to, it is giving me the right number, but when I actually set it, it just changes to 100. Any tips or help would be much appreciated.

Btw the arrays are global and protected 2d arrays of type int.

Code snippet: So in this code snippet, the System.out.println prints out the correct value when the actually assignment is commentated out, but when actually assigned it changes to 100.

I've tried a few options, and still trying to fix it, but I am curious to know if anyone else has run into a similar problem before?

Edit: I have updated the declaration of the array and the only other function the array gets changed in. The array values are fine after the resource extraction function, but during the consumption function everything bugs out when I try assigning a new value of 75% of whatever is currently held.

So for example, the numbers in the array is 4000, then after consumption it should be 3000, but instead of 3000 that line just prints out 72, because 72 is 3/4 of 97 after the truncation. But when the assignment is commented out and ran, all the numbers are getting incremented correctly to whatever number like 4869 for example.

Edit2:

I have posted the entirety of my code. The simulation is run over and over again. And therefore the extraction and consumption are ran over and over and over. I have done the math and the error that I posted still stands and only changes depending on the number of agents and resources.

Ex. agent = 24 and resource = 24, then after a few iterations it caps at 24 for some reason, and when agent = 8 and resource = 8, it caps at 72. Which I did do the math for and because of how the formulas work, they shouldn't be gaining the same amount every iteration, until a really high number like 10000 is reached.

Sorry for all the wordiness, and I appreciate all the help I have gotten so far!


Solution

  • The following works as expected:

    public class SO1 {
    
        int agentCount = 1;
        int resourceCount = 1;
        int[][] resourcesHeld = new int[agentCount][resourceCount];
    
        public void resourceConsumption() {
            for (int i = 0; i < agentCount; i++) {
                for (int j = 0; j < resourceCount; j++) {
                    resourcesHeld[i][j] = resourcesHeld[i][j] * 3 / 4;
                }
            }
        }
    
        public static void main(String[] args) {
            SO1 s = new SO1();
    
            s.resourcesHeld[0][0] = 4000;
            s.resourceConsumption();
            System.err.println(s.resourcesHeld[0][0]); // 3000
        }
    }
    

    I suspect that either the values in resourcesHeld are not what you think they are before resourceConsumption() call, or you modify them later.

    Can you amend your code to verify:

    public void resourceConsumption() {
        for (int i = 0; i < agentCount; i++) {
            for (int j = 0; j < resourceCount; j++) {
                System.err.println("BEFORE: " + resourcesHeld[i][j]);
                resourcesHeld[i][j] = resourcesHeld[i][j] * 3 / 4;
                System.err.println("AFTER: " + resourcesHeld[i][j]);
    
            }
        }
    }
    

    Edit

    Well, it looks like (in the case of 8 agents) after some iterations of step() the value of resourcesHeld is 97. Then resourceConsumption() brings is down to 72, then resourceExtract() bring it back to 97.

    In other words at some point resourceExtract() increases the values of resourcesHeld by 3/4 and resourceConsumption() decreases them back by 3/4. From this point onward you oscillate between those two values.