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%20Max-volume%20axis-parallel%20cuboid%20inscribed%20in%20a%20Regular%20Icosahedron%0A%0A%20%20%20%20This%20notebook%20presents%20an%20exercise%20in%20using%20affine%20conic%20constraints%20and%20the%20geometric%20mean%20cone%20(introduced%20as%20a%20standalone%20domain%20in%20v10).%20We%20implement%20the%20maximum%20volume%20cuboid%20example%20discussed%20in%20the%20MOSEK%20modelling%20cookbook%2C%20section%204.3.2.%0A%0A%20%20%20%20In%20the%20current%20example%2C%20we%20seek%20the%20maximal%20volume%2C%20axis-parallel%20cuboid%20inscribed%20within%20a%20%5BRegular%20Icosahedron%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRegular_icosahedron).%20Note%20that%20a%20max-volume%20cuboid%20(not%20necessarily%20axis-parallel)%20will%20lead%20to%20a%20non-convex%20problem.%20That%20will%20not%20be%20the%20scope%20of%20this%20notebook.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%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%20The%20model%20to%20implement%20is%3A%0A%0A%20%20%20%20%5Cbegin%7Balign%7D%0A%20%20%20%20%26%20%5Cmax%20%26%20%26%20t%20%5Cnonumber%20%5C%5C%0A%20%20%20%20%26%20%5Ctextrm%7Bs.t.%7D%20%26%20%26%20t%20%5Cleq%20(x_1%20%5Ccdots%20x_n)%5E%7B1%2Fn%7D%20%5C%5C%0A%20%20%20%20%26%20%26%20%26%20p%20%2B%20E%5Ei%20%5Ccdot%20x%20%3D%20u_1%5Eiv_1%20%2B%20%5Ccdots%20%2B%20u_m%5Eiv_m%20%26%20%5Cforall%20i%3D1%2C%5Ccdots%2C2%5En%20%5C%5C%0A%20%20%20%20%26%20%26%20%26%20%5Csum_%7Bj%3D1%7D%5E%7Bm%7Du_j%5Ei%20%3D%201%20%26%20%5Cforall%20i%3D1%2C%5Ccdots%2C2%5En%20%5C%5C%0A%20%20%20%20%26%20%26%20%26%20x%5Cgeq0%5C%2C%20%2C%20%5C%2C%20u%5Cgeq0%20%5Cnonumber%20%5C%5C%0A%20%20%20%20%5Cend%7Balign%7D%0A%0A%0A%20%20%20%20where%20%24E%5Ei%24%20is%20a%20diagonal%20matrix%20such%20that%20%24%5Ctext%7Bdiag%7D(E%5Ei)%20%3D%20e%5Ei%24%20and%20%24e%5Ei%24%20is%20a%20vector%20of%201's%20and%200's%20enumerating%20the%20%24i%24-th%20vertex%20of%20the%20inscribed%20cuboid%20(%24i%3D1%2C%5Ccdots%2C2%5En%24%20in%20%24%5Cmathbb%7BR%7D%5En%24).%20The%20variable%20vector%20%24x%24%20represents%20the%20edge%20lengths%20of%20the%20cuboid%3B%20%24p%24%20is%20the%20left-most%20vertex%20of%20the%20cuboid.%20Note%20that%20%24%5Ctext%7Bdiag%7D(e%5Ei)%5Ccdot%20x%24%20denotes%20the%20element-wise%20multiplication%20of%20the%20%24e_i%24%20and%20%24x%24%20vectors.%0A%0A%20%20%20%20Given%20the%20vertices%20(%24v_1%2C%20%5Ccdots%2C%20v_m%24)%2C%20the%20icosahedron%20(with%20%24m%3D12%24)%20can%20be%20represented%20by%20the%20convex%20hull%20formed%20using%20its%20vertices.%20This%20is%20implemented%20in%20the%20second%20and%20the%20third%20constraints.%0A%0A%20%20%20%20The%20objective%20value%20is%20the%20%24n%24-th%20root%20of%20the%20volume%20of%20the%20inscribed%20cuboid.%0A%0A%20%20%20%20It%20is%20the%20first%20constraint%20that%20will%20be%20implemented%20using%20the%20primal%20geometric%20mean%20cone.%20%0A%0A%20%20%20%20As%20an%20exercise%2C%20the%20reader%20is%20encouraged%20to%20find%20an%20alternative%20implementation%20using%20the%20power%20cone.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%20Optimizer%20API%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%20The%20mathematical%20statement%20of%20the%20problem%20in%20a%20matrix-focused%20perspective%20is%20as%20follows%3A%20%0A%0A%20%20%20%20%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Cmax%20%26%20%5Cquad%20%5Cquad%20t%20%20%5C%5C%0A%20%20%20%20%5C%5C%0A%20%20%20%20%5Ctextrm%7Bs.t.%7D%20%26%20%5Cquad%20%5Cquad%20%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%20c%20c%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%20%26%5Ccdots%260%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%20%20%20%20%20%20%20%20%20%20%5Cvdots%20%26%5Cddots%26%5Cvdots%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%20%20%20%20%20%20%20%20%20%200%26%5Ccdots%26%201%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20%5Cvdots%20%5C%5C%20x_n%20%5C%5C%20t%5Cend%7Barray%7D%20%5Cright%5D%20%20%5Cin%20%5Cmathbb%7BK%7D%5E%7Bn%2B1%7D_%7B%5Ctext%7BGEO%7D%7D%20%5C%5C%20%0A%20%20%20%20%20%20%20%20%20%20%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%5C%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%26%20%5Cquad%20%5Cquad%20%5Cleft%5B%5Cbegin%7Barray%7D%7B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20e%5Ei_1%20%26%5Ccdots%26%200%20%20%20%20%26%20%5Cvdots%26%201%20%20%20%20%26%5Ccdots%26%200%20%20%20%20%26%26%5Ccdots%26%26-v%5E1_1%26%5Ccdots%20%26-v%5E1_m%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%20%20%20%20%20%20%20%20%20%20%5Cvdots%26%5Cddots%26%5Cvdots%26%200%20%20%20%20%20%26%5Cvdots%26%5Cddots%26%5Cvdots%26%26%5Ccdots%26%26%5Cvdots%26%5Ccdots%26%5Cvdots%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%20%20%20%20%20%20%20%20%20%200%20%26%5Ccdots%20%20%20%20%26%20e%5Ei_n%26%20%5Cvdots%20%26%200%20%20%20%20%26%5Ccdots%26%201%20%20%20%26%26%5Ccdots%26%26-v%5En_1%26%5Ccdots%26-v%5En_m%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%20%20%20%20%20%20%20%20%20%20%26%5Ccdots%26%260%26%26%5Ccdots%26%26%26%5Ccdots%26%261%20%26%20%5Ccdots%20%26%201%20%5Cend%7Barray%7D%5Cright%5D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%20x%5C%5C%20t%20%5C%5C%20p%5C%5C%20%5Cvdots%5C%5C%20u%5Cend%7Barray%7D%20%5Cright%5D%20%2B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D%200%20%5C%5C%5Cvdots%20%5C%5C%20%5Cvdots%20%5C%5C0%20%5C%5C%20-1%5Cend%7Barray%7D%5Cright%5D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%2B1%7D_%7B0%7D%20%26%20%5Cforall%20i%3D1%2C%5Ccdots%2C2%5En%20%5C%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%26%20%5Cquad%20%5Cquad%20%20x%5Cgeq0%5C%2C%20%2C%20%5C%2C%20u%5Cgeq0%20%5Cnonumber%20%5C%5C%0A%20%20%20%20%5Cend%7Balign*%7D%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%23%20Vertices%20of%20a%20Regular%20icosahedron%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20itertools%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20%23%20Vertices%20of%20a%20regular%20icosahedron%20with%20edge%20length%202%0A%20%20%20%20f%20%3D%20(1%2Bnp.sqrt(5))%2F2%0A%20%20%20%20icosahedron%20%3D%20np.array(%5B%5B%200%2C%201%2C%20f%5D%2C%5B%200%2C-1%2C%20f%5D%2C%5B%200%2C%201%2C-f%5D%2C%5B%200%2C-1%2C-f%5D%2C%5B%201%2C%20f%2C%200%5D%2C%5B%201%2C-f%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B-1%2C%20f%2C%200%5D%2C%5B-1%2C-f%2C%200%5D%2C%5B%20f%2C%200%2C%201%5D%2C%5B-f%2C%200%2C%201%5D%2C%5B%20f%2C%200%2C-1%5D%2C%5B-f%2C%200%2C-1%5D%5D)%0A%20%20%20%20print(f%22Volume%20of%20the%20icosahedron%20%3D%20%7B2.18169699*8%7D%22)%0A%20%20%20%20return%20icosahedron%2C%20itertools%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%23%23%20Formulation%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(icosahedron%2C%20itertools%2C%20np%2C%20sys)%3A%0A%20%20%20%20from%20mosek%20import%20Task%2C%20iparam%2C%20optimizertype%2C%20miomode%2C%20soltype%2C%20prosta%2C%20boundkey%2C%20streamtype%2C%20objsense%0A%0A%20%20%20%20inf%20%3D%200.0%20%23%20Symbolic%20constant%20(value%20irrelevant)%0A%0A%20%20%20%20def%20streamprinter(text)%3A%0A%20%20%20%20%20%20%20%20sys.stdout.write(text)%0A%20%20%20%20%20%20%20%20sys.stdout.flush()%0A%0A%20%20%20%20def%20convexHullConstraint(task%2C%20p_v%2C%20c_v%2C%20dom)%3A%0A%20%20%20%20%20%20%20%20m%2C%20n%20%3D%20len(p_v)%2C%20len(p_v%5B0%5D)%0A%20%20%20%20%20%20%20%20nvar%2C%20nafe%20%3D%20task.getnumvar()%2C%20task.getnumafe()%0A%20%20%20%20%20%20%20%20%23%20VARIABLES%3A%20dim(u)%20%3D%20m%0A%20%20%20%20%20%20%20%20task.appendvars(m)%0A%20%20%20%20%20%20%20%20task.putvarboundsliceconst(nvar%2C%20nvar%2Bm%2C%20boundkey.lo%2C%200%2C%20inf)%0A%20%20%20%20%20%20%20%20%23%20Append%20n%2B1%20affine%20expressions%20to%20the%20task.%0A%20%20%20%20%20%20%20%20task.appendafes(n%2B1)%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20task.putafefrow(nafe%20%2B%20i%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bi%2C%20i%2Bn%2B1%5D%20%2B%20list(range(nvar%2C%20nvar%2Bm))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bc_v%5Bi%5D%2C%201.0%5D%20%2B%20list(-p_v%5B%3A%2C%20i%5D))%0A%20%20%20%20%20%20%20%20task.putafefrow(nafe%20%2B%20n%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range(nvar%2C%20nvar%2Bm)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B1.0%5D*m)%0A%20%20%20%20%20%20%20%20task.putafeg(nafe%20%2B%20n%2C%20-1)%0A%20%20%20%20%20%20%20%20%23%20Construct%20the%20ACC%0A%20%20%20%20%20%20%20%20task.appendacc(dom%2C%20range(nafe%2C%20nafe%2Bn%2B1)%2C%20None)%0A%0A%20%20%20%20def%20OPT_MaxVolCuboid(polyhedron)%3A%0A%20%20%20%20%20%20%20%20m%20%3D%20len(polyhedron)%0A%20%20%20%20%20%20%20%20n%20%3D%20len(polyhedron%5B0%5D)%0A%0A%20%20%20%20%20%20%20%20cuboid%20%3D%20list(itertools.product(%5B0%2C1%5D%2C%20repeat%20%3D%20n))%0A%0A%20%20%20%20%23%20MOSEK%20TASK%0A%20%20%20%20%20%20%20%20task%20%3D%20Task()%0A%20%20%20%20%20%20%20%20task.set_Stream(streamtype.log%2C%20streamprinter)%0A%0A%20%20%20%20%23%20VARIABLES%3A%20dim(x)%20%3D%20n%3B%20dim(t)%20%3D%201%3B%20dim(p)%20%3D%20n%0A%20%20%20%20%20%20%20%20task.appendvars(2*n%2B1)%20%0A%20%20%20%20%20%20%20%20task.putvarboundsliceconst(0%2C%20n%2C%20boundkey.lo%2C%200%2C%20inf)%0A%20%20%20%20%20%20%20%20task.putvarboundsliceconst(n%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202*n%2B1%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20boundkey.fr%2C%20-inf%2C%20inf)%0A%0A%20%20%20%20%23%20OBJECTIVE%3A%20maximize%20t%0A%20%20%20%20%20%20%20%20task.putcj(n%2C%201)%0A%20%20%20%20%20%20%20%20task.putobjsense(objsense.maximize)%0A%0A%20%20%20%20%23%20ACCs%0A%20%20%20%20%23%20GEOMETRIC%20MEAN%20CONE%3A%0A%20%20%20%20%20%20%20%20%23%201.%20AFE%0A%20%20%20%20%20%20%20%20task.appendafes(n%2B1)%0A%20%20%20%20%20%20%20%20task.putafefentrylist(range(n%2B1)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range(n%2B1)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B1.0%5D*(n%2B1))%0A%20%20%20%20%20%20%20%20%23%202.%20Domain%0A%20%20%20%20%20%20%20%20geo_cone%20%3D%20task.appendprimalgeomeanconedomain(n%2B1)%0A%20%20%20%20%20%20%20%20%23%203.%20AFE%20%5Cin%20Domain%20--%3E%20ACC%0A%20%20%20%20%20%20%20%20task.appendacc(geo_cone%2C%20range(n%2B1)%2C%20None)%0A%0A%20%20%20%20%23%20CONVEX%20HULL%20CONSTRAINTS%3A%0A%20%20%20%20%20%20%20%20%23%20Re-use%20this%20domain%20instance%20for%20all%20ACCs%20hereafter.%0A%20%20%20%20%20%20%20%20r_zero%20%3D%20task.appendrzerodomain(n%2B1)%0A%0A%20%20%20%20%20%20%20%20%23%20One%20ACC%20for%20each%20vertex%20of%20the%20cuboid%0A%20%20%20%20%20%20%20%20for%20i%2C%20c_v%20in%20enumerate(cuboid)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20convexHullConstraint(task%2C%20polyhedron%2C%20c_v%2C%20r_zero)%0A%0A%20%20%20%20%23%20OPTIMIZE%3A%0A%20%20%20%20%20%20%20%20task.optimize()%0A%20%20%20%20%20%20%20%20task.solutionsummary(streamtype.msg)%0A%0A%20%20%20%20%20%20%20%20solution%20%3D%20task.getxx(soltype.itr)%0A%20%20%20%20%20%20%20%20x%2C%20t%2C%20p%20%3D%20np.array(solution%5B%3An%5D)%2C%20solution%5Bn%5D%2C%20np.array(solution%5Bn%2B1%3A2*n%2B1%5D)%0A%20%20%20%20%20%20%20%20cuboid_vertices%20%3D%20np.array(%5B%20p%20%2B%20e*x%20for%20e%20in%20cuboid%20%5D)%0A%0A%20%20%20%20%20%20%20%20print(f%22%5CnVolume%20of%20the%20inscribed%20cuboid%20%3D%20%7Bt**n%7D%22%20)%0A%20%20%20%20%20%20%20%20return%20cuboid_vertices%0A%0A%20%20%20%20%23%20Solve%20the%20model%20%0A%20%20%20%20cuboid%20%3D%20OPT_MaxVolCuboid(icosahedron)%0A%20%20%20%20return%20(cuboid%2C)%0A%0A%0A%40app.cell%0Adef%20_(cuboid%2C%20icosahedron)%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20mpl_toolkits.mplot3d.art3d%20import%20Poly3DCollection%0A%20%20%20%20from%20mpl_toolkits.mplot3d%20import%20Axes3D%0A%20%20%20%20from%20scipy.spatial%20import%20ConvexHull%0A%0A%20%20%20%20def%20inscribed_cuboid_plot(icosahedron%2C%20cuboid)%3A%0A%20%20%20%20%20%20%20%20fig%20%3D%20plt.figure(figsize%3D(5%2C5))%0A%20%20%20%20%20%20%20%20ax%20%3D%20fig.add_subplot(111%2C%20projection%3D%223d%22)%0A%0A%20%20%20%20%20%20%20%20ico_hull%20%3D%20ConvexHull(icosahedron)%0A%20%20%20%20%20%20%20%20for%20s%20in%20ico_hull.simplices%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tri%20%3D%20Poly3DCollection(%5Bicosahedron%5Bs%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20tri.set_edgecolor('black')%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20tri.set_alpha(0.3)%0A%20%20%20%20%20%20%20%20%20%20%20%20tri.set_facecolor('red')%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.add_collection3d(tri)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.scatter(icosahedron%5B%3A%2C%200%5D%2C%20icosahedron%5B%3A%2C%201%5D%2C%20icosahedron%5B%3A%2C%202%5D%2C%20color%3D'darkred')%0A%0A%20%20%20%20%20%20%20%20cub_hull%20%3D%20ConvexHull(cuboid)%0A%20%20%20%20%20%20%20%20for%20s%20in%20cub_hull.simplices%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tri%20%3D%20Poly3DCollection(%5Bcuboid%5Bs%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23tri.set_edgecolor('black')%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20tri.set_alpha(0.8)%0A%20%20%20%20%20%20%20%20%20%20%20%20tri.set_facecolor('blue')%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.add_collection3d(tri)%0A%0A%20%20%20%20%20%20%20%20ax.set_xlim(-2%2C%202)%0A%20%20%20%20%20%20%20%20ax.set_ylim(-2%2C%202)%0A%20%20%20%20%20%20%20%20ax.set_zlim(-2%2C%202)%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20%23%20Draw%20the%203-D%20interactive%20plot%20using%20cuboid%20vertices%0A%20%20%20%20inscribed_cuboid_plot(icosahedron%2C%20cuboid)%0A%20%20%20%20return%20(inscribed_cuboid_plot%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Fusion%20API%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(icosahedron%2C%20inscribed_cuboid_plot%2C%20itertools%2C%20np%2C%20sys)%3A%0A%20%20%20%20from%20mosek.fusion%20import%20Model%2C%20Domain%2C%20Expr%2C%20ObjectiveSense%2C%20Matrix%2C%20Var%2C%20SolutionStatus%0A%20%20%20%20%23%20Find%20the%20maximum%20volume%20cuboid%20inscribed%20within%20a%20closed%20polyhedron%0A%20%20%20%20%23%20vertices%20(m*n%20shaped%20numpy%20array)%20%3A%20vertices%20of%20a%20convex%20polyhedron%0A%0A%20%20%20%20def%20FUS_MaxVolCuboid(vertices)%3A%0A%20%20%20%20%20%20%20%20m%20%3D%20len(vertices)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Number%20of%20vertices%0A%20%20%20%20%20%20%20%20n%20%3D%20len(vertices%5B0%5D)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20R%5En%0A%20%20%20%20%20%20%20%20cuboid%20%3D%20list(%20itertools.product(%5B0%2C%201%5D%2C%20repeat%3Dn))%20%20%20%20%23%20cuboid%20vertices%20(eg.%20000%2C%20001%2C%20...%2C%20111)%0A%0A%20%20%20%20%20%20%20%20M%20%3D%20Model()%0A%20%20%20%20%20%20%20%20M.setLogHandler(sys.stdout)%0A%0A%20%20%20%20%20%20%20%20x%20%3D%20M.variable('x'%2C%20n%2C%20Domain.greaterThan(0))%0A%20%20%20%20%20%20%20%20p%20%3D%20M.variable('p'%2C%20n)%0A%20%20%20%20%20%20%20%20t%20%3D%20M.variable('t')%0A%0A%20%20%20%20%20%20%20%20%23%20Maximize%3A%20(volume_of_cuboid)**1%2Fn%0A%20%20%20%20%20%20%20%20M.objective(ObjectiveSense.Maximize%2C%20t)%0A%20%20%20%20%20%20%20%20M.constraint('obj'%2C%20Expr.vstack(x%2C%20t)%2C%20Domain.inPGeoMeanCone())%0A%0A%20%20%20%20%20%20%20%20%23%20K%20%3A%20convex%20hull%20from%20vertices%0A%20%20%20%20%20%20%20%20u%20%3D%20M.variable('u'%2C%20%5Bm%2C%202**n%5D%2C%20Domain.greaterThan(0))%0A%20%20%20%20%20%20%20%20M.constraint(Expr.sum(u%2C%200)%2C%20Domain.equalsTo(1.0))%0A%20%20%20%20%20%20%20%20for%20i%2C%20c_v%20in%20enumerate(cuboid)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20i-th%20vertex%20of%20the%20cuboid%20%0A%20%20%20%20%20%20%20%20%20%20%20%20V%20%3D%20Expr.add(p%2C%20Expr.mulElm(list(c_v%5B%3A%5D)%2C%20x))%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20i-th%20vertex%20%5Cin%20K%0A%20%20%20%20%20%20%20%20%20%20%20%20K%20%3D%20Expr.vstack(%5B%20Expr.dot(%20u.slice(%5B0%2Ci%5D%2C%5Bm%2Ci%2B1%5D)%2C%20vertices%5B%3A%2Cj%5D)%20for%20j%20in%20range(n)%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20M.constraint('v_%7B%7D'.format(c_v)%2C%20Expr.sub(V%2C%20K)%2C%20Domain.equalsTo(0))%0A%0A%20%20%20%20%20%20%20%20M.solve()%0A%20%20%20%20%20%20%20%20print(f%22%20%5CnVolume%20of%20cuboid%20%3D%20%7BM.primalObjValue()**n%7D%22)%0A%20%20%20%20%20%20%20%20x_0%20%3D%20x.level()%0A%20%20%20%20%20%20%20%20p_0%20%3D%20p.level()%0A%0A%20%20%20%20%20%20%20%20cuboid_vertices%20%3D%20np.array(%5B%20p_0%20%2B%20e*x_0%20for%20e%20in%20cuboid%20%5D)%0A%20%20%20%20%20%20%20%20return%20cuboid_vertices%0A%0A%20%20%20%20cuboid_%20%3D%20FUS_MaxVolCuboid(icosahedron)%0A%20%20%20%20inscribed_cuboid_plot(icosahedron%2C%20cuboid_)%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
83cc29e6ec05032df049b7d381a00e9a4901330c79d8d1f92965b1d8b1af888c