Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168693
Image: ubuntu2004
html("<h2>Simple linear programming example using cvxopt</h2>") html("Minimize -4*x1-5*x2") html("Subject to: 2*x1+x2≤ 3") html("x1+2*x2≤ 3") html("x1 ≥ 0, x2 ≥ 0") html("<p></p>") RealNumber=float Integer=int from cvxopt.base import matrix as m from cvxopt import solvers c = m([-4.0, -5.0]) G = m([[2.0, 1., -1.0, 0.0], [1.0, 2.0, 0.0, -1.0]]) h = m([3.0, 3.0, 0.0, 0.0]) sol = solvers.lp(c,G,h) sol

Simple linear programming example using cvxopt

Minimize -4*x1-5*x2
Subject to: 2*x1+x2≤ 3
x1+2*x2≤ 3
x1 ≥ 0, x2 ≥ 0

pcost dcost gap pres dres k/t 0: -8.1000e+00 -1.8300e+01 4e+00 0e+00 8e-01 1e+00 1: -8.8055e+00 -9.4357e+00 2e-01 1e-16 4e-02 3e-02 2: -8.9981e+00 -9.0049e+00 2e-03 1e-16 5e-04 4e-04 3: -9.0000e+00 -9.0000e+00 2e-05 3e-16 5e-06 4e-06 4: -9.0000e+00 -9.0000e+00 2e-07 1e-16 5e-08 4e-08 {'status': 'optimal', 'x': <2x1 matrix, tc='d'>, 's': <4x1 matrix, tc='d'>, 'z': <4x1 matrix, tc='d'>, 'y': <0x1 matrix, tc='d'>}
html('<h2>Inactive constraint interact example</h2>') html("Maximize 3*x1+2*x2") html("Subject to: 2*x1+x2≤ 100") html("x1+x2≤ 80") html("x1≤ x0") html("x1 ≥ 0, x2 ≥ 0") html("<p></p>") @interact def region(x0 = slider(20,60,1)): x1,x2,t = var("x1,x2,t") line1 = plot(100-2*x1, 0, 50) line2 = plot(80-x1, 0, 80) line3 = parametric_plot((x0,t), (t, 0, 90)) reg = region_plot([2*x1+x2<100,x1+x2<80,x1<x0],(x1,0,60),(x2,0,100), plot_points=400, incol='gray') show(line1+line2+line3+reg)
html('<h2>Inactive constraint interact example</h2>') html("Maximize 3*x1+2*x2") html("Subject to: 2*x1+x2≤ 100") html("x1+x2≤ 80") html("x1≤ x0") html("x1 ≥ 0, x2 ≥ 0") html("<p></p>") @interact def region(x0 = slider(20,60,1)): x1,x2,t = var("x1,x2,t") line1 = plot(100-2*x1, 0, 50) line2 = plot(80-x1, 0, 80) line3 = parametric_plot((x0,t), (t, 0, 90)) reg1 = region_plot([2*x1+x2<100,x1+x2<80],(x1,0,20),(x2,0,100), plot_points=400, incol='gray') reg2 = region_plot([2*x1+x2<100,x1+x2<80,x1<x0],(x1,20,60),(x2,0,100), plot_points=400, incol='lightblue') show(line1+line2+line3+reg1+reg2)