title: Relating input to output with Neural Network and optimizing inputs using Genetic Algorithm
body: <p>I'm currently doing proces optimization for laser cutting - in MATLAB. I am trying to relate the process parameters with the cutting quality such as e.g.:</p>
  
  <p>Inputs <em>(Process parameters)</em></p>
  
  <ol>
  <li>Cutting speed</li>
  <li>Laser power</li>
  <li>Assist gas pressure</li>
  </ol>
  
  <p>Outputs <em>(Quality parameters)</em></p>
  
  <ol>
  <li>Cutting depth</li>
  <li>Cutting width</li>
  </ol>
  
  <p>I am first training a Neural Network model in order to predict the cutting quality from the process parameters.</p>
  
  <pre><code>[inputs,targets] = lasercutting_dataset;
  
  nLayers = 2;            % number of hidden layers
  trainFcn = 'trainlm';   % Levenberg Marqhart training function
  
  net = fitnet(nLayers,trainFcn);
  </code></pre>
  
  <p>This works fine, and for now I am not interested in the performance.</p>
  
  <p>Next I want to optimize (maximize) the input parameter cutting speed using Genetic Algorithm. This means my fitness function (object function) is 1/cutting speed.</p>
  
  <p>I have made a matlab function for my fitness function:</p>
  
  <pre><code>function y = fitness(x)
  
      y = 1/x(1);
  
  end
  </code></pre>
  
  <p>Next i set the number of design variables and their upper and lower bounds:</p>
  
  <pre><code>nvars = 3;    % Number of variables
  LB = [130 8130  4470];   % Lower bound
  UB = [175 11255 8250];  % Upper bound
  </code></pre>
  
  <p>Next i define my constraint on the cutting width (I will be adding one for the depth as well).</p>
  
  <pre><code>function [c, ceq] = constraints(net,x) 
  
      outs = net(x)
  
      c = [outs(2)+495; outs(2)-505];
      % 495 &lt;= outs
      % outs &lt;= 505
  
      ceq = [];
  
  end
  </code></pre>
  
  <p>This function is the one causing problems!</p>
  
  <p>Lastly I make handles for the functions and start the optimization:</p>
  
  <pre><code>[x,fval] = ga(@fitness_func,nvars,[],[],[],[],LB,UB,@(x) ConstraintFunction(net,x));
  </code></pre>
  
  <p>I have tried defining an input vector for the neural network (net()) inside the constraints function which was independent of x - this worked fine:</p>
  
  <pre><code>dummyInput = [value; value; value];
  net(dummyInput);
  </code></pre>
  
  <p>I'm among others getting the error, that my functions constraints is not getting enough input parameters.</p>
  
  <p>The problem seems to be something with passing the dynamically changing "x" to a function and then predicting on it, using the output for a set of dynamically changing constraints.</p>
  
  <p>Any ideas as to what might be the problem?</p>
  
  <p>I've been using <a href="http://www.mathworks.se/support/solutions/en/data/1-EFLUUB/?solution=1-EFLUUB" rel="nofollow">THIS</a> and <a href="http://www.mathworks.se/help/gads/examples/constrained-minimization-using-the-genetic-algorithm.html" rel="nofollow">THIS</a> for inspiration.</p>
  
  <p>Any help is appreciated - sorry for the long questiong. I have several articles on the matter - none of which explains the problem in matlab, merely the procedure.</p>