title: Specifying Logical Conditions (e.g. "greater than" and "less than") in R
body: <p>I am using the R programming language - I am trying to perform &quot;multi objective constrained optimization&quot;.</p>
  <p>I created some data for this example:</p>
  <pre><code>#load libraries
  library(dplyr)
  
  
  # create some data for this example
  a1 = rnorm(1000,100,10)
  b1 = rnorm(1000,100,5)
  c1 = sample.int(1000, 1000, replace = TRUE)
  train_data = data.frame(a1,b1,c1)
  </code></pre>
  <p>I then defined a function (&quot;funct_set&quot;) with &quot;4 objectives&quot; (f<a href="https://i.stack.imgur.com/o5EV0.png" rel="nofollow noreferrer">1</a>, f[2], f[3], f[4]) which are to be minimized for a set of &quot;seven inputs&quot; ([x1], [x2], [x3], x[4], x[5], x[6], x[7]):</p>
  <pre><code>#load libraries
      library(dplyr)
      library(mco)
      
  #define function
  
  funct_set &lt;- function (x) {
      x1 &lt;- x[1]; x2 &lt;- x[2]; x3 &lt;- x[3] ; x4 &lt;- x[4]; x5 &lt;- x[5]; x6 &lt;- x[6]; x[7] &lt;- x[7]
      f &lt;- numeric(4)
      
      
      #bin data according to random criteria
      train_data &lt;- train_data %&gt;%
          mutate(cat = ifelse(a1 &lt;= x1 &amp; b1 &lt;= x3, &quot;a&quot;,
                              ifelse(a1 &lt;= x2 &amp; b1 &lt;= x4, &quot;b&quot;, &quot;c&quot;)))
      
      train_data<span class="math-container">$cat = as.factor(train_data$</span>cat)
      
      #new splits
      a_table = train_data %&gt;%
          filter(cat == &quot;a&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      b_table = train_data %&gt;%
          filter(cat == &quot;b&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      c_table = train_data %&gt;%
          filter(cat == &quot;c&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      
      
      #calculate  quantile (&quot;quant&quot;) for each bin
      
      table_a = data.frame(a_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; x[5],1,0 )))
      
      table_b = data.frame(b_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; x[6],1,0 )))
      
      table_c = data.frame(c_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = ifelse(c1 &gt; x[7],1,0 )))
      
      f[1] = -mean(table_a<span class="math-container">$quant)
      f[2] = -mean(table_b$</span>quant)
      f[3] = -mean(table_c$quant)
      
      
      #group all tables
      
      final_table = rbind(table_a, table_b, table_c)
      # calculate the total mean : this is what needs to be optimized
      
      f[4] = -mean(final_table$quant)
      
      
      return (f);
  }
  Next, I define a series of 4 &quot;restrictions&quot; (i.e. logical conditions/constrains) used in the optimization:
  
  #define restrictions
  
  restrictions &lt;- function (x) {
      x1 &lt;- x[1]; x2 &lt;- x[2]; x3 &lt;- x[3]; x4 &lt;- x[4]; x5&lt;- x[5] ; x6 &lt;- x[6]; x7 &lt;- x[7]
      restrictions &lt;- logical(4)
      restrictions[1] &lt;- (x3 - x1 &gt;= 0)
      restrictions[2] &lt;- (x4 - x2 &gt;= 0)
      restrictions[3] &lt;- (x7 - x6 &gt;= 0)
   restrictions[4] &lt;- (x6 - x5 &gt;= 0)
      return (restrictions);
  }
  </code></pre>
  <p>Finally, I run the optimization algorithm that attempts to simultaneously minimize all 4 objectives with respect to the restrictions:</p>
  <pre><code>#run optimization
  
  
  optimization &lt;- nsga2(funct_set, idim = 7, odim = 4 ,   constraints = restrictions, cdim = 4,
                        
                        generations=150,
                        popsize=100,
                        cprob=0.7,
                        cdist=20,
                        mprob=0.2,
                        mdist=20,
                        lower.bounds=rep(80,80,80,80, 100,200,300),
                        upper.bounds=rep(120,120,120,120,200,300,400)
  )
  </code></pre>
  <p>The above code works fine.</p>
  <p>Problem : I noticed that in the output of this code, the optimization algorithm is not respecting the restrictions. For example:</p>
  <p><a href="https://i.stack.imgur.com/o5EV0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o5EV0.png" alt="enter image description here" /></a></p>
  <p>In the above picture, I have identified some rows where the logical conditions specified in the restrictions are violated.</p>
  <p>Does anyone know why this is happening? Have I incorrectly specified the restrictions? Can someone please show me how to fix this?</p>
  <p>Thanks</p>