Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to optimize the following function</p> <pre><code>$cost = $amountOfProductA * $costOfProductA + $amountOfProductB * $costOfProductB + $amountOfProductC * $costOfProductC </code></pre> <p>With the following restriction</p> <pre><code>$powerDeliveredByA * $amountOfProductA + $powerDeliveredByB * $amountOfProductB + $powerDeliveredByC * $amountOfProductC = 65 </code></pre> <p>So these lines find solutions that yield 65 (or close to 65, using an acceptable threshold you'd have to set), then sort the solutions array by the cost, and get the first element of the solutions array:</p> <pre><code>$requiredPower = 65; $productA = array('amount' =&gt; 0, 'cost' =&gt; 50, 'powerDelivered' =&gt; 5); $productB = array('amount' =&gt; 0, 'cost' =&gt; 80, 'powerDelivered' =&gt; 9); $productC = array('amount' =&gt; 0, 'cost' =&gt; 140, 'powerDelivered' =&gt; 15); $increment = 0.01; $threshold = 0.01; $solutions = array(); while($productA['amount'] * $productA['powerDelivered'] &lt; $requiredPower) { $productC['amount'] = 0; while($productB['amount'] * $productB['powerDelivered'] &lt; $requiredPower) { $productC['amount'] = 0; while($productC['amount'] * $productC['powerDelivered'] &lt; $requiredPower) { if($productA['amount'] * $productA['powerDelivered'] + $productB['amount'] * $productB['powerDelivered'] + $productC['amount'] * $productC['powerDelivered'] &gt; $requiredPower + $threshold) { break; } if(isWithinThreshold($productA['powerDelivered'] * $productA['amount'] + $productB['powerDelivered'] * $productB['amount'] + $productC['powerDelivered'] * $productC['amount'], $requiredPower, $threshold)) { //var_dump($productA['powerDelivered'] * $productA['amount'] + $productB['powerDelivered'] * $productB['amount'] + $productC['powerDelivered'] * $productC['amount']); $cost = $productA['amount'] * $productA['cost'] + $productB['amount'] * $productB['cost'] + $productC['amount'] * $productC['cost']; $solutions[number_format($cost,10,'.','')] = array('cost' =&gt; $cost, 'qA' =&gt; $productA['amount'], 'qB' =&gt; $productB['amount'], 'qC' =&gt; $productC['amount']); } $productC['amount'] = $productC['amount'] + $increment; } $productB['amount'] = $productB['amount'] + $increment; } $productA['amount'] = $productA['amount'] + $increment; } ksort($solutions, SORT_NUMERIC); $minimumCost = array_shift($solutions); var_dump($minimumCost); //checks if $value1 is within $value2 +- $threshold function isWithinThreshold($value1, $value2, $threshold) { if($value1 &gt;= $value2 - $threshold &amp;&amp; $value1 &lt;= $value2 + $threshold) { return true; } } </code></pre> <p>The way to optimize a function is described here: <a href="http://en.wikipedia.org/wiki/Knapsack_problem" rel="nofollow">Function Optimization</a></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. 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.
    3. 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