title: Linear programming in R using lpsolve
body: <p>I'm trying to solve a linear programming problem in <code>R</code> using <code>lpsolve</code> package.</p>
  
  <p>Here is the problem:</p>
  
  <p><a href="https://i.stack.imgur.com/fsRFB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fsRFB.jpg" alt="enter image description here"></a></p>
  
  <p>Here is the sample in R for reproducible example:</p>
  
  <pre><code>    library("lpSolve")
  a &lt;- matrix(c(1,2,5,
                1/2,1,3,
                1/5,1/3,1),nrow=3,byrow=T)
  
  #
  f.obj &lt;- c(1,0,0,0)
  
  f.con &lt;- matrix (c(
    1,1,-a[1,2],0, #Contraint 1 for a12
    1,-1,a[1,2],0, #Contraint 2 for a12
    1,1,0,-a[1,3], #Contraint 1 for a13
    1,-1,0,a[1,3], #Contraint 2 for a13
    1,0,1,-a[2,3], #Contraint 1 for a23
    1,0,-1,a[2,3], #Contraint 2 for a23
    0,1,1,1, #Contraint 3
    0,1,0,0, #Constraint 4
    0,0,1,0, #Constraint 4
    0,0,0,1 #Constraint 4
  
    ), nrow=10, byrow=TRUE)
  
  f.dir &lt;- c(rep("&lt;=",6), "=",rep("&gt;",3))
  
  f.rhs &lt;- c(rep(1,6),1,rep(0,3))
  
  g &lt;- lp ("max", f.obj, f.con, f.dir, f.rhs)
  g$solution
  </code></pre>
  
  <p>I'm able to solve this manually for a small problem, what if I had a <code>7 X 7</code> or a <code>n x n</code> matrix <code>a</code>. How would I specify the constraint <code>1</code> and <code>2</code>, especially I'm struggling to define the constraint as it relates to a[i,j]?</p>
  
  <pre><code>a = matrix( 
    c(1,4,9,6,6,5,5,
      1/4,1,7,5,5,3,4,
      1/9,1/7,1,1/5,1/5,1/7,1/5,
      1/6,1/5,5,1,1,1/3,1/3,
      1/6,1/5,5,1,1,1/3,1/3,
      1/5,1/3,7,3,3,1,2,
      1/5,1/4,5,3,3,1/2,1
    ),nrow = 7,byrow =T)
  </code></pre>
  
  <p>the solution to the above matrix is <code>0.986 0.501 0.160 0.043 0.060 0.060 0.1 0.075</code> Any help would be greatly appreciated.</p>