├── README.md ├── tmp.gif ├── simp_plt.jl ├── play.jl ├── .ipynb_checkpoints ├── Untitled-checkpoint.ipynb ├── Untitled1-checkpoint.ipynb ├── Untitled2-checkpoint.ipynb ├── Untitled3-checkpoint.ipynb ├── Untitled4-checkpoint.ipynb ├── homotop-checkpoint.ipynb ├── kissing_sos-checkpoint.ipynb └── SOS Diff Eq-checkpoint.ipynb ├── graph_test.jl ├── manipulate.jl ├── julia_plot.jl ├── mip_filter.jl ├── homopoly.jl ├── tsp.jl ├── Untitled1.ipynb ├── Untitled2.ipynb ├── kissing_sos.ipynb ├── Untitled3.ipynb ├── Untitled4.ipynb ├── homotop.ipynb └── SOS Diff Eq.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # julia_trash 2 | -------------------------------------------------------------------------------- /tmp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philzook58/julia_trash/master/tmp.gif -------------------------------------------------------------------------------- /simp_plt.jl: -------------------------------------------------------------------------------- 1 | using JuMP 2 | 3 | pyplot() # Choose a backend 4 | plot(rand(4,4)) 5 | gui() 6 | 7 | -------------------------------------------------------------------------------- /play.jl: -------------------------------------------------------------------------------- 1 | #using ForwardDiff 2 | 3 | print("HI") 4 | 5 | function main() 6 | print("Uo") 7 | end 8 | 9 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/homotop-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/kissing_sos-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /graph_test.jl: -------------------------------------------------------------------------------- 1 | using Graphs 2 | g = simple_graph(3) 3 | 4 | add_edge!(g, 1, 2) 5 | add_edge!(g, 3, 2) 6 | add_edge!(g, 3, 1) 7 | Graphs.plot(g) 8 | -------------------------------------------------------------------------------- /manipulate.jl: -------------------------------------------------------------------------------- 1 | using Interact 2 | using Gadfly 3 | 4 | @manipulate for ϕ = 0:π/16:4π, f = [sin, cos], both = false 5 | if both 6 | plot([θ -> sin(θ + ϕ), θ -> cos(θ + ϕ)], 0, 8) 7 | else 8 | plot(θ -> f(θ + ϕ), 0, 8) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /julia_plot.jl: -------------------------------------------------------------------------------- 1 | print("hello") 2 | a = 1+2 3 | print(3a) 4 | 5 | #α = λ x = 6 | 7 | map(x -> x^2 + 2x - 1, [1,3,-1]) 8 | 9 | #Pkg.add("PyPlot") 10 | using PyPlot 11 | x = linspace(0,2pi,1000); y = sin(3x + 4cos(2x)) 12 | plot(x, y, color="red", linewidth=2.0, linestyle="--") 13 | -------------------------------------------------------------------------------- /mip_filter.jl: -------------------------------------------------------------------------------- 1 | using JuMP 2 | #using Clp 3 | using GLPK 4 | 5 | #m = Model(solver = ClpSolver()) 6 | model = Model(with_optimizer(GLPK.Optimizer)) 7 | @variable(model, 0 <= x[1:20] <= 1) 8 | @variable(model, 0 <= t[1:20] <= 1/127) 9 | @variable(model, 0 <= z[1:20] <= 127, Int) 10 | @constraint(model, x - z/127 .<= t) 11 | @constraint(model, -t .<= x - z/127) 12 | -------------------------------------------------------------------------------- /homopoly.jl: -------------------------------------------------------------------------------- 1 | module HomoPoly 2 | 3 | using HomotopyContinuation 4 | 5 | myfun(x) = 3x 6 | 7 | # declare variables x and y 8 | @polyvar x y 9 | 10 | # define the polynomials 11 | function testp() 12 | f₁ = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y 13 | f₂ = x^2+2x*y^2 - 2y^2 - 1/2 14 | result = solve([f₁, f₂]) 15 | print(result) 16 | end 17 | 18 | 19 | end -------------------------------------------------------------------------------- /tsp.jl: -------------------------------------------------------------------------------- 1 | 2 | using JuMP 3 | using Clp 4 | println("Hey friend") 5 | 6 | m = Model(solver = ClpSolver()) 7 | 8 | 9 | @variable(m, 0 <= x <= 2 ) 10 | @variable(m, 0 <= y <= 30 ) 11 | 12 | @objective(m, Min, x+ y ) 13 | @constraint(m, x + y >= 1 ) 14 | 15 | print(m) 16 | 17 | status = solve(m) 18 | 19 | println("Objective value: ", getobjectivevalue(m)) 20 | println("x = ", getvalue(x)) 21 | println("y = ", getvalue(y)) -------------------------------------------------------------------------------- /Untitled1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "hello" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print(\"hello\")" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | } 27 | ], 28 | "metadata": { 29 | "kernelspec": { 30 | "display_name": "Julia 0.6.2", 31 | "language": "julia", 32 | "name": "julia-0.6" 33 | }, 34 | "language_info": { 35 | "file_extension": ".jl", 36 | "mimetype": "application/julia", 37 | "name": "julia", 38 | "version": "0.6.2" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 2 43 | } 44 | -------------------------------------------------------------------------------- /Untitled2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "┌ Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572]\n", 13 | "└ @ Base loading.jl:1186\n", 14 | "ERROR: LoadError: LoadError: syntax: invalid \"::\" syntax\n", 15 | "Stacktrace:\n", 16 | " [1] include at ./boot.jl:317 [inlined]\n", 17 | " [2] include_relative(::Module, ::String) at ./loading.jl:1038\n", 18 | " [3] include at ./sysimg.jl:29 [inlined]\n", 19 | " [4] include(::String) at /Users/philip/.julia/packages/JuMP/6aWuF/src/JuMP.jl:13\n", 20 | " [5] top-level scope at none:0\n", 21 | " [6] include at ./boot.jl:317 [inlined]\n", 22 | " [7] include_relative(::Module, ::String) at ./loading.jl:1038\n", 23 | " [8] include(::Module, ::String) at ./sysimg.jl:29\n", 24 | " [9] top-level scope at none:2\n", 25 | " [10] eval at ./boot.jl:319 [inlined]\n", 26 | " [11] eval(::Expr) at ./client.jl:389\n", 27 | " [12] top-level scope at ./none:3\n", 28 | "in expression starting at /Users/philip/.julia/packages/JuMP/6aWuF/src/JuMPContainer.jl:18\n", 29 | "in expression starting at /Users/philip/.julia/packages/JuMP/6aWuF/src/JuMP.jl:55\n" 30 | ] 31 | }, 32 | { 33 | "ename": "ErrorException", 34 | "evalue": "Failed to precompile JuMP [4076af6c-e467-56ae-b986-b466b2749572] to /Users/philip/.julia/compiled/v1.0/JuMP/DmXqY.ji.", 35 | "output_type": "error", 36 | "traceback": [ 37 | "Failed to precompile JuMP [4076af6c-e467-56ae-b986-b466b2749572] to /Users/philip/.julia/compiled/v1.0/JuMP/DmXqY.ji.", 38 | "", 39 | "Stacktrace:", 40 | " [1] error(::String) at ./error.jl:33", 41 | " [2] macro expansion at ./logging.jl:313 [inlined]", 42 | " [3] compilecache(::Base.PkgId, ::String) at ./loading.jl:1184", 43 | " [4] macro expansion at ./logging.jl:311 [inlined]", 44 | " [5] _require(::Base.PkgId) at ./loading.jl:941", 45 | " [6] require(::Base.PkgId) at ./loading.jl:852", 46 | " [7] macro expansion at ./logging.jl:311 [inlined]", 47 | " [8] require(::Module, ::Symbol) at ./loading.jl:834", 48 | " [9] top-level scope at In[1]:1" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "import JuMP" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Julia 1.0.0", 67 | "language": "julia", 68 | "name": "julia-1.0" 69 | }, 70 | "language_info": { 71 | "file_extension": ".jl", 72 | "mimetype": "application/julia", 73 | "name": "julia", 74 | "version": "1.0.0" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /kissing_sos.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using JuMP\n", 10 | "using SumOfSquares\n", 11 | "using DynamicPolynomials\n", 12 | "using SCS" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "┌ Warning: The addition operator has been used on JuMP expressions a large number of times. This warning is safe to ignore but may indicate that model generation is slower than necessary. For performance reasons, you should not add expressions in a loop. Instead of x += y, use add_to_expression!(x,y) to modify x in place. If y is a single variable, you may also use add_to_expression!(x, coef, y) for x += coef*y.\n", 25 | "└ @ JuMP /Users/philip/.julia/packages/JuMP/MsUSY/src/JuMP.jl:747\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "using JuMP\n", 31 | "using SumOfSquares\n", 32 | "using DynamicPolynomials\n", 33 | "using SCS\n", 34 | "N = 10\n", 35 | "d = 2\n", 36 | "@polyvar x[1:N,1:d]\n", 37 | "X = monomials(reshape(x,d*N), 0:2)\n", 38 | "X1 = monomials(reshape(x,d*N), 0:4)\n", 39 | "\n", 40 | "model = SOSModel(with_optimizer(SCS.Optimizer))\n", 41 | "\n", 42 | "acc = nothing\n", 43 | "for t in sum(x .* x, dims=2)\n", 44 | " #print(t)\n", 45 | " p = @variable(model, [1:1], Poly(X1))\n", 46 | " #print(p)\n", 47 | " if acc != nothing\n", 48 | " acc += p * (t - 1)\n", 49 | " else\n", 50 | " acc = p * (t - 1)\n", 51 | " end\n", 52 | "end\n", 53 | "\n", 54 | "for i in range(1,stop=N)\n", 55 | " for j in range(1,stop=i-1)\n", 56 | " d = x[i,:] - x[j,:]\n", 57 | " p = @variable(model, [1:1], SOSPoly(X))\n", 58 | " acc += p * (sum(d .* d) - 1)\n", 59 | " end\n", 60 | "end\n", 61 | "\n", 62 | "#print(acc)\n", 63 | "print(typeof(acc))\n", 64 | "@constraint(model, acc[1] == -1 )\n", 65 | "optimize!(model)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 39, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "1-element Array{UnitRange{Int64},1}:\n", 77 | " 1:1" 78 | ] 79 | }, 80 | "execution_count": 39, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [] 93 | } 94 | ], 95 | "metadata": { 96 | "@webio": { 97 | "lastCommId": null, 98 | "lastKernelId": null 99 | }, 100 | "kernelspec": { 101 | "display_name": "Julia 1.2.0", 102 | "language": "julia", 103 | "name": "julia-1.2" 104 | }, 105 | "language_info": { 106 | "file_extension": ".jl", 107 | "mimetype": "application/julia", 108 | "name": "julia", 109 | "version": "1.2.0" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 2 114 | } 115 | -------------------------------------------------------------------------------- /Untitled3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "┌ Info: Precompiling Polyhedra [67491407-f73d-577b-9b50-8179a7c68029]\n", 13 | "└ @ Base loading.jl:1187\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "using Polyhedra\n", 19 | "using JuMP" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stderr", 29 | "output_type": "stream", 30 | "text": [ 31 | "┌ Info: Precompiling PiecewiseLinearOpt [0f51c51e-adfa-5141-8a04-d40246b8977c]\n", 32 | "└ @ Base loading.jl:1187\n", 33 | "┌ Warning: Deprecated syntax `type` at /Users/philip/.julia/packages/PiecewiseLinearOpt/65zl7/src/types.jl:1.\n", 34 | "│ Use `mutable struct` instead.\n", 35 | "└ @ ~/.julia/packages/PiecewiseLinearOpt/65zl7/src/types.jl:1\n", 36 | "┌ Warning: Deprecated syntax `parametric method syntax PWLFunction{D}(x::Vector{NTuple{D}}, z::Vector, T::Vector{Vector}, meta::Dict)` around /Users/philip/.julia/packages/PiecewiseLinearOpt/65zl7/src/types.jl:8.\n", 37 | "│ Use `PWLFunction(x::Vector{NTuple{D}}, z::Vector, T::Vector{Vector}, meta::Dict) where D` instead.\n", 38 | "└ @ ~/.julia/packages/PiecewiseLinearOpt/65zl7/src/types.jl:8\n", 39 | "┌ Warning: Deprecated syntax `type` at /Users/philip/.julia/packages/PiecewiseLinearOpt/65zl7/src/jump.jl:4.\n", 40 | "│ Use `mutable struct` instead.\n", 41 | "└ @ ~/.julia/packages/PiecewiseLinearOpt/65zl7/src/jump.jl:4\n", 42 | "┌ Warning: Deprecated syntax `parametric method syntax compute_hyperplanes{T}(C::Vector{Vector{T}})` around /Users/philip/.julia/packages/PiecewiseLinearOpt/65zl7/src/jump.jl:339.\n", 43 | "│ Use `compute_hyperplanes(C::Vector{Vector{T}}) where T` instead.\n", 44 | "└ @ ~/.julia/packages/PiecewiseLinearOpt/65zl7/src/jump.jl:339\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "using PiecewiseLinearOpt" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stderr", 59 | "output_type": "stream", 60 | "text": [ 61 | "┌ Warning: Deprecated syntax `implicit assignment to global variable `#1###368``.\n", 62 | "│ Use `global #1###368` instead.\n", 63 | "└ @ nothing none:0\n" 64 | ] 65 | }, 66 | { 67 | "data": { 68 | "text/latex": [ 69 | "$$ x $$" 70 | ], 71 | "text/plain": [ 72 | "x" 73 | ] 74 | }, 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "m = Model()\n", 82 | "@variable(m, x)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "z = piecewiselinear(m, x, -pi:pi, fd)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 5, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "π = 3.1415926535897..." 103 | ] 104 | }, 105 | "execution_count": 5, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "pi" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "Pkg.add(\"ConditionalJuMP\")" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "using Cbc" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Julia 0.7.0", 143 | "language": "julia", 144 | "name": "julia-0.7" 145 | }, 146 | "language_info": { 147 | "file_extension": ".jl", 148 | "mimetype": "application/julia", 149 | "name": "julia", 150 | "version": "0.7.0" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 2 155 | } 156 | -------------------------------------------------------------------------------- /Untitled4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "┌ Info: Precompiling ConditionalJuMP [ae04f764-fc8b-5ee0-af1c-aa760b5c9409]\n", 13 | "└ @ Base loading.jl:1187\n", 14 | "WARNING: Method definition setrounding(Type{T<:Union{Float32, Float64}}, Base.Rounding.RoundingMode{T} where T) where {T<:Union{Float32, Float64}} in module Base at deprecated.jl:1699 overwritten in module SetRounding at /Users/philip/.julia/packages/SetRounding/MjtxK/src/SetRounding.jl:47.\n", 15 | "WARNING: Method definition setrounding(Type{T<:Union{Float32, Float64}}, Base.Rounding.RoundingMode{T} where T) where {T<:Union{Float32, Float64}} in module Base at deprecated.jl:1699 overwritten in module SetRounding at /Users/philip/.julia/packages/SetRounding/MjtxK/src/SetRounding.jl:47.\n", 16 | "WARNING: could not import Base.showfull into IntervalArithmetic\n", 17 | "WARNING: Method definition setrounding(Type{T<:Union{Float32, Float64}}, Base.Rounding.RoundingMode{T} where T) where {T<:Union{Float32, Float64}} in module Base at deprecated.jl:1699 overwritten in module SetRounding at /Users/philip/.julia/packages/SetRounding/MjtxK/src/SetRounding.jl:47.\n", 18 | "┌ Warning: Deprecated syntax ``?` used as an identifier` at /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/macros.jl:30.\n", 19 | "└ @ ~/.julia/packages/ConditionalJuMP/durM1/src/macros.jl:30\n", 20 | "WARNING: importing deprecated binding Base.@sprintf into ConditionalJuMP.\n", 21 | "WARNING: Base.@sprintf is deprecated: it has been moved to the standard library package `Printf`.\n", 22 | "Add `using Printf` to your imports.\n", 23 | " likely near /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/ConditionalJuMP.jl:277\n", 24 | "WARNING: Base.@sprintf is deprecated: it has been moved to the standard library package `Printf`.\n", 25 | "Add `using Printf` to your imports.\n", 26 | " likely near /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/ConditionalJuMP.jl:284\n", 27 | "WARNING: Base.@sprintf is deprecated: it has been moved to the standard library package `Printf`.\n", 28 | "Add `using Printf` to your imports.\n", 29 | " likely near /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/ConditionalJuMP.jl:293\n", 30 | "WARNING: importing deprecated binding Base.Void into ConditionalJuMP.\n", 31 | "WARNING: Base.Void is deprecated, use Nothing instead.\n", 32 | " likely near /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/ConditionalJuMP.jl:417\n", 33 | "ERROR: LoadError: cannot add methods to a builtin function\n", 34 | "Stacktrace:\n", 35 | " [1] top-level scope at none:0\n", 36 | " [2] include at ./boot.jl:317 [inlined]\n", 37 | " [3] include_relative(::Module, ::String) at ./loading.jl:1038\n", 38 | " [4] include(::Module, ::String) at ./sysimg.jl:29\n", 39 | " [5] top-level scope at none:2\n", 40 | " [6] eval at ./boot.jl:319 [inlined]\n", 41 | " [7] eval(::Expr) at ./client.jl:399\n", 42 | " [8] top-level scope at ./none:3\n", 43 | "in expression starting at /Users/philip/.julia/packages/ConditionalJuMP/durM1/src/ConditionalJuMP.jl:525\n" 44 | ] 45 | }, 46 | { 47 | "ename": "ErrorException", 48 | "evalue": "Failed to precompile ConditionalJuMP [ae04f764-fc8b-5ee0-af1c-aa760b5c9409] to /Users/philip/.julia/compiled/v0.7/ConditionalJuMP/vKvUe.ji.", 49 | "output_type": "error", 50 | "traceback": [ 51 | "Failed to precompile ConditionalJuMP [ae04f764-fc8b-5ee0-af1c-aa760b5c9409] to /Users/philip/.julia/compiled/v0.7/ConditionalJuMP/vKvUe.ji.", 52 | "", 53 | "Stacktrace:", 54 | " [1] error(::String) at ./error.jl:33", 55 | " [2] macro expansion at ./logging.jl:313 [inlined]", 56 | " [3] compilecache(::Base.PkgId, ::String) at ./loading.jl:1185", 57 | " [4] macro expansion at ./logging.jl:311 [inlined]", 58 | " [5] _require(::Base.PkgId) at ./loading.jl:941", 59 | " [6] require(::Base.PkgId) at ./loading.jl:852", 60 | " [7] macro expansion at ./logging.jl:311 [inlined]", 61 | " [8] require(::Module, ::Symbol) at ./loading.jl:834", 62 | " [9] top-level scope at In[3]:4" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "using JuMP\n", 68 | "using Cbc\n", 69 | "using PiecewiseLinearOpt\n", 70 | "using ConditionalJuMP" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "m = Model(CbcSolver())" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "N = 10\n", 89 | "@variable(m, -1 <= x[1:N] <= 10)\n", 90 | "@variable(m, -1 <= v[1:N] <= 10)\n", 91 | "#@variable(m, -1 belowfloor[1:N], Bin)\n", 92 | "g = 1.0\n", 93 | "T = 1.0\n", 94 | "dt = T/N\n", 95 | "for t in 1:N\n", 96 | " @constraint(m, x[t+1] == x[t] + dt*v[t])\n", 97 | " @constraint(m, v[t+1] == @switch(\n", 98 | " (x[t] + dt*(v[t] - g*dt) <= 0) => v[t] - g*dt,\n", 99 | " (x[t] + dt*(v[t] - g*dt) >= 0 => -v[t] - g*dt)))\n", 100 | "\n", 101 | "end\n", 102 | "\n" 103 | ] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Julia 0.7.0", 109 | "language": "julia", 110 | "name": "julia-0.7" 111 | }, 112 | "language_info": { 113 | "file_extension": ".jl", 114 | "mimetype": "application/julia", 115 | "name": "julia", 116 | "version": "0.7.0" 117 | } 118 | }, 119 | "nbformat": 4, 120 | "nbformat_minor": 2 121 | } 122 | -------------------------------------------------------------------------------- /homotop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 19, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using HomotopyContinuation\n", 10 | "using LinearAlgebra" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 13, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "ename": "UndefVarError", 20 | "evalue": "UndefVarError: I not defined", 21 | "output_type": "error", 22 | "traceback": [ 23 | "UndefVarError: I not defined", 24 | "", 25 | "Stacktrace:", 26 | " [1] top-level scope at In[13]:12" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "x = 10\n", 32 | "x + 1\n", 33 | "f(x) = 2x\n", 34 | "f(3)\n", 35 | "δ = 3 # tabs after latex\n", 36 | "typeof(\"My string\")\n", 37 | "function myfun(x,y)\n", 38 | " x + y\n", 39 | "end\n", 40 | "zeros(3)\n", 41 | "ones(2)\n", 42 | "methods(Bool" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 10, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "4-element Array{Float64,1}:\n", 54 | " 0.5\n", 55 | " 0.2\n", 56 | " 0.3\n", 57 | " 0.4" 58 | ] 59 | }, 60 | "execution_count": 10, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "link_lengths = [0.5,0.2,0.3,0.4]\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 37, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "(DynamicPolynomials.PolyVar{true}[p₁₋₁ p₁₋₂; p₂₋₁ p₂₋₂; p₃₋₁ p₃₋₂; p₄₋₁ p₄₋₂],)" 78 | ] 79 | }, 80 | "execution_count": 37, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "@polyvar p[1:4,1:2]" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 73, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "4-element Array{DynamicPolynomials.Polynomial{true,Int64},1}:\n", 98 | " p₁₋₁² + p₁₋₂² - 1\n", 99 | " p₂₋₁² + p₂₋₂² - 1\n", 100 | " p₃₋₁² + p₃₋₂² - 1\n", 101 | " p₄₋₁² + p₄₋₂² - 1" 102 | ] 103 | }, 104 | "execution_count": 73, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "q = vec(sum(p .* p, dims=2) .- 1)\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 55, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "3×1 Array{DynamicPolynomials.Polynomial{true,Float64},2}:\n", 122 | " p₁₋₁² - 2.0p₁₋₁p₂₋₁ + p₂₋₁² + p₁₋₂² - 2.0p₁₋₂p₂₋₂ + p₂₋₂² - 0.2\n", 123 | " p₂₋₁² - 2.0p₂₋₁p₃₋₁ + p₃₋₁² + p₂₋₂² - 2.0p₂₋₂p₃₋₂ + p₃₋₂² - 0.3\n", 124 | " p₃₋₁² - 2.0p₃₋₁p₄₋₁ + p₄₋₁² + p₃₋₂² - 2.0p₃₋₂p₄₋₂ + p₄₋₂² - 0.4" 125 | ] 126 | }, 127 | "execution_count": 55, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "#r = sum((p[2:4,:] .- p[1:3,:]).^2,dims=2) - link_lengths[2:4]" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 65, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "2-element Array{Float64,1}:\n", 145 | " -0.5\n", 146 | " 0.7" 147 | ] 148 | }, 149 | "execution_count": 65, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "end_position = [-0.5, .7]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 72, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "2-element Array{DynamicPolynomials.Polynomial{true,Float64},1}:\n", 167 | " 0.5p₁₋₁ + 0.2p₂₋₁ + 0.3p₃₋₁ + 0.4p₄₋₁ + 0.5\n", 168 | " 0.5p₁₋₂ + 0.2p₂₋₂ + 0.3p₃₋₂ + 0.4p₄₋₂ - 0.7" 169 | ] 170 | }, 171 | "execution_count": 72, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "r = vec(sum(reshape(link_lengths, (4,1)) .* p,dims=1)) - end_position #- reshape(end_position,(1,2))" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 87, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "8-element Array{DynamicPolynomials.Polynomial{true,Float64},1}:\n", 196 | " p₁₋₁² + p₁₋₂² - 1.0 \n", 197 | " p₂₋₁² + p₂₋₂² - 1.0 \n", 198 | " p₃₋₁² + p₃₋₂² - 1.0 \n", 199 | " p₄₋₁² + p₄₋₂² - 1.0 \n", 200 | " 0.5p₁₋₁ + 0.2p₂₋₁ + 0.3p₃₋₁ + 0.4p₄₋₁ + 0.5\n", 201 | " 0.5p₁₋₂ + 0.2p₂₋₂ + 0.3p₃₋₂ + 0.4p₄₋₂ - 0.7\n", 202 | " p₁₋₁ + 1.0 \n", 203 | " p₁₋₂ " 204 | ] 205 | }, 206 | "execution_count": 87, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "#constraints = vcat(q,r, dot(rand(8), vec(p)), dot(rand(8), vec(p)) )\n", 213 | "constraints = vcat(q,r, p[1,1]+1, p[1,2] )" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 89, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "2-element Array{Array{Float64,1},1}:\n", 225 | " [-1.0, 0.968246, 3.0965e-15, -0.484123, 1.55882e-21, 0.25, 1.0, 0.875] \n", 226 | " [-1.0, -0.968246, 8.88279e-15, 0.484123, -1.45501e-21, 0.25, 1.0, 0.875]" 227 | ] 228 | }, 229 | "execution_count": 89, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "real_solutions(solve(constraints))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "Hmm. Randomly using linear cuts did not do what I was hoping. Maybe I could add an optimization criteria. closeness to other control points. just constraining the arm to go to the left worked\n", 243 | "Optics with polynomials.\n", 244 | "Iso surface starting positiion. S(x)=0 \n", 245 | "(x1-x2)^2 + (y1-y2)^2 = d\n", 246 | "d(x,x') = (x1-x2)^2 + (y1-y2)^2\n", 247 | "partial(d,x)\n" 248 | ] 249 | } 250 | ], 251 | "metadata": { 252 | "kernelspec": { 253 | "display_name": "Julia 1.1.1", 254 | "language": "julia", 255 | "name": "julia-1.1" 256 | }, 257 | "language_info": { 258 | "file_extension": ".jl", 259 | "mimetype": "application/julia", 260 | "name": "julia", 261 | "version": "1.1.1" 262 | } 263 | }, 264 | "nbformat": 4, 265 | "nbformat_minor": 2 266 | } 267 | -------------------------------------------------------------------------------- /SOS Diff Eq.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using JuMP\n", 10 | "using SumOfSquares\n", 11 | "using DynamicPolynomials\n", 12 | "using SCS" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "## Minimax optimization of the residual of a differential equation. \n", 20 | "\n", 21 | "\n", 22 | "We can't solve differential equations $Ly = 0$ exactly usually. We need to work in some finite subspace of the full function space $ y(t) = \\sum_i a_i f_i(t)$. A common criteria is to find a solution that is closest to obeying the differential equation in a least squares sense, say $ \\min (Ly)^2 $. This is nice because it leads to linear system of equations. However, a minimax objective $\\min \\max |Ly| $ is also feasible using the sum of squares method. See here for more http://www.philipzucker.com/deriving-the-chebyshev-polynomials-using-sum-of-squares-optimization-with-sympy-and-cvxpy/\n", 23 | "\n", 24 | "\n", 25 | "We can write down the optimization problem using a finite polynomial parametrization of our solution. We relax the condition of being some of squares everywhere to instead just a region of interest by adding a term that makes the inequality stricter in the domain and looser outside the domain. The domain is described by a polynomial expression $t (1 - t) $ which is positive when $ 0 \\leq t \\leq 1$ and negative otherwise. \n", 26 | "Here is an example for \n", 27 | "\n", 28 | "\n", 29 | "$$ \\frac{d^2 y}{dt^2}=-y$$\n", 30 | "$$y(0)=1$$ \n", 31 | "$$y'(0) = 0$$\n", 32 | "with exact solution $ \\cos(t) $\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 73, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "----------------------------------------------------------------------------\n", 45 | "\tSCS v2.0.2 - Splitting Conic Solver\n", 46 | "\t(c) Brendan O'Donoghue, Stanford University, 2012-2017\n", 47 | "----------------------------------------------------------------------------\n", 48 | "Lin-sys: sparse-indirect, nnz in A = 194, CG tol ~ 1/iter^(2.00)\n", 49 | "eps = 1.00e-05, alpha = 1.50, max_iters = 5000, normalize = 1, scale = 1.00\n", 50 | "acceleration_lookback = 20, rho_x = 1.00e-03\n", 51 | "Variables n = 78, constraints m = 96\n", 52 | "Cones:\tprimal zero / dual free vars: 24\n", 53 | "\tsd vars: 72, sd blks: 4\n", 54 | "Setup time: 5.36e-05s\n", 55 | "----------------------------------------------------------------------------\n", 56 | " Iter | pri res | dua res | rel gap | pri obj | dua obj | kap/tau | time (s)\n", 57 | "----------------------------------------------------------------------------\n", 58 | " 0| 4.78e+01 2.68e+01 9.76e-01 -7.62e+01 -4.32e-01 0.00e+00 7.52e-05 \n", 59 | " 100| 3.50e-03 1.47e-03 2.78e-05 4.86e-03 4.89e-03 1.90e-16 6.21e-03 \n", 60 | " 200| 9.02e-03 5.15e-03 4.34e-04 6.13e-03 6.57e-03 2.30e-17 1.28e-02 \n", 61 | " 300| 3.15e-02 2.19e-02 3.84e-05 5.68e-03 5.64e-03 6.49e-16 1.92e-02 \n", 62 | " 400| 9.99e-01 9.98e-01 2.19e-03 1.04e-03 -1.15e-03 9.14e-03 2.58e-02 \n", 63 | " 500| 1.02e-02 6.49e-03 7.17e-04 1.03e-02 1.10e-02 8.30e-04 3.22e-02 \n", 64 | " 600| 2.99e-03 1.53e-03 2.39e-04 1.22e-02 1.24e-02 2.99e-16 3.88e-02 \n", 65 | " 700| 3.72e-03 1.42e-03 1.45e-04 1.26e-02 1.28e-02 1.75e-16 4.51e-02 \n", 66 | " 800| 1.95e-03 7.17e-04 1.25e-04 1.30e-02 1.29e-02 7.31e-18 5.12e-02 \n", 67 | " 900| 3.50e-03 1.47e-03 1.05e-04 1.27e-02 1.28e-02 1.04e-17 5.75e-02 \n", 68 | " 1000| 1.88e-03 7.09e-04 3.37e-05 1.32e-02 1.31e-02 8.55e-17 6.39e-02 \n", 69 | " 1100| 1.39e-03 1.30e-03 2.08e-04 1.35e-02 1.33e-02 2.78e-17 7.56e-02 \n", 70 | " 1180| 4.01e-06 1.55e-06 5.25e-07 1.33e-02 1.33e-02 2.82e-16 8.12e-02 \n", 71 | "----------------------------------------------------------------------------\n", 72 | "Status: Solved\n", 73 | "Timing: Solve time: 8.12e-02s\n", 74 | "\tLin-sys: avg # CG iterations: 8.52, avg solve time: 9.38e-06s\n", 75 | "\tCones: avg projection time: 3.61e-05s\n", 76 | "\tAcceleration: avg step time: 2.04e-05s\n", 77 | "----------------------------------------------------------------------------\n", 78 | "Error metrics:\n", 79 | "dist(s, K) = 2.6215e-09, dist(y, K*) = 1.7954e-09, s'y/|s||y| = -2.0203e-10\n", 80 | "primal res: |Ax + s - b|_2 / (1 + |b|_2) = 4.0101e-06\n", 81 | "dual res: |A'y + c|_2 / (1 + |c|_2) = 1.5531e-06\n", 82 | "rel gap: |c'x + b'y| / (1 + |c'x| + |b'y|) = 5.2534e-07\n", 83 | "----------------------------------------------------------------------------\n", 84 | "c'x = 0.0133, -b'y = 0.0133\n", 85 | "============================================================================\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "@polyvar t\n", 91 | "T = monomials(t, 0:4)\n", 92 | "model = SOSModel(with_optimizer(SCS.Optimizer))\n", 93 | "@variable(model, y, Poly(T))\n", 94 | "@variable(model, α)\n", 95 | "dy = differentiate(y, t)\n", 96 | "ddy = differentiate(dy, t)\n", 97 | "domain = t*(π/2-t)\n", 98 | "@variable(model, λ_1 , SOSPoly(T))\n", 99 | "@variable(model, λ_2 , SOSPoly(T))\n", 100 | "@constraint(model, y(t => 0) == 1)\n", 101 | "@constraint(model, dy(t => 0) == 0)\n", 102 | "@constraint(model, ddy + y - λ_1*domain >= -α)\n", 103 | "@constraint(model, α >= ddy + y + λ_2*domain)\n", 104 | "\n", 105 | "@objective(model, Min, α)\n", 106 | "\n", 107 | "optimize!(model)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 74, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/latex": [ 118 | "$$ 0.027642031145745472t^{4} + 0.021799794207213376t^{3} - 0.5066442977156951t^{2} + 3.506190174561713e-8t + 1.0000000041335204 $$" 119 | ], 120 | "text/plain": [ 121 | "0.027642031145745472t⁴ + 0.021799794207213376t³ - 0.5066442977156951t² + 3.506190174561713e-8t + 1.0000000041335204" 122 | ] 123 | }, 124 | "execution_count": 74, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "value(y)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 78, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "image/svg+xml": [ 141 | "\n", 142 | "\n", 143 | "\n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | "\n", 148 | "\n", 151 | "\n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | "\n", 156 | "\n", 159 | "\n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | "\n", 164 | "\n", 167 | "\n", 170 | "\n", 173 | "\n", 176 | "\n", 179 | "\n", 182 | "\n", 185 | "\n", 188 | "\n", 191 | "\n", 194 | "\n", 197 | "\n", 200 | "\n", 203 | "\n", 206 | "\n", 209 | "\n", 212 | "\n", 215 | "\n", 218 | "\n", 221 | "\n", 224 | "\n", 225 | "0.0\n", 226 | "\n", 227 | "\n", 228 | "0.5\n", 229 | "\n", 230 | "\n", 231 | "1.0\n", 232 | "\n", 233 | "\n", 234 | "1.5\n", 235 | "\n", 236 | "\n", 237 | "0.00\n", 238 | "\n", 239 | "\n", 240 | "0.25\n", 241 | "\n", 242 | "\n", 243 | "0.50\n", 244 | "\n", 245 | "\n", 246 | "0.75\n", 247 | "\n", 248 | "\n", 249 | "1.00\n", 250 | "\n", 251 | "\n", 269 | "\n", 287 | "\n", 290 | "\n", 293 | "\n", 296 | "\n", 297 | "y1\n", 298 | "\n", 299 | "\n", 302 | "\n", 303 | "y2\n", 304 | "\n", 305 | "\n" 306 | ] 307 | }, 308 | "execution_count": 78, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "using Plots\n", 315 | "xs = 0:0.01:π/2; exact_y = cos.(xs); approx_y = map(x -> value(y)(t => x), xs)# These are the plotting data\n", 316 | "plot(xs,[exact_y,approx_y])\n" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "Nice" 324 | ] 325 | } 326 | ], 327 | "metadata": { 328 | "kernelspec": { 329 | "display_name": "Julia 1.2.0", 330 | "language": "julia", 331 | "name": "julia-1.2" 332 | }, 333 | "language_info": { 334 | "file_extension": ".jl", 335 | "mimetype": "application/julia", 336 | "name": "julia", 337 | "version": "1.2.0" 338 | } 339 | }, 340 | "nbformat": 4, 341 | "nbformat_minor": 2 342 | } 343 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/SOS Diff Eq-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "using JuMP\n", 10 | "using SumOfSquares\n", 11 | "using DynamicPolynomials\n", 12 | "using SCS" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "## Minimax optimization of the residual of a differential equation. \n", 20 | "\n", 21 | "\n", 22 | "We can't solve differential equations $Ly = 0$ exactly usually. We need to work in some finite subspace of the full function space $ y(t) = \\sum_i a_i f_i(t)$. A common criteria is to find a solution that is closest to obeying the differential equation in a least squares sense, say $ \\min (Ly)^2 $. This is nice because it leads to linear system of equations. However, a minimax objective $\\min \\max |Ly| $ is also feasible using the sum of squares method. See here for more http://www.philipzucker.com/deriving-the-chebyshev-polynomials-using-sum-of-squares-optimization-with-sympy-and-cvxpy/\n", 23 | "\n", 24 | "\n", 25 | "We can write down the optimization problem using a finite polynomial parametrization of our solution. We relax the condition of being some of squares everywhere to instead just a region of interest by adding a term that makes the inequality stricter in the domain and looser outside the domain. The domain is described by a polynomial expression $t (1 - t) $ which is positive when $ 0 \\leq t \\leq 1$ and negative otherwise. \n", 26 | "Here is an example for \n", 27 | "\n", 28 | "\n", 29 | "$$ \\frac{d^2 y}{dt^2}=-y$$\n", 30 | "$$y(0)=1$$ \n", 31 | "$$y'(0) = 0$$\n", 32 | "with exact solution $ \\cos(t) $\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 73, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "----------------------------------------------------------------------------\n", 45 | "\tSCS v2.0.2 - Splitting Conic Solver\n", 46 | "\t(c) Brendan O'Donoghue, Stanford University, 2012-2017\n", 47 | "----------------------------------------------------------------------------\n", 48 | "Lin-sys: sparse-indirect, nnz in A = 194, CG tol ~ 1/iter^(2.00)\n", 49 | "eps = 1.00e-05, alpha = 1.50, max_iters = 5000, normalize = 1, scale = 1.00\n", 50 | "acceleration_lookback = 20, rho_x = 1.00e-03\n", 51 | "Variables n = 78, constraints m = 96\n", 52 | "Cones:\tprimal zero / dual free vars: 24\n", 53 | "\tsd vars: 72, sd blks: 4\n", 54 | "Setup time: 5.36e-05s\n", 55 | "----------------------------------------------------------------------------\n", 56 | " Iter | pri res | dua res | rel gap | pri obj | dua obj | kap/tau | time (s)\n", 57 | "----------------------------------------------------------------------------\n", 58 | " 0| 4.78e+01 2.68e+01 9.76e-01 -7.62e+01 -4.32e-01 0.00e+00 7.52e-05 \n", 59 | " 100| 3.50e-03 1.47e-03 2.78e-05 4.86e-03 4.89e-03 1.90e-16 6.21e-03 \n", 60 | " 200| 9.02e-03 5.15e-03 4.34e-04 6.13e-03 6.57e-03 2.30e-17 1.28e-02 \n", 61 | " 300| 3.15e-02 2.19e-02 3.84e-05 5.68e-03 5.64e-03 6.49e-16 1.92e-02 \n", 62 | " 400| 9.99e-01 9.98e-01 2.19e-03 1.04e-03 -1.15e-03 9.14e-03 2.58e-02 \n", 63 | " 500| 1.02e-02 6.49e-03 7.17e-04 1.03e-02 1.10e-02 8.30e-04 3.22e-02 \n", 64 | " 600| 2.99e-03 1.53e-03 2.39e-04 1.22e-02 1.24e-02 2.99e-16 3.88e-02 \n", 65 | " 700| 3.72e-03 1.42e-03 1.45e-04 1.26e-02 1.28e-02 1.75e-16 4.51e-02 \n", 66 | " 800| 1.95e-03 7.17e-04 1.25e-04 1.30e-02 1.29e-02 7.31e-18 5.12e-02 \n", 67 | " 900| 3.50e-03 1.47e-03 1.05e-04 1.27e-02 1.28e-02 1.04e-17 5.75e-02 \n", 68 | " 1000| 1.88e-03 7.09e-04 3.37e-05 1.32e-02 1.31e-02 8.55e-17 6.39e-02 \n", 69 | " 1100| 1.39e-03 1.30e-03 2.08e-04 1.35e-02 1.33e-02 2.78e-17 7.56e-02 \n", 70 | " 1180| 4.01e-06 1.55e-06 5.25e-07 1.33e-02 1.33e-02 2.82e-16 8.12e-02 \n", 71 | "----------------------------------------------------------------------------\n", 72 | "Status: Solved\n", 73 | "Timing: Solve time: 8.12e-02s\n", 74 | "\tLin-sys: avg # CG iterations: 8.52, avg solve time: 9.38e-06s\n", 75 | "\tCones: avg projection time: 3.61e-05s\n", 76 | "\tAcceleration: avg step time: 2.04e-05s\n", 77 | "----------------------------------------------------------------------------\n", 78 | "Error metrics:\n", 79 | "dist(s, K) = 2.6215e-09, dist(y, K*) = 1.7954e-09, s'y/|s||y| = -2.0203e-10\n", 80 | "primal res: |Ax + s - b|_2 / (1 + |b|_2) = 4.0101e-06\n", 81 | "dual res: |A'y + c|_2 / (1 + |c|_2) = 1.5531e-06\n", 82 | "rel gap: |c'x + b'y| / (1 + |c'x| + |b'y|) = 5.2534e-07\n", 83 | "----------------------------------------------------------------------------\n", 84 | "c'x = 0.0133, -b'y = 0.0133\n", 85 | "============================================================================\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "@polyvar t\n", 91 | "T = monomials(t, 0:4)\n", 92 | "model = SOSModel(with_optimizer(SCS.Optimizer))\n", 93 | "@variable(model, y, Poly(T))\n", 94 | "@variable(model, α)\n", 95 | "dy = differentiate(y, t)\n", 96 | "ddy = differentiate(dy, t)\n", 97 | "domain = t*(π/2-t)\n", 98 | "@variable(model, λ_1 , SOSPoly(T))\n", 99 | "@variable(model, λ_2 , SOSPoly(T))\n", 100 | "@constraint(model, y(t => 0) == 1)\n", 101 | "@constraint(model, dy(t => 0) == 0)\n", 102 | "@constraint(model, ddy + y - λ_1*domain >= -α)\n", 103 | "@constraint(model, α >= ddy + y + λ_2*domain)\n", 104 | "\n", 105 | "@objective(model, Min, α)\n", 106 | "\n", 107 | "optimize!(model)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 74, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/latex": [ 118 | "$$ 0.027642031145745472t^{4} + 0.021799794207213376t^{3} - 0.5066442977156951t^{2} + 3.506190174561713e-8t + 1.0000000041335204 $$" 119 | ], 120 | "text/plain": [ 121 | "0.027642031145745472t⁴ + 0.021799794207213376t³ - 0.5066442977156951t² + 3.506190174561713e-8t + 1.0000000041335204" 122 | ] 123 | }, 124 | "execution_count": 74, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "value(y)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 78, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "image/svg+xml": [ 141 | "\n", 142 | "\n", 143 | "\n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | "\n", 148 | "\n", 151 | "\n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | "\n", 156 | "\n", 159 | "\n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | "\n", 164 | "\n", 167 | "\n", 170 | "\n", 173 | "\n", 176 | "\n", 179 | "\n", 182 | "\n", 185 | "\n", 188 | "\n", 191 | "\n", 194 | "\n", 197 | "\n", 200 | "\n", 203 | "\n", 206 | "\n", 209 | "\n", 212 | "\n", 215 | "\n", 218 | "\n", 221 | "\n", 224 | "\n", 225 | "0.0\n", 226 | "\n", 227 | "\n", 228 | "0.5\n", 229 | "\n", 230 | "\n", 231 | "1.0\n", 232 | "\n", 233 | "\n", 234 | "1.5\n", 235 | "\n", 236 | "\n", 237 | "0.00\n", 238 | "\n", 239 | "\n", 240 | "0.25\n", 241 | "\n", 242 | "\n", 243 | "0.50\n", 244 | "\n", 245 | "\n", 246 | "0.75\n", 247 | "\n", 248 | "\n", 249 | "1.00\n", 250 | "\n", 251 | "\n", 269 | "\n", 287 | "\n", 290 | "\n", 293 | "\n", 296 | "\n", 297 | "y1\n", 298 | "\n", 299 | "\n", 302 | "\n", 303 | "y2\n", 304 | "\n", 305 | "\n" 306 | ] 307 | }, 308 | "execution_count": 78, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "using Plots\n", 315 | "xs = 0:0.01:π/2; exact_y = cos.(xs); approx_y = map(x -> value(y)(t => x), xs)# These are the plotting data\n", 316 | "plot(xs,[exact_y,approx_y])\n" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "Nice" 324 | ] 325 | } 326 | ], 327 | "metadata": { 328 | "kernelspec": { 329 | "display_name": "Julia 1.2.0", 330 | "language": "julia", 331 | "name": "julia-1.2" 332 | }, 333 | "language_info": { 334 | "file_extension": ".jl", 335 | "mimetype": "application/julia", 336 | "name": "julia", 337 | "version": "1.2.0" 338 | } 339 | }, 340 | "nbformat": 4, 341 | "nbformat_minor": 2 342 | } 343 | --------------------------------------------------------------------------------