import%20marimo%0A%0A__generated_with%20%3D%20%220.15.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22!%5BMOSEK%20ApS%5D(https%3A%2F%2Fwww.mosek.com%2Fstatic%2Fimages%2Fbranding%2Fwebgraphmoseklogocolor.png%20)%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20*Fusion%3A*%20Object-Oriented%20API%20for%20Conic%20Optimization%0A%0A%20%20%20%20*Fusion*%20is%20an%20Object-Oriented%20API%20specifically%20designed%20for%20Conic%20Optimization%20with%20**MOSEK**.%20In%20version%209%20of%20**MOSEK**%20*Fusion*%20is%20available%20for%20Python%2C%20C%23%2C%20Java%20and%20C%2B%2B.%0A%0A%20%20%20%20*Fusion*%20makes%20it%20easy%20to%20assemble%20optimization%20models%20from%20conic%20blocks%20without%20going%20through%20the%20nitty-gritty%20of%20converting%20the%20optimization%20problem%20into%20matrix%20form%20-%20*Fusion*%20takes%20care%20of%20that%20part.%20It%20makes%20it%20easy%20to%20add%20and%20remove%20constraints%20and%20experiment%20with%20the%20model%2C%20making%20prototyping%20of%20complex%20models%20very%20quick.%20It%20provides%20%20linear%20expressions%2C%20linear%20algebra%20operations%20and%20cones.%0A%0A%20%20%20%20This%20is%20a%20quick%20demonstration%20of%20the%20main%20capabilities%20of%20*Fusion*.%20More%20details%20may%20be%20found%20in%20the%20documentation%20for%20the%20respective%20APIs.%20In%20particular%20section%206%20of%20each%20Fusion%20API%20manual%20contains%20a%20lot%20more%20modeling%20techniques.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20mosek.fusion%20import%20Model%2C%20Domain%2C%20Expr%2C%20ObjectiveSense%2C%20Matrix%2C%20Var%0A%20%20%20%20import%20mosek.fusion.pythonic%20%20%20%20%20%20%23%20Provides%20operators%20%2B%2C%20-%2C%20%40%2C%20.T%2C%20slicing%20etc.%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20sys%0A%20%20%20%20return%20Domain%2C%20Expr%2C%20Model%2C%20ObjectiveSense%2C%20np%2C%20sys%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Problem%20formulation%20in%20*Fusion*%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20Fusion%20solves%20optimization%20problems%20of%20the%20form%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%7Brll%7D%0A%20%20%20%20%5Ctext%7Bminimize%2Fmaximize%7D%20%20%20%20%26%20c%5ET%20x%20%20%20%20%20%20%20%26%20%5C%5C%0A%20%20%20%20%5Ctext%7Bsubject%20to%7D%20%20%20%20%20%20%20%20%20%20%20%26%20A%5Ei%20x%20%2B%20b%5Ei%20%26%20%5Cin%20%26%20K%5Ei%2C%20%26%20%5Cforall%20i%2C%20%5C%5C%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24K%5Ei%24%20are%20convex%20cones.%20The%20possible%20cones%20%24K%5Ei%24%20are%20%20%0A%0A%20%20%20%20*%20%24%5C%7B0%5C%7D%24%20-%20the%20zero%20cone.%20This%20expresses%20simply%20a%20linear%20equality%20constraint%20%24Ax%2Bb%3D0%24.%0A%20%20%20%20*%20%24%5Cmathbb%7BR%7D_%2B%24%20-%20positive%20orthant.%20This%20expresses%20simply%20a%20linear%20inequality%20constraint%20%24Ax%2Bb%5Cgeq%200%24.%0A%20%20%20%20*%20%24%5Cmathcal%7BQ%7D%24%20-%20quadratic%20cone%2C%20%24x_1%5Cgeq%20%5Csqrt%7Bx_2%5E2%2B%5Ccdots%2Bx_n%5E2%7D%24%20where%20%24n%24%20is%20the%20length%20of%20the%20cone.%0A%20%20%20%20*%20%24%5Cmathcal%7BQ_r%7D%24%20-%20rotated%20quadratic%20cone%2C%20%242x_1x_2%5Cgeq%20x_3%5E2%2B%5Ccdots%2Bx_n%5E2%24%2C%20%24x_1%2Cx_2%5Cgeq%200%24.%0A%20%20%20%20*%20%24K_%5Cmathrm%7Bexp%7D%24%20-%20the%20exponential%20cone%20%24x_1%5Cgeq%20x_2%5Cexp(x_3%2Fx_2)%24%2C%20useful%20in%20particular%20in%20entropy%20ptoblems.%0A%20%20%20%20*%20%24%5Cmathcal%7BP%7D_%5Calpha%24%20-%20the%20three-dimensional%20power%20cone%20%24x_1%5E%5Calpha%20x_2%5E%7B1-%5Calpha%7D%5Cgeq%20%7Cx_3%7C%24%2C%20where%20%240%3C%5Calpha%3C1%24.%0A%20%20%20%20*%20%24%5Cmathbb%7BS%7D_%2B%24%20-%20the%20cone%20of%20positive%20semidefinite%20matrices.%0A%0A%20%20%20%20That%20allows%20for%20expressing%20linear%2C%20conic%20quadratic%20(SOCP)%2C%20semidefinite%2C%20relative%20entropy%2C%20%24p%24-norm%20and%20many%20other%20types%20of%20problems.%0A%0A%20%20%20%20%23%20Linear%20expressions%0A%0A%20%20%20%20Linear%20expressions%20are%20represented%20by%20the%20class%20%60%60Expression%60%60%2C%20of%20which%20%60%60Variable%60%60%20(that%20is%20an%20optimization%20variable%20in%20the%20model)%20is%20a%20special%20case.%20Linear%20expressions%20are%20constructed%20in%20an%20intuitive%20way.%20For%20example%20if%20%24A%2Cb%24%20are%20constant%20data%20matrices%20then%20we%20can%20form%20%24Ax%2Bb%24%20as%20follows%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Model%2C%20np)%3A%0A%20%20%20%20_m%2C%20n%20%3D%20(10%2C%206)%0A%20%20%20%20_A%20%3D%20np.random.uniform(-1.0%2C%201.0%2C%20%5B_m%2C%20n%5D)%0A%20%20%20%20b%20%3D%20np.random.uniform(-1.0%2C%201.0%2C%20%5B_m%5D)%0A%20%20%20%20M%20%3D%20Model('example%20model')%0A%20%20%20%20x%20%3D%20M.variable(n)%0A%20%20%20%20e%20%3D%20_A%20%40%20x%20%2B%20b%0A%20%20%20%20print(e.getShape())%0A%20%20%20%20return%20M%2C%20e%2C%20n%2C%20x%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Conic%20constraints%0A%0A%20%20%20%20We%20can%20now%20solve%20the%20unconstrained%20linear%20regression%20problem%2C%20that%20is%20minimize%20%24%5C%7CAx%2Bb%5C%7C_2%24.%20This%20will%20require%20a%20new%20variable%20%24t%24%20such%20that%20the%20compound%20vector%20%24(t%2CAx%2Bb)%24%20belongs%20to%20the%20quadratic%20cone%20(i.e.%20%24t%5Cgeq%20%5C%7CAx%2Bb%5C%7C_2%24).%20The%20compound%20vector%20is%20created%20as%20a%20stack%20from%20existing%20expressions%20of%20compatible%20shapes.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Domain%2C%20Expr%2C%20M%2C%20ObjectiveSense%2C%20e%2C%20x)%3A%0A%20%20%20%20%23%20Add%20scalar%20variable%20and%20conic%20quadratic%20constraint%20t%20%3E%3D%20%5C%7CAx%2Bb%5C%7C_2%0A%20%20%20%20t%20%3D%20M.variable()%0A%20%20%20%20M.constraint(Expr.vstack(t%2C%20e)%2C%20Domain.inQCone())%0A%0A%20%20%20%20%23%20Let%20t%20be%20the%20objective%20we%20minimize%0A%20%20%20%20M.objective(ObjectiveSense.Minimize%2C%20t)%0A%0A%20%20%20%20%23%20Solve%20and%20print%20solution%0A%20%20%20%20M.solve()%0A%20%20%20%20print(x.level())%0A%20%20%20%20return%20(t%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Suppose%20we%20want%20to%20further%20restrict%20%24x%24%2C%20say%20%24f%5ETx%5Cgeq%201%24%20where%20%24f%24%20is%20a%20given%20vector.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Expr%2C%20M%2C%20n%2C%20np%2C%20x)%3A%0A%20%20%20%20f%20%3D%20np.random.uniform(0.0%2C%201.0%2C%20%5Bn%5D)%0A%0A%20%20%20%20%23%20f%5ET%20dot%20x%20%3E%3D%201%0A%20%20%20%20M.constraint(Expr.dot(f%2Cx)%20%3E%3D%201)%0A%0A%20%20%20%20M.solve()%0A%20%20%20%20print(x.level())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Matrix%20notation%0A%0A%20%20%20%20Now%20suppose%20we%20want%20change%20the%20objective%20to%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7Bminimize%7D%5Cquad%20%5C%7CAx%20%2B%20b%5C%7C_2%20%2B%20%5Csum_%7Bj%3D1%7D%5En%20%5Clambda_i%20e%5E%7Bx_i%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24%5Clambda%24%20are%20positive%20coefficients.%20This%20can%20be%20rewritten%20as%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Barray%7D%0A%20%20%20%20%7Blll%7D%0A%20%20%20%20%5Ctext%7Bminimize%7D%20%20%20%20%26%20t%20%2B%20%5Clambda%5ET%20w%20%20%20%26%20%5C%5C%0A%20%20%20%20%5Ctext%7Bsubject%20to%7D%20%20%26%20(t%2C%20Ax%2Bb)%20%5Cin%20%5Cmathcal%7BQ%7D%2C%20%26%20%5C%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%26%20(w_i%2C%201%2C%20x_i)%20%5Cin%20K_%5Cmathrm%7Bexp%7D.%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20(Indeed%2C%20the%20last%20set%20of%20constraints%20is%20just%20%24w_i%5Cgeq%20e%5E%7Bx_i%7D%24).%20We%20only%20need%20to%20define%20the%20last%20set%20of%20constraints%20and%20this%20can%20be%20achieved%20in%20one%20go%20by%20stacking%20them%20in%20a%20matrix%20as%20follows%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Domain%2C%20Expr%2C%20M%2C%20n%2C%20x)%3A%0A%20%20%20%20w%20%3D%20M.variable(n)%0A%0A%20%20%20%20M.constraint(Expr.hstack(w%2C%20Expr.constTerm(n%2C%201.0)%2C%20x)%2C%20Domain.inPExpCone())%3B%0A%20%20%20%20return%20(w%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20The%20horizontal%20stack%20above%20creates%20an%20expression%20of%20shape%20%24n%20%5Ctimes%203%24%20which%20looks%20as%20follows%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bcc%7D%0A%20%20%20%20w_1%20%26%201%20%26%20x_1%20%5C%5C%0A%20%20%20%20w_2%20%26%201%20%26%20x_2%20%5C%5C%0A%20%20%20%20.%20%26%20.%20%26%20.%20%20%5C%5C%0A%20%20%20%20w_n%20%26%201%20%26%20x_n%20%5C%5C%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20and%20the%20conic%20constraint%20is%20just%20a%20short%20way%20of%20writing%20that%20*every%20row%20of%20that%20matrix%20belongs%20to%20the%20exponential%20cone*%2C%20which%20is%20exactly%20what%20we%20need.%0A%0A%20%20%20%20We%20can%20now%20solve%20it%2C%20this%20time%20with%20log%20on%20screen.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(M%2C%20ObjectiveSense%2C%20n%2C%20np%2C%20sys%2C%20t%2C%20w%2C%20x)%3A%0A%20%20%20%20lamb%20%3D%20np.random.uniform(0.0%2C%201.0%2C%20%5Bn%5D)%0A%0A%20%20%20%20M.setLogHandler(sys.stdout)%0A%0A%20%20%20%20%23%20Objective%20%3D%20t%20%2B%20lambda%20dot%20w%0A%20%20%20%20M.objective(ObjectiveSense.Minimize%2C%20t%20%2B%20w.T%20%40%20lamb)%0A%20%20%20%20M.solve()%0A%20%20%20%20print(x.level())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Semidefinite%20variables%0A%0A%20%20%20%20An%20%24n%5Ctimes%20n%24%20semidefinite%20variable%20can%20be%20defined%20with%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Domain%2C%20Model)%3A%0A%20%20%20%20n_1%20%3D%205%0A%20%20%20%20M_1%20%3D%20Model('semidefinite%20model')%0A%20%20%20%20X%20%3D%20M_1.variable(Domain.inPSDCone(n_1))%0A%20%20%20%20print(X.getShape())%0A%20%20%20%20return%20M_1%2C%20X%2C%20n_1%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22and%20it%20can%20be%20used%20like%20any%20variable%20of%20this%20shape.%20For%20example%20we%20can%20solve%20the%20very%20simple%20illustrative%20problem%20of%20maximizing%20the%20sum%20of%20elements%20of%20%24X%24%20subject%20to%20a%20fixed%20diagonal.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Expr%2C%20M_1%2C%20ObjectiveSense%2C%20X%2C%20n_1%2C%20np)%3A%0A%20%20%20%20diag%20%3D%20np.random.uniform(1.0%2C%202.0%2C%20n_1)%0A%20%20%20%20print(diag%2C%20'%5Cn')%0A%20%20%20%20M_1.constraint(X.diag()%20%3D%3D%20diag)%0A%20%20%20%20M_1.objective(ObjectiveSense.Maximize%2C%20Expr.sum(X))%0A%20%20%20%20M_1.solve()%0A%20%20%20%20print(np.reshape(X.level()%2C%20%5B5%2C%205%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20A%20cheatsheet%20to%20building%20expressions%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Domain%2C%20Expr%2C%20Model%2C%20np)%3A%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20%23%20Problem%20dimensions%20and%20data%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20n_2%20%3D%206%0A%20%20%20%20_m%20%3D%2010%0A%20%20%20%20a%20%3D%20np.random.uniform(0.0%2C%201.0%2C%20n_2)%20%20%20%20%20%20%20%20%20%23%20Vector%20a%0A%20%20%20%20_A%20%3D%20np.random.uniform(0.0%2C%201.0%2C%20(_m%2C%20n_2))%20%20%23%20Matrix%20A%0A%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20%23%20Define%20the%20model%20and%20variables%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20M_2%20%3D%20Model('demo_model')%0A%0A%20%20%20%20x_1%20%3D%20M_2.variable('x'%2C%20n_2%2C%20Domain.unbounded())%0A%20%20%20%20y%20%20%20%3D%20M_2.variable('y'%2C%20n_2%2C%20Domain.greaterThan(0.0))%0A%20%20%20%20z%20%20%20%3D%20M_2.variable('z'%2C%20n_2%2C%20Domain.inRange(-1.0%2C%201.0))%0A%20%20%20%20X_1%20%3D%20M_2.variable('X'%2C%20%5Bn_2%2C%20n_2%5D)%0A%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20%23%20Simple%20expressions%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20e0%20%3D%20x_1%20%2B%201.0%0A%20%20%20%20e1%20%3D%20x_1%20%2B%20y%0A%20%20%20%20e2%20%3D%20a%20%2B%20y%0A%20%20%20%20e3%20%3D%20x_1%20-%20y%0A%20%20%20%20e4%20%3D%20x_1%20%2B%20y%20%2B%20z%0A%20%20%20%20e5%20%3D%20Expr.add(%5Bx_1%2C%20y%2C%20z%5D)%0A%20%20%20%20e6%20%3D%207.0%20*%20x_1%0A%20%20%20%20e7%20%3D%20Expr.mulElm(a%2C%20x_1)%0A%20%20%20%20e8%20%3D%20Expr.dot(a%2C%20x_1)%20%20%20%20%20%20%20%20%20%20%20%23%20scalar%0A%20%20%20%20print(%22Shape%20of%20e8%3A%22%2C%20e8.getShape())%0A%20%20%20%20e9%20%3D%20Expr.outer(a%2C%20x_1)%20%20%20%20%20%20%20%20%20%23%20matrix%0A%20%20%20%20print(%22Shape%20of%20e9%3A%22%2C%20e9.getShape())%0A%20%20%20%20e10%20%3D%20Expr.sum(x_1)%0A%20%20%20%20e11%20%3D%20_A%20%40%20X_1%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20matrix%20multiplication%0A%20%20%20%20print(%22Shape%20of%20e11%3A%22%2C%20e11.getShape())%0A%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20%23%20Explicit%20expressions%20using%20Expr%20methods%0A%20%20%20%20%23%20-----------------------------%0A%20%20%20%20e0_expl%20%20%3D%20Expr.add(x_1%2C%201.0)%0A%20%20%20%20e1_expl%20%20%3D%20Expr.add(x_1%2C%20y)%0A%20%20%20%20e2_expl%20%20%3D%20Expr.add(a%2C%20y)%0A%20%20%20%20e3_expl%20%20%3D%20Expr.sub(x_1%2C%20y)%0A%20%20%20%20e4_expl%20%20%3D%20Expr.add(Expr.add(x_1%2C%20y)%2C%20z)%0A%20%20%20%20e6_expl%20%20%3D%20Expr.mul(7.0%2C%20x_1)%0A%20%20%20%20e11_expl%20%3D%20Expr.mul(_A%2C%20X_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%3Ca%20rel%3D%22license%22%20href%3D%22http%3A%2F%2Fcreativecommons.org%2Flicenses%2Fby%2F4.0%2F%22%3E%3Cimg%20alt%3D%22Creative%20Commons%20License%22%20style%3D%22border-width%3A0%22%20src%3D%22https%3A%2F%2Fi.creativecommons.org%2Fl%2Fby%2F4.0%2F80x15.png%22%20%2F%3E%3C%2Fa%3E%3Cbr%20%2F%3EThis%20work%20is%20licensed%20under%20a%20%3Ca%20rel%3D%22license%22%20href%3D%22http%3A%2F%2Fcreativecommons.org%2Flicenses%2Fby%2F4.0%2F%22%3ECreative%20Commons%20Attribution%204.0%20International%20License%3C%2Fa%3E.%20The%20**MOSEK**%20logo%20and%20name%20are%20trademarks%20of%20%3Ca%20href%3D%22http%3A%2F%2Fmosek.com%22%3EMosek%20ApS%3C%2Fa%3E.%20The%20code%20is%20provided%20as-is.%20Compatibility%20with%20future%20release%20of%20**MOSEK**%20or%20the%20%60Fusion%20API%60%20are%20not%20guaranteed.%20For%20more%20information%20contact%20our%20%5Bsupport%5D(mailto%3Asupport%40mosek.com).%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
06bf3b95540f3c6070a580b8c52b85a0f357c9b21de443463ffde593f6c34865