-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolytope.jl
More file actions
127 lines (118 loc) · 3.04 KB
/
polytope.jl
File metadata and controls
127 lines (118 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function build_polytope(N; shift=0.0)
return [[cospi(2k / N + shift), sinpi(2k / N + shift)] for k in 0:(N - 1)]
end
function init_plot(title="")
pl = Plots.plot(;
aspect_ratio=:equal,
legend=:outerleft,
xlim=(-1.1, 1.1),
ylim=(-1.1, 1.1),
title=title,
)
return pl
end;
function plot_polytope!(pl, vertices)
return Plots.plot!(
vcat(map(first, vertices), first(vertices[1])),
vcat(map(last, vertices), last(vertices[1]));
fillrange=0,
fillcolor=:gray,
fillalpha=0.2,
linecolor=:black,
label=L"\mathrm{conv}(\mathcal{V})",
)
end;
const logocolors = Colors.JULIA_LOGO_COLORS
function plot_objective!(pl, θ)
Plots.plot!(
pl,
[0.0, θ[1]],
[0.0, θ[2]];
color=logocolors.purple,
arrow=true,
lw=2,
label=nothing,
)
Plots.annotate!(pl, [-0.2 * θ[1]], [-0.2 * θ[2]], [L"\theta"])
return pl
end;
function plot_maximizer!(pl, θ, instance, maximizer)
ŷ = maximizer(θ; instance)
return Plots.scatter!(
pl,
[ŷ[1]],
[ŷ[2]];
color=logocolors.red,
markersize=9,
markershape=:square,
label=L"f(\theta)",
)
end;
# function get_angle(v)
# @assert !(norm(v) ≈ 0)
# v = v ./ norm(v)
# if v[2] >= 0
# return acos(v[1])
# else
# return π + acos(-v[1])
# end
# end;
# function plot_distribution!(pl, probadist)
# A = probadist.atoms
# As = sort(A; by=get_angle)
# p = probadist.weights
# Plots.plot!(
# pl,
# vcat(map(first, As), first(As[1])),
# vcat(map(last, As), last(As[1]));
# fillrange=0,
# fillcolor=:blue,
# fillalpha=0.1,
# linestyle=:dash,
# linecolor=logocolors.blue,
# label=L"\mathrm{conv}(\hat{p}(\theta))",
# )
# return Plots.scatter!(
# pl,
# map(first, A),
# map(last, A);
# markersize=25 .* p .^ 0.5,
# markercolor=logocolors.blue,
# markerstrokewidth=0,
# markeralpha=0.4,
# label=L"\hat{p}(\theta)",
# )
# end;
# function plot_expectation!(pl, probadist)
# ŷΩ = compute_expectation(probadist)
# return scatter!(
# pl,
# [ŷΩ[1]],
# [ŷΩ[2]];
# color=logocolors.blue,
# markersize=6,
# markershape=:hexagon,
# label=L"\hat{f}(\theta)",
# )
# end;
# function compress_distribution!(
# probadist::FixedAtomsProbabilityDistribution{A,W}; atol=0
# ) where {A,W}
# (; atoms, weights) = probadist
# to_delete = Int[]
# for i in length(probadist):-1:1
# ai = atoms[i]
# for j in 1:(i - 1)
# aj = atoms[j]
# if isapprox(ai, aj; atol=atol)
# weights[j] += weights[i]
# push!(to_delete, i)
# break
# end
# end
# end
# sort!(to_delete)
# deleteat!(atoms, to_delete)
# deleteat!(weights, to_delete)
# return probadist
# end;