Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Permutation Library

Author
Message
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 22nd Feb 2012 13:30 Edited at: 22nd Feb 2012 14:48
Hi there,

it's me again. With another small library. Again.

So anyway - this time it's about permutations. Not sure if there's anything available to handle permutations, so I wrote some functions to do the job.

What do you need permutations for in the first place? I'm glad that you ask! And to be honest, I'm not sure. I need them extremely rarely, but it seemed that they solve a quite specific problem every now and then, which is as follows:
Consider the case that you've got a list of something, e.g. AI units in a game. At some point you want an AI unit to pick a new target to attack. This target should be a random one, but it has to satisfy certain constraints (e.g. the target unit has to be an enemy).
There are many ways to get the job done:
1. Cycle through all units (for i=1 to UnitCount ...) and pick the first one that satisfies the constraints -> problem: units with low IDs will always be chosen first, which is not fair
2. Cycle through all units but starting at a random index (offset = rnd(UnitCount-1) : for i=1 to UnitCount : currentUnit = 1 + ((i+offset) mod UnitCount) ...) and pick the first one that satisfies the constraints -> problem: if there's a block of allied units followed by a block of enemy units, the first one of those enemy units will be attacked more often, which is still not fair
3. Cycle through all units, then make a list of all units who satisfy the given constraints, and pick a random unit out of this list -> fair, but difficult to code and potentially bad performance since this approach calculcates much more than necessary
4. Cycle through all units in completely random order and pick the first one to satisfy the constraints -> problem solved.


Method 4 can be done using permutations. For those of you who haven't heared of them, a permutation is basically a certain order of numbers. For example, (1 2 3 4 5) is a permutation of size 5 with all elements in correct oder. (1 2 3 5 4) is a permutation where 4 and 5 are swapped (so unit with index 5 would be checked before unit 4). (1 2 3 4 4) is not a valid permutation because every element has to be contained in the permutation exactly once.


Now about the library, it provides the following commands:
perm_init() - has to be called once after starting your program
perm_SetAsFirstMemblock(memID) - makes sure the library uses only memblocks with ID >= memID
permID = perm_Make(size) - creates an identity-permutation of given size
perm_Delete(permID) - deletes an existing permutation
index = perm_Get(permID, index) - returns the permutation value of a given index (with 1 <= index <= size)
perm_identity(permID) - sets a permutation to be the identity
perm_Shuffle(permID) - shuffles the permutation
perm_Flip(permID) - flips the values, so that (1 2 3) becomes (3 2 1), i.e. the reverse order
perm_Invert(targetPermID, sourcePermID) - inverts the source permutation, so that a chain of both permutations becomes the identity; all permutations need to have the same size
perm_Chain(targetPermID, sourcePermID1, sourcePermID2) - target permutation becomes the result of chaining both source permutations; all permutations need to have the same size
perm_toString(permID) - converts a permutation into a string, mainly used for debugging purposes



Code:



noobnerd
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 23rd Feb 2012 22:56
Cool ! And really tgc should hire you, the speed you chomp out theese things
I think could actually find uses for this!

Keep up the good work!

Login to post a reply

Server time is: 2024-04-25 05:32:52
Your offset time is: 2024-04-25 05:32:52