Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is quite challenging, but i found a working (maybe not the most performant) solution. My Example is written in PHP, but you should be able to adapt it. I'll try to explain the "thoughts" behind the code.</p> <p><strong>Note:</strong> It seems like you added the "10 persons, 3 Jobs" constraint later - or i simple did overread it. However, my code should give you an example that you <em>might</em> be able to adapt to that constraint. My Code currently is assuming that there are <code>n</code> jobs for <code>n</code> persons. (The most easiest way of adapting it to the 10/3 criteria, would be to split up the 3 Jobs into 10 equal units of work, when assuming 10 workers!)</p> <p><strong>First</strong>, lets create some basic architecture stuff. We need <code>person</code>, <code>job</code> and obviously a matrix, representing the satisfaction of a person towards a job. The following snipped does exactly that:</p> <pre><code>&lt;?php class Person{ var $name; var $prim; var $sec; function __construct($name, $prim, $sec){ $this-&gt;name = $name; $this-&gt;prim = $prim; $this-&gt;sec = $sec; } function likes($job){ if ($job-&gt;type == $this-&gt;prim) return 2; if ($job-&gt;type == $this-&gt;sec) return 1; else return -1; } } class Job{ var $name; var $type; function __construct($name, $type){ $this-&gt;name = $name; $this-&gt;type = $type; } } $persons = array( "Max" =&gt; new Person("Max", "programing", "testing"), "Peter" =&gt; new Person("Peter", "testing", "docu"), "Sam" =&gt; new Person("Sam", "designing", "testing") ); $jobs = array( "New Classes" =&gt; new Job("New Classes", "programing"), "Theme change" =&gt; new Job("Theme change", "designing"), "Test Controller" =&gt; new Job("Test Controller", "testing") ); // debug: draw it: echo "&lt;h2&gt;Happines with Jobs&lt;/h2&gt; "; echo "&lt;table border=1&gt;"; $p=0; echo "&lt;tr&gt;"; foreach ($jobs AS $job){ $j=0; foreach ($persons as $person){ if ($p++==0){ echo "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;"; foreach ($persons as $per) { echo "&lt;td&gt;".$per-&gt;name."&lt;/td&gt;"; } echo "&lt;/tr&gt;"; } if ($j++==0){ echo "&lt;td&gt;".$job-&gt;name."&lt;/td&gt;"; } echo "&lt;td&gt;".$person-&gt;likes($job)."&lt;/td&gt;"; } echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p>This will give you a table like this: </p> <p><img src="https://i.stack.imgur.com/5iYvD.jpg" alt="Happiness with Jobs"></p> <p><strong>Second</strong>, we need to create ALL permutations of jobs and persons. (actually we don't need to, but doing so, will show you the reason, why we don't need to!)</p> <p>To create all permutations, we are using just the <strong>name</strong> of a person or job. (we can resolve the name back to the actual object later)</p> <pre><code>//build up all permutations $personNames = array(); foreach ($persons AS $person){ $personNames[] = $person-&gt;name; } $jobNames = array(); foreach ($jobs AS $job){ $jobNames[] = $job-&gt;name; } $personsPerms = array(); pc_permute($personNames,$personsPerms); $jobsPerms = array(); pc_permute($jobNames,$jobsPerms); function pc_permute($items, &amp;$result, $perms = array( )) { if (empty($items)) { $result[] = join('/', $perms); } else { for ($i = count($items) - 1; $i &gt;= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); pc_permute($newitems,$result, $newperms); } } } </code></pre> <p>Now, we have 2 Arrays: All job permutations and all person permutations. For the Example given above, the arrays will look like this (3 Elements each, makes 3*2*1=6 Permutations per Array):</p> <pre><code>Array ( [0] =&gt; Max/Peter/Sam [1] =&gt; Peter/Max/Sam [2] =&gt; Max/Sam/Peter [3] =&gt; Sam/Max/Peter [4] =&gt; Peter/Sam/Max [5] =&gt; Sam/Peter/Max ) Array ( [0] =&gt; New Classes/Theme change/Test Controller [1] =&gt; Theme change/New Classes/Test Controller [2] =&gt; New Classes/Test Controller/Theme change [3] =&gt; Test Controller/New Classes/Theme change [4] =&gt; Theme change/Test Controller/New Classes [5] =&gt; Test Controller/Theme change/New Classes ) </code></pre> <p>Now, We can create a <code>nXn</code> Table, containing ALL Values of the overall satisfaction for ALL possible job allocations:</p> <pre><code>// debug: draw it: echo "&lt;h2&gt;Total Happines of Combination (full join)&lt;/h2&gt; "; echo "&lt;table border=1&gt;"; $p=0; echo "&lt;tr&gt;"; $row = 0; $calculated = array(); foreach ($jobsPerms AS $jobComb){ $j=0; $jobs_t = explode("/", $jobComb); foreach ($personsPerms as $personComb){ if ($p++==0){ echo "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;"; foreach ($personsPerms as $n) { echo "&lt;td&gt;".$n."&lt;/td&gt;"; } echo "&lt;/tr&gt;"; } if ($j++==0){ echo "&lt;td&gt;".$jobComb."&lt;/td&gt;"; } $persons_t = explode("/", $personComb); $h = 0; echo "&lt;td&gt;"; for ($i=0; $i&lt; count($persons_t); $i++){ $h += $persons[$persons_t[$i]]-&gt;likes($jobs[$jobs_t[$i]]); } echo $h; echo "&lt;/td&gt;"; } $col=0; $row++; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p><img src="https://i.stack.imgur.com/hJxt1.jpg" alt="enter image description here"></p> <p>Lets call this matrix "M"</p> <p>This Matrix contains a "lot" of double combinations: (a/b) TO (1/2) is equal to (b/a) to (2/1) etc...</p> <p>After all: We simple can ignore:</p> <ul> <li>Either Each row > 1</li> <li>OR Each column > 1</li> </ul> <p>Ignoring all Columns > 1:</p> <pre><code>echo "&lt;h2&gt;Total Happines of Combination (ignoring columns)&lt;/h2&gt; "; echo "&lt;table border=1&gt;"; $p=0; echo "&lt;tr&gt;"; $row = 0; $calculated = array(); foreach ($jobsPerms AS $jobComb){ $j=0; $jobs_t = explode("/", $jobComb); $col = 0; $personComb = $personsPerms[0]; if ($p++==0){ echo "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;"; echo "&lt;td&gt;".$personsPerms[0]."&lt;/td&gt;"; echo "&lt;/tr&gt;"; } if ($j++==0){ echo "&lt;td&gt;".$jobComb."&lt;/td&gt;"; } $persons_t = explode("/", $personComb); $h = 0; echo "&lt;td&gt;"; for ($i=0; $i&lt; count($persons_t); $i++){ $h += $persons[$persons_t[$i]]-&gt;likes($jobs[$jobs_t[$i]]); } echo $h; echo "&lt;/td&gt;"; $col=0; $row++; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p>Output:</p> <p><img src="https://i.stack.imgur.com/K4rui.jpg" alt="enter image description here"></p> <p>And there you go! In this Example (one of) the most satisfying Solution would be:</p> <ul> <li>Max -> New Classes (+2)</li> <li>Peter -> Test Controller (+2)</li> <li>Sam -> Theme Change (+2)</li> </ul> <p>-> Happiness: 6.</p> <p>There are other, equal distributions as well.</p> <p>Example: 6 persons / 6 jobs:</p> <pre><code>$persons = array( "Max" =&gt; new Person("Max", "programing", "testing"), "Peter" =&gt; new Person("Peter", "testing", "docu"), "Sam" =&gt; new Person("Sam", "designing", "testing"), "Jeff" =&gt; new Person("Jeff", "docu", "programing"), "Fred" =&gt; new Person("Fred", "programing", "designing"), "Daniel" =&gt; new Person("Daniel", "designing", "docu") ); $jobs = array( "New Classes" =&gt; new Job("New Classes", "programing"), "Theme change" =&gt; new Job("Theme change", "designing"), "Test Controller" =&gt; new Job("Test Controller", "testing"), "Create Manual" =&gt; new Job("Create Manual", "docu"), "Program more!" =&gt; new Job("Program more!", "programing"), "Style the frontend" =&gt; new Job("Style the frontend", "designing") ); </code></pre> <p><img src="https://i.stack.imgur.com/0FsYZ.jpg" alt="enter image description here"></p> <p>results in (Persons: Max / Peter / Sam / Jeff / Fred / Daniel)</p> <p><img src="https://i.stack.imgur.com/CoLbC.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload