Note that there are some explanatory texts on larger screens.

plurals
  1. POconstrained optimization of quadratic function
    text
    copied!<p>I am using the <a href="http://cran.r-project.org/web/packages/alabama/alabama.pdf" rel="nofollow">alabama package</a> to optimize a non-linear constrained optimization problem.</p> <p>The problem is:</p> <pre><code> minimise: -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3] +0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5]) </code></pre> <p>The equality constraint is:</p> <pre><code>x[1]+ x[2]+x[3]+x[4]+x[5] = 2600 </code></pre> <p>The inequality constraints are:</p> <pre><code>x[1]&gt; 900 x[1] &lt; 1100 x[2] &gt; 400 x[2] &lt; 600 x[3] &gt; 250 x[3] &lt; 350 x[4] &gt; 175 x[4] &lt; 225 x[5] &gt; 295 x[5] &lt; 305 </code></pre> <p>Here is what I am trying:</p> <pre><code>fn &lt;- function() { -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3] + 0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5]) } heq &lt;- function(x) { x[1] + x[2] + x[3] + x[4] +x[5] - 2600 } hin &lt;- function(x) { h &lt;- rep(NA, 1) h[1] &lt;- x[1] - 900 h[2] &lt;- 1100 - x[1] h[3] &lt;- x[2] - 400 h[4] &lt;- 600 - x[2] h[5] &lt;- x[3] - 250 h[6] &lt;- 350 - x[3] h[7] &lt;- x[4] - 175 h[8] &lt;- 225 - x[4] h[9] &lt;- x[5] - 295 h[10] &lt;- 305 - x[5] h } </code></pre> <p>Here are the various problems I face with different values of par:</p> <p>Case1:</p> <pre><code>ans &lt;- auglag(par= NULL,fn=fn, gr=NULL,hin=hin, heq=heq) Error in h[1] &lt;- x[1] - 900 : replacement has length zero </code></pre> <p>Case2:</p> <pre><code>ans &lt;- auglag(par= c(1,1,1,1,1),fn=fn,hin=hin, heq=heq) Error in h[1] &lt;- x[1] + x[2] + x[3] + x[4] + x[5] - 2600 : object 'h' not found </code></pre> <p>Case3: </p> <pre><code> ans &lt;- auglag(par= c(1000,500,300,200,300),fn=fn,hin=hin, heq=heq) Error in h[1] &lt;- x[1] + x[2] + x[3] + x[4] + x[5] - 2600 : object 'h' not found </code></pre> <p>Case4:</p> <pre><code>ans &lt;- auglag(par=NULL,fn=fn, gr=NULL,hin=hin,heq=heq) Error in h[1] &lt;- x[1] - 900 : replacement has length zero </code></pre> <p>What is the correct way to apply auglag or constrOptim.nl? I tried solving it through <a href="http://svitsrv25.epfl.ch/R-doc/library/quadprog/html/solve.QP.html" rel="nofollow">solve.QP</a> but couldn't undestand what parameters to pass.</p> <p>After the edits made by @Hong Ooi, here are the new errors:</p> <pre><code>&gt; ans &lt;- auglag(par= NULL,fn=fn, gr=NULL,hin=hin, heq=heq) Error in h[1] &lt;- x[1] - 900 : replacement has length zero &gt; ans &lt;- auglag(par= c(1,1,1,1,1),fn=fn,hin=hin, heq=heq) Error in fn(par, ...) : unused argument(s) (par) &gt; ans &lt;- auglag(par= c(1000,500,300,200,300),fn=fn,hin=hin, heq=heq) Error in fn(par, ...) : unused argument(s) (par) &gt; ans &lt;- auglag(par=NULL,fn=fn, gr=NULL,hin=hin,heq=heq) Error in h[1] &lt;- x[1] - 900 : replacement has length zero </code></pre>
 

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