Jump to content

Recommended Posts

Hey,

I am struggling to find a way to code an idea.

I have a list of 10 peoples (possibly less) and i'd like to give them roles on a team.

In this team, roles have differents importances. I have a roles array sort by importance.

For each roles i have an array of people who can achieve this role base on a value (The lowest value having the highest priority) . Everyone CANT do every roles.

I'd like to have the best team composition base on the role importance and the users roles values.

 

Any help ?

Ty

Link to comment
https://forums.phpfreaks.com/topic/314357-role-distribution/
Share on other sites

Its more about a coding problem on how i can put users on the good role. Im a php newby and i cant find a logic, a way to sort this the good way.

$in_team = array();

foreach ($roles_array as $role_id => $role) {

    foreach ($users_roles[$role_id] as $user_id => $score) {

        if (!in_array($user_id, $in_team)) {

            $final_array[$role_id] = $user_id;

            $in_team[] = $user_id;

            break;
        }
    }
}

I end up with this:

// Keys are role_id, values are user_id

$final_array = Array
(
    [2] => 1
    [10] => 613
    [1] => 751
    [6] => 709
    [4] => 649
    [3] => 797
    [5] => 594
    [8] => 445
)

But for exemple, i have lost role 7 which is a role with importance.

 

The priority is to have users in roles with the highest importance and in this roles having the user with the lowest score (the ones on the top of the Users arrays), with 1 user by role.

 

// Role list with keys as role_id, order by importance DESC

$roles_array = Array
(
    [2] => Array
        (
            [role_id] => 2
            [importance] => 9
        )

    [10] => Array
        (
            [role_id] => 10
            [importance] => 9
        )

    [1] => Array
        (
            [role_id] => 1
            [importance] => 8
        )

    [6] => Array
        (
            [role_id] => 6
            [importance] => 8
        )

    [4] => Array
        (
            [role_id] => 4
            [importance] => 7
        )

    [7] => Array
        (
            [role_id] => 7
            [importance] => 7
        )

    [3] => Array
        (
            [role_id] => 3
            [importance] => 0
        )

    [5] => Array
        (
            [role_id] => 5
            [importance] => 0
        )

    [8] => Array
        (
            [role_id] => 8
            [importance] => 0
        )

    [9] => Array
        (
            [role_id] => 9
            [importance] => 0
        )

)

// Users array by roles with role_id as key and a list of user_id (user_id as key) sort by user "score" the first one having the priority on the role.

$users_roles = Array
(
    [2] => Array
        (
            [1] => -10403083
            [649] => -10103685
            [445] => -10103190
            [613] => -10103162
            [594] => -10003838
        )

    [10] => Array
        (
            [1] => -10101899
            [613] => -10101799
            [649] => -10101599
            [445] => -10101299
        )

    [1] => Array
        (
            [751] => -9106772
            [613] => -9106679
            [709] => -9106281
            [1] => -9106179
            [445] => -9106175
            [594] => -9106087
            [797] => -9006662
        )

    [6] => Array
        (
            [751] => -9106772
            [613] => -9106679
            [709] => -9106281
            [1] => -9106179
            [445] => -9106175
            [594] => -9106087
            [797] => -9006662
        )

    [4] => Array
        (
            [613] => -8205090
            [649] => -8105661
            [709] => -8105270
            [1] => -8105188
            [751] => -8104676
        )

    [7] => Array
        (
            [613] => -8205090
            [649] => -8105661
            [709] => -8105270
            [1] => -8105188
            [751] => -8104676
        )

    [3] => Array
        (
            [797] => -1506653
            [751] => -1106899
            [1] => -1106894
            [649] => -1106793
            [613] => -1106299
            [709] => -1105790
            [594] => -1006679
        )

    [5] => Array
        (
            [594] => -1510886
            [797] => -1210750
            [613] => -1111984
            [751] => -1111673
            [1] => -1111193
            [709] => -1110979
            [445] => -1108879
            [649] => -1108683
        )

    [8] => Array
        (
            [594] => -1510886
            [797] => -1210750
            [613] => -1111984
            [751] => -1111673
            [1] => -1111193
            [709] => -1110979
            [445] => -1108879
            [649] => -1108683
        )

    [9] => Array
        (
            [594] => -1510886
            [797] => -1210750
            [613] => -1111984
            [751] => -1111673
            [1] => -1111193
            [709] => -1110979
            [445] => -1108879
            [649] => -1108683
        )

)

Thanks you

Link to comment
https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593023
Share on other sites

the sample data, which was probably defined to demonstrate this issue, ran out of users that could perform role id 7 before  reaching the role id 7 evaluation. when this occurs, you need to be willing to accept a less than ideal solution, by introducing a random element into the process.

a general method would be to take the top n (n = 1, 2, 3, ...) initial entries in the $roles_array, shuffle it/them, and produce a 'current' array consisting with those shuffled entries and the remaining unshuffled entries and try to produce a solution using that data that satisfies all the roles that have required importance. repeat the shuffling for each n value at least n squared times to give the shuffling a chance to produce unique results (computers are not very good at doing things randomly.) after each shuffle, check if the key order has already been tried, by remembering the composite key sequences, that have been evaluated, in an array, by imploding the keys with some unique character, e.g. a | or similar. if the key order has already been tried, skip the current evaluation loop.

 

  • Great Answer 1
Link to comment
https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593032
Share on other sites

It seems to me that mac_gyver has identified the key problem you have, which is that it appears you exhaust all the available players qualified for role 7 before arriving at role 7.  

This is equivalent to saying you have an open position for "chief surgeon" and only 4 candidates available, and you find that all 4 candidates already took other positions.  What can you do if you have no qualified applicants?  There is nothing to do -- the job sits open until you have more qualified applicants.

If you want to insure that you place someone, you could, if a group has no person in the role, continue on to check the next group, and to keep doing that until either you filled the role Or you again have gotten to the end of the users_roles array.  This will fill the role with the top available applicant in the following group(s) if possible.  

 

 

Link to comment
https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593047
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.