Linear Programming Example

Main.LinearProgramming History

Hide minor edits - Show changes to markup

April 16, 2023, at 01:42 PM by 136.36.4.38 -
Deleted lines 126-233:
  1. Generate a contour plot
  1. Import some other libraries that we'll need
  2. matplotlib and numpy packages must also be installed

import matplotlib import numpy as np import matplotlib.pyplot as plt

  1. Design variables at mesh points

x = np.arange(-1.0, 8.0, 0.02) y = np.arange(-1.0, 6.0, 0.02) x1, x2 = np.meshgrid(x,y)

  1. Equations and Constraints

profit = 100.0 * x1 + 125.0 * x2 A_usage = 3.0 * x1 + 6.0 * x2 B_usage = 8.0 * x1 + 4.0 * x2

  1. Create a contour plot

plt.figure()

  1. Weight contours

lines = np.linspace(100.0,800.0,8) CS = plt.contour(x1,x2,profit,lines,colors='g') plt.clabel(CS, inline=1, fontsize=10)

  1. A usage < 30

CS = plt.contour(x1,x2,A_usage,[26.0, 28.0, 30.0],colors='r',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. B usage < 44

CS = plt.contour(x1, x2,B_usage,[40.0,42.0,44.0],colors='b',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 1 <= 500 L

CS = plt.contour(x1, x2,x1 ,[0.0, 0.1, 4.9, 5.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 2 <= 400 L

CS = plt.contour(x1, x2,x2 ,[0.0, 0.1, 3.9, 4.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Add some labels

plt.title('Soft Drink Production Problem') plt.xlabel('Product 1 (100 L)') plt.ylabel('Product 2 (100 L)')

  1. Save the figure as a PNG

plt.savefig('contour.png')

  1. Show the plots

plt.show() (:sourceend:) (:divend:)

The linear program is solved with the APM model through a web-service while the contour plot is generated with the Python package Matplotlib.

(:toggle hide mycode2 button show="Show APM Python Source with Contours":) (:div id=mycode2:) (:source lang=python:)

  1. Import APM Python library

try:

    from APMonitor import *

except:

    # Automatically install APMonitor
    import pip
    pip.main(['install','APMonitor'])
    from APMonitor import *
  1. Select the server

server = 'https://byu.apmonitor.com'

  1. Give the application a name

app = 'production'

  1. Clear any previous applications by that name

apm(server,app,'clear all')

  1. Write the model file

fid = open('softdrink.apm','w') fid.write('Variables \n') fid.write(' x1 > 0 , < 5 ! Product 1 \n') fid.write(' x2 > 0 , < 4 ! Product 2 \n') fid.write(' profit \n') fid.write(' \n') fid.write('Equations \n') fid.write(' ! profit function \n') fid.write(' maximize profit \n') fid.write(' profit = 100 * x1 + 125 * x2 \n') fid.write(' 3 * x1 + 6 * x2 <= 30 \n') fid.write(' 8 * x1 + 4 * x2 <= 44 \n') fid.close() apm_load(server,app,'softdrink.apm')

  1. Solve on APM server

solver_output = apm(server,app,'solve')

  1. Display solver output

print(solver_output)

  1. Retrieve results

sol = apm_sol(server,app)

print ('') print (-- Results of the Optimization Problem --) print ('Product 1 (x1): ' + str(sol['x1'])) print ('Product 2 (x2): ' + str(sol['x2'])) print ('Profit: ' + str(sol['profit']))

  1. Display Results in Web Viewer

url = apm_web_var(server,app)

April 16, 2023, at 01:41 PM by 136.36.4.38 -
Added lines 5-6:

Linear programming (LP) is a mathematical optimization technique used to solve problems with a linear objective function and linear constraints. Linear Programming maximizes or minimizes a linear objective function of several variables subject to constraints that are also linear in the same variables.

Deleted lines 13-14:

Linear programming (LP) is a mathematical optimization technique used to solve problems with a linear objective function and linear constraints. Linear Programming maximizes or minimizes a linear objective function of several variables subject to constraints that are also linear in the same variables.

April 16, 2023, at 01:40 PM by 136.36.4.38 -
Changed line 13 from:

Linear programming (LP) is a mathematical optimization technique used to solve problems with a linear objective function and linear constraints. LP maximizes or minimizes a linear objective function of several variables subject to constraints that are also linear in the same variables.

to:

Linear programming (LP) is a mathematical optimization technique used to solve problems with a linear objective function and linear constraints. Linear Programming maximizes or minimizes a linear objective function of several variables subject to constraints that are also linear in the same variables.

April 16, 2023, at 01:40 PM by 136.36.4.38 -
Changed lines 5-6 from:

See Optimization with Python for examples with:

to:

See Optimization with Python for examples of:

Added lines 12-17:

Linear programming (LP) is a mathematical optimization technique used to solve problems with a linear objective function and linear constraints. LP maximizes or minimizes a linear objective function of several variables subject to constraints that are also linear in the same variables.

Linear programming is widely used in engineering decision-making to optimize resource allocation, such as minimize costs, maximize profits, or maximize output, subject to a set of constraints. For example, a refinery might use linear programming to determine the optimal crude oils to purchase to maximize profit from their specific equipment that is subject to processing constraints and can make a certain amount of each refined product. Linear programming can also be used to solve complex logistical problems, such as a schedule production runs, determine optimal transportation routes, and minimize inventory costs.

An advantage of linear programming over nonlinear programming is the ability to handle a large number of variables and constraints, model complex situations, and provide an explainable, quantitative basis for decision-making. By using linear programming, engineers can make more informed decisions, reduce costs, and increase efficiency.

April 16, 2023, at 01:33 PM by 136.36.4.38 -
Changed lines 7-11 from:
  • Linear Programming (LP)
  • Quadratic Programming (QP)
  • Nonlinear Programming (NLP)
  • Mixed Integer LP (MILP)
  • Mixed Integer NLP (MINLP)
to:
  • 1️⃣ Linear Programming (LP)
  • 2️⃣ Quadratic Programming (QP)
  • 3️⃣ Nonlinear Programming (NLP)
  • 4️⃣ Mixed Integer Linear Programming (MILP)
  • 5️⃣ Mixed Integer Nonlinear Programming (MINLP)
April 16, 2023, at 01:33 PM by 136.36.4.38 -
Changed line 13 from:

Refinery Example LP Problem

to:

Refinery Example LP Problem

April 16, 2023, at 01:32 PM by 136.36.4.38 -
Added lines 4-13:

See Optimization with Python for examples with:

  • Linear Programming (LP)
  • Quadratic Programming (QP)
  • Nonlinear Programming (NLP)
  • Mixed Integer LP (MILP)
  • Mixed Integer NLP (MINLP)

Refinery Example LP Problem

Added lines 61-62:

See additional solution options for solving Linear Programming with Python.

Changed line 97 from:

m.Obj(-profit) # maximize

to:

m.Maximize(profit)

June 21, 2020, at 04:43 AM by 136.36.211.159 -
Deleted lines 264-282:

(:html:)

 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

(:htmlend:)

December 04, 2019, at 04:44 AM by 12.244.228.90 -
Added lines 33-36:

(:html:) <iframe width="560" height="315" src="https://www.youtube.com/embed/2dY_tRamSjY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> (:htmlend:)

December 04, 2019, at 03:54 AM by 12.244.228.90 -
Changed lines 27-29 from:

A simple production planning problem is given by the use of two ingredients A and B that produce products 1 and 2. In this case, it requires:

  • 3 units of A and 6 units of B to produce Product 1
  • 8 units of A and 4 units of B to produce Product 2
to:

A simple production planning problem is given by the use of two ingredients A and B that produce products 1 and 2. The available supply of A is 30 units and B is 44 units. For production it requires:

  • 3 units of A and 8 units of B to produce Product 1
  • 6 units of A and 4 units of B to produce Product 2
December 04, 2019, at 03:48 AM by 12.244.228.90 -
Changed line 43 from:

m.Obj(-profit) # maximize

to:

m.Maximize(profit) # maximize

June 01, 2019, at 02:56 PM by 45.56.3.173 -
Changed lines 25-26 from:

Soft Drink Production Problem (Example 2)

to:

Soft Drink Production Problem (Example 2)

Deleted lines 32-33:

Deleted lines 56-57:

Deleted lines 76-77:

June 01, 2019, at 02:51 PM by 45.56.3.173 -
Changed lines 86-91 from:

Below are the source files for generating the contour plots in Python. The linear program is solved with the APM model through a web-service while the contour plot is generated with the Python package Matplotlib.

(:toggle hide mycode1 button show="Show Gekko Python Source":)

to:

Below are the source files for generating the contour plots in GEKKO Python and APM Python.

(:toggle hide mycode1 button show="Show Gekko Python Source with Contours":)

Changed lines 159-164 from:

(:toggle hide mycode2 button show="Show APM Python Source":)

to:

The linear program is solved with the APM model through a web-service while the contour plot is generated with the Python package Matplotlib.

(:toggle hide mycode2 button show="Show APM Python Source with Contours":)

June 01, 2019, at 02:49 PM by 45.56.3.173 -
Added lines 34-61:

Python Solution

(:source lang=python:) from gekko import GEKKO

m = GEKKO() x1 = m.Var(lb=0,ub=5) x2 = m.Var(lb=0,ub=4) profit = m.Var()

m.Obj(-profit) # maximize m.Equation(profit==100*x1 + 125*x2) m.Equation(3*x1+6*x2<=30) m.Equation(8*x1+4*x2<=44)

m.solve()

print ('') print (-- Results of the Optimization Problem --) print ('Product 1 (x1): ' + str(x1.value)) print ('Product 2 (x2): ' + str(x2.value)) print ('Profit: ' + str(profit.value)) (:sourceend:)


Contour Plot

June 01, 2019, at 02:47 PM by 45.56.3.173 -
Added lines 63-64:

(:toggle hide mycode1 button show="Show Gekko Python Source":) (:div id=mycode1:)

Changed lines 66-108 from:
  1. Import APM Python library

try:

    from APMonitor import *

except:

    # Automatically install APMonitor
    import pip
    pip.main(['install','APMonitor'])
    from APMonitor import *
  1. Select the server

server = 'https://byu.apmonitor.com'

  1. Give the application a name

app = 'production'

  1. Clear any previous applications by that name

apm(server,app,'clear all')

  1. Write the model file

fid = open('softdrink.apm','w') fid.write('Variables \n') fid.write(' x1 > 0 , < 5 ! Product 1 \n') fid.write(' x2 > 0 , < 4 ! Product 2 \n') fid.write(' profit \n') fid.write(' \n') fid.write('Equations \n') fid.write(' ! profit function \n') fid.write(' maximize profit \n') fid.write(' profit = 100 * x1 + 125 * x2 \n') fid.write(' 3 * x1 + 6 * x2 <= 30 \n') fid.write(' 8 * x1 + 4 * x2 <= 44 \n') fid.close() apm_load(server,app,'softdrink.apm')

  1. Solve on APM server

solver_output = apm(server,app,'solve')

  1. Display solver output

print(solver_output)

  1. Retrieve results

sol = apm_sol(server,app)

to:

from gekko import GEKKO

m = GEKKO() x1 = m.Var(lb=0,ub=5) x2 = m.Var(lb=0,ub=4) profit = m.Var()

m.Obj(-profit) # maximize m.Equation(profit==100*x1 + 125*x2) m.Equation(3*x1+6*x2<=30) m.Equation(8*x1+4*x2<=44)

m.solve()

Changed lines 82-88 from:

print ('Product 1 (x1): ' + str(sol['x1'])) print ('Product 2 (x2): ' + str(sol['x2'])) print ('Profit: ' + str(sol['profit']))

  1. Display Results in Web Viewer

url = apm_web_var(server,app)

to:

print ('Product 1 (x1): ' + str(x1.value)) print ('Product 2 (x2): ' + str(x2.value)) print ('Profit: ' + str(profit.value))

Added lines 132-235:

(:divend:)

(:toggle hide mycode2 button show="Show APM Python Source":) (:div id=mycode2:) (:source lang=python:)

  1. Import APM Python library

try:

    from APMonitor import *

except:

    # Automatically install APMonitor
    import pip
    pip.main(['install','APMonitor'])
    from APMonitor import *
  1. Select the server

server = 'https://byu.apmonitor.com'

  1. Give the application a name

app = 'production'

  1. Clear any previous applications by that name

apm(server,app,'clear all')

  1. Write the model file

fid = open('softdrink.apm','w') fid.write('Variables \n') fid.write(' x1 > 0 , < 5 ! Product 1 \n') fid.write(' x2 > 0 , < 4 ! Product 2 \n') fid.write(' profit \n') fid.write(' \n') fid.write('Equations \n') fid.write(' ! profit function \n') fid.write(' maximize profit \n') fid.write(' profit = 100 * x1 + 125 * x2 \n') fid.write(' 3 * x1 + 6 * x2 <= 30 \n') fid.write(' 8 * x1 + 4 * x2 <= 44 \n') fid.close() apm_load(server,app,'softdrink.apm')

  1. Solve on APM server

solver_output = apm(server,app,'solve')

  1. Display solver output

print(solver_output)

  1. Retrieve results

sol = apm_sol(server,app)

print ('') print (-- Results of the Optimization Problem --) print ('Product 1 (x1): ' + str(sol['x1'])) print ('Product 2 (x2): ' + str(sol['x2'])) print ('Profit: ' + str(sol['profit']))

  1. Display Results in Web Viewer

url = apm_web_var(server,app)

  1. Generate a contour plot
  1. Import some other libraries that we'll need
  2. matplotlib and numpy packages must also be installed

import matplotlib import numpy as np import matplotlib.pyplot as plt

  1. Design variables at mesh points

x = np.arange(-1.0, 8.0, 0.02) y = np.arange(-1.0, 6.0, 0.02) x1, x2 = np.meshgrid(x,y)

  1. Equations and Constraints

profit = 100.0 * x1 + 125.0 * x2 A_usage = 3.0 * x1 + 6.0 * x2 B_usage = 8.0 * x1 + 4.0 * x2

  1. Create a contour plot

plt.figure()

  1. Weight contours

lines = np.linspace(100.0,800.0,8) CS = plt.contour(x1,x2,profit,lines,colors='g') plt.clabel(CS, inline=1, fontsize=10)

  1. A usage < 30

CS = plt.contour(x1,x2,A_usage,[26.0, 28.0, 30.0],colors='r',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. B usage < 44

CS = plt.contour(x1, x2,B_usage,[40.0,42.0,44.0],colors='b',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 1 <= 500 L

CS = plt.contour(x1, x2,x1 ,[0.0, 0.1, 4.9, 5.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 2 <= 400 L

CS = plt.contour(x1, x2,x2 ,[0.0, 0.1, 3.9, 4.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Add some labels

plt.title('Soft Drink Production Problem') plt.xlabel('Product 1 (100 L)') plt.ylabel('Product 2 (100 L)')

  1. Save the figure as a PNG

plt.savefig('contour.png')

  1. Show the plots

plt.show() (:sourceend:) (:divend:)

Added lines 62-161:

(:source lang=python:)

  1. Import APM Python library

try:

    from APMonitor import *

except:

    # Automatically install APMonitor
    import pip
    pip.main(['install','APMonitor'])
    from APMonitor import *
  1. Select the server

server = 'https://byu.apmonitor.com'

  1. Give the application a name

app = 'production'

  1. Clear any previous applications by that name

apm(server,app,'clear all')

  1. Write the model file

fid = open('softdrink.apm','w') fid.write('Variables \n') fid.write(' x1 > 0 , < 5 ! Product 1 \n') fid.write(' x2 > 0 , < 4 ! Product 2 \n') fid.write(' profit \n') fid.write(' \n') fid.write('Equations \n') fid.write(' ! profit function \n') fid.write(' maximize profit \n') fid.write(' profit = 100 * x1 + 125 * x2 \n') fid.write(' 3 * x1 + 6 * x2 <= 30 \n') fid.write(' 8 * x1 + 4 * x2 <= 44 \n') fid.close() apm_load(server,app,'softdrink.apm')

  1. Solve on APM server

solver_output = apm(server,app,'solve')

  1. Display solver output

print(solver_output)

  1. Retrieve results

sol = apm_sol(server,app)

print ('') print (-- Results of the Optimization Problem --) print ('Product 1 (x1): ' + str(sol['x1'])) print ('Product 2 (x2): ' + str(sol['x2'])) print ('Profit: ' + str(sol['profit']))

  1. Display Results in Web Viewer

url = apm_web_var(server,app)

  1. Generate a contour plot
  1. Import some other libraries that we'll need
  2. matplotlib and numpy packages must also be installed

import matplotlib import numpy as np import matplotlib.pyplot as plt

  1. Design variables at mesh points

x = np.arange(-1.0, 8.0, 0.02) y = np.arange(-1.0, 6.0, 0.02) x1, x2 = np.meshgrid(x,y)

  1. Equations and Constraints

profit = 100.0 * x1 + 125.0 * x2 A_usage = 3.0 * x1 + 6.0 * x2 B_usage = 8.0 * x1 + 4.0 * x2

  1. Create a contour plot

plt.figure()

  1. Weight contours

lines = np.linspace(100.0,800.0,8) CS = plt.contour(x1,x2,profit,lines,colors='g') plt.clabel(CS, inline=1, fontsize=10)

  1. A usage < 30

CS = plt.contour(x1,x2,A_usage,[26.0, 28.0, 30.0],colors='r',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. B usage < 44

CS = plt.contour(x1, x2,B_usage,[40.0,42.0,44.0],colors='b',linewidths=[0.5,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 1 <= 500 L

CS = plt.contour(x1, x2,x1 ,[0.0, 0.1, 4.9, 5.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Container for 0 <= Product 2 <= 400 L

CS = plt.contour(x1, x2,x2 ,[0.0, 0.1, 3.9, 4.0],colors='k',linewidths=[4.0,1.0,1.0,4.0]) plt.clabel(CS, inline=1, fontsize=10)

  1. Add some labels

plt.title('Soft Drink Production Problem') plt.xlabel('Product 1 (100 L)') plt.ylabel('Product 2 (100 L)')

  1. Save the figure as a PNG

plt.savefig('contour.png')

  1. Show the plots

plt.show() (:sourceend:)

January 27, 2014, at 03:59 AM by 23.255.228.67 -
Changed line 20 from:

<iframe width="560" height="315" src="//www.youtube.com/embed/i8WS6HlE8qM?list=UU2GuY-AxnNxIJFAVfEW0QFA" frameborder="0" allowfullscreen></iframe>

to:

<iframe width="560" height="315" src="//www.youtube.com/embed/i8WS6HlE8qM" frameborder="0" allowfullscreen></iframe>

January 27, 2014, at 03:58 AM by 23.255.228.67 -
Added lines 7-8:

Refinery Optimization with Linear Programming

Changed lines 10-11 from:
  • Solve Refinery Optimization Problem with Integer Variables
to:
Added lines 13-20:

(:htmlend:)

Refinery Optimization with Mixed Integer Linear Programming

  • Solve Refinery Optimization Problem with Integer Variables

(:html:) <iframe width="560" height="315" src="//www.youtube.com/embed/i8WS6HlE8qM?list=UU2GuY-AxnNxIJFAVfEW0QFA" frameborder="0" allowfullscreen></iframe>

January 23, 2014, at 03:01 PM by 23.255.228.67 -
Deleted lines 4-5:

Linear Programming Example 1

Changed lines 14-15 from:

Linear Programming Example 2

to:

Soft Drink Production Problem (Example 2)

Deleted lines 28-29:

Deleted lines 35-36:

January 23, 2014, at 03:00 PM by 23.255.228.67 -
Changed lines 5-16 from:

Linear Programming Example

to:

Linear Programming Example 1

A refinery must produce 100 gallons of gasoline and 160 gallons of diesel to meet customer demands. The refinery would like to minimize the cost of crude and two crude options exist. The less expensive crude costs $80 USD per barrel while a more expensive crude costs $95 USD per barrel. Each barrel of the less expensive crude produces 10 gallons of gasoline and 20 gallons of diesel. Each barrel of the more expensive crude produces 15 gallons of both gasoline and diesel. Find the number of barrels of each crude that will minimize the refinery cost while satisfying the customer demands.

  • Solve Refinery Optimization Problem with Continuous Variables
  • Solve Refinery Optimization Problem with Integer Variables

(:html:) <iframe width="560" height="315" src="//www.youtube.com/embed/M_mpRrGKKMo?rel=0" frameborder="0" allowfullscreen></iframe> (:htmlend:)

Linear Programming Example 2