simulation help

0 favourites
  • 5 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I'm experimenting with a simulation.

    A person agent has an income amount and needs to decide how to best allocate their money when buying goods.

    I have each good type as a sprite. They have Type, Cost and Priority variables.

    Each sprite has 4 instances with different variables, representing 4 choices of the same good type.

    The first always has a Cost of 0, and Type "none" and priority 1, to represent the option that a person agent doesn't want, or can't afford to buy any of that good.

    The other instances are given different Priority values indicating how much the person prioritises or desires spending on that good, and different Cost values.

    The idea is that these values, income, cost, priority, change through out the simulation and so people change their buying habits to maximise their spending to get the best mix of goods for their money.

    The way i have it set up is;

    	
    
    
    //First i dismiss any goods that are already more expensive than the persons income.
    
    all goods cost > person income then set boolean too_expesive true.
    
    //now check each possible combination of goods and find the combination with the highest combined Priority and lowest Cost.
    
    if boolean too_expesive = false
    for each good A
    
    	if boolean too_expesive = false
    	for each good B
    	good A cost + good B cost <= persons income
    
    		if boolean too_expesive = false
    		for each good C
    		good A cost + good B cost + good C cost <= persons income
    
    		---- Set total cost = good A cost + good B cost + good C cost
    		---- Set total priority = good A priority * good B priority * good C priority
    
    			if total cost <= persons income
    			if total priority => best total priority
    			
    			---- Set best total priority = total priority
    			---- Set chosen good A uid = good A uid
    			---- Set chosen good B uid = good B uid
    			---- Set chosen good C uid = good C uid
    
    

    This method works well, but the more goods you add the more exponential the calculations become, example;

    each good has 4 options times the number of goods types so with 3 good types that 4 x 4 x 4 = 64 combinations. 4 goods is 256, 5 goods 1024, 6 good types 4096 etc.

    Now if you have lots of agents as well the whole things becomes too processor intensive.

    Is there any other method or a better way of doing it that accomplishes the same thing?

    Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • There’s a branch of math that deals with stuff like that called

    en.m.wikipedia.org/wiki/Mathematical_optimization

    But Wikipedia probably doesn’t have the most understandable examples. A simple example of that is maximizing the a fenced in area with the least amount of fence. Although I’m not certain it has any helpful tricks in your case since the triple loop is the one causing the calculation to take longer.

    Algorithmically you might be able to shorten the loops by sorting the lists of a,c and c by cost so you could use stop the loops when you know the rest of the instances were too expensive. Something like this:

    For each A ordered by cost
    - if A> income then stop loop 
    - for each B ordered by cost
    - - if A+B> income then stop loop
    - - for each C ordered by cost
    - - - if A+B+C>income then stop loop
    - - - look at priority…

    Since it is a simulation and if you’re happy with a good solution but not necessarily the best solution you could just compare three random instances of A,b,c multiple times. Could seem more realistic in a way since given the same problem humans would do that. Something like this:

    repeat 100 times
    Pick random A
    Pick random B
    Pick random C
    — do stuff

    Even that probably could be done better with a weighted random with a sorted list so that higher priority stuff is looked at more than low priority stuff. Or maybe sort the lists by cost and pick random instances by a normal random distribution (bell curve). That would make the stuff in the middle of the cost range be picked more than the the expensive or cheap stuff. To do that you’d need an array of the instances sorted by cost and a function to get a normally distributed random instead of the standard uniform one. I forget if c3 provides that.

    Array of A instances
    Sort array by cost
    i=int(normalRandom(array.width))

    Anyways just some ideas.

  • Thanks for your suggestions. I'll give them some thought.

    I did try another method now, where i do one ordered for each loop on all goods based on priority descending, buying and reducing a persons money each time, and discarding other goods of the same type each loop. As the money goes down, they sacrifice priority for affordability.

    Its acceptably fast so far, but its a slightly different behaviour, as its not an optimum distribution of capital like before.

  • Another option to make it faster is to do the same thing in JavaScript. That would churn through the loops faster than the event sheet. Further down that rabbit trail is wasm but not sure it’s worth all the extra effort. Faster still could be to somehow leverage the gpu to do it in parallel. Maybe pack all the data into a texture, run that through a shader onto an output shader, and read back the pixels for the result. I may be mistaken though since I’ve never attempted that and your sim may not be able to be parallelized.

  • Sounds interesting, but way outside my capabilities.

    I will see how things go.

    Thanks.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)