-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize.lean
161 lines (135 loc) · 3.78 KB
/
optimize.lean
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import algebra.order.monoid
import data.nat.basic
import data.int.basic
section
variables {α : Type*} [has_lt α] (f : ℕ → α) (x : ℕ)
@[simp]
def concave [has_add α] (f : ℕ → α) :=
∀ x, f x + f (x + 2) < f (x + 1) + f (x + 1)
@[simp]
def increasing_at := f x < f (x + 1)
@[simp]
def decreasing_at := f (x + 1) < f x
end
section
variables {α : Type*} [ordered_cancel_add_comm_monoid α] {f : ℕ → α}
lemma concave_t (h : concave f) (x y) :
f x + f (x + y + 2) < f (x + 1) + f (x + y + 1) :=
begin
induction y,
case nat.zero {
exact h x,
},
case nat.succ : y ih {
specialize h (x + y + 1),
apply lt_of_add_lt_add_right,
apply lt_of_add_lt_add_right,
calc f x + f (x + y + 3) + f (x + y + 2) + f (x + y + 1)
= f x + f (x + y + 2) + (f (x + y + 1) + f (x + y + 3)) : by cc
... < f (x + 1) + f (x + y + 1) + (f (x + y + 2) + f (x + y + 2)) : add_lt_add ih h
... = f (x + 1) + f (x + y + 2) + f (x + y + 2) + f (x + y + 1) : by cc,
},
end
lemma concave_t' (h : concave f) {x y} :
x < y → f x + f (y + 1) < f (x + 1) + f y :=
begin
intro hxy,
cases nat.exists_eq_add_of_lt hxy with z hy,
rw hy,
apply concave_t h,
end
variable [decidable_rel (@has_lt.lt α _)]
def argmax' (h : concave f) : ∀ {x y}, increasing_at f x → decreasing_at f (x + y + 1) → ℕ
| x 0 _ _ := x + 1
| x (y + 1) hi hd :=
if hd' : f (x + y + 2) < f (x + y + 1)
then argmax' hi hd'
else by {
unfold decreasing_at at hd,
}
def maximum (h : concave f) : ∀ {x y}, increasing_at f x → decreasing_at f y → ℕ
| x y := sorry
example : concave f → increasing_at x + 1 →
end
section
variables (f : ℕ → ℤ) (x y : ℕ)
lemma concave_upper_bound : concave f → f x + f (x + y + 2) + y < f (x + 1) + f (x + y + 1) :=
begin
intro h,
induction y,
case nat.zero {
simp,
apply h,
},
case nat.succ : y ih {
have,
{
calc f x + f (x + y + 3) + y + 1 + f (x + y + 2) + f (x + y + 1)
= f x + f (x + y + 2) + y + (f (x + y + 1) + f (x + y + 3) + 1) : by cc
... < f (x + 1) + f (x + y + 1) + (f (x + y + 2) + f (x + y + 2)) : add_lt_add_of_lt_of_le ih (h (x + y + 1))
... = f (x + 1) + f (x + y + 2) + f (x + y + 2) + f (x + y + 1) : by cc,
},
rw [int.coe_nat_succ, ← add_assoc],
exact lt_of_add_lt_add_right (lt_of_add_lt_add_right this),
},
end
def find_decreasing' : ℕ → ℕ → ℕ
| x 0 := x
| x (y + 1) :=
if f (x + 1) < f x
then x
else find_decreasing' (x + 1) y
def find_decreasing : ℕ :=
if h : f 1 < f 0
then 0
else find_decreasing' f 1 (f 1 - f 0).to_nat
lemma find_decreasing'_bound : x ≤ find_decreasing' f x y ∧ find_decreasing' f x y ≤ x + y :=
begin
induction y generalizing x,
case nat.zero {
split; refl,
},
case nat.succ : y ih {
unfold find_decreasing',
by_cases f (x + 1) < f x; simp [h],
specialize ih (x + 1),
rw [add_assoc, add_comm 1 y] at ih,
split,
transitivity,
exact nat.le_succ _,
exact ih.left,
exact ih.right,
},
end
lemma int.coe_to_nat {x : ℤ} : 0 ≤ x → ↑x.to_nat = x :=
by cases x; simp
example : concave f → decreasing_at f (find_decreasing f) :=
begin
intro hf,
unfold decreasing_at find_decreasing,
by_cases f 1 < f 0; simp [h],
have := λ z, concave_upper_bound f 0 z hf,
simp at this,
generalize hy : (f 1 - f 0).to_nat = y,
have hf := int.coe_to_nat (sub_nonneg.mpr (not_lt.mp h)),
have : f 1 = f 0 + y,
rw ← hy,
rw hf,
simp,
rw this at *,
have hb := find_decreasing'_bound f 1 y,
induction y,
case nat.zero {
unfold find_decreasing',
simp at h hy,
specialize hf 0,
simp [le_antisymm h hy] at hf,
assumption,
},
case nat.succ : y ih {
},
have : f 1 = (f 1 - f 0).to_nat + f 0,
rw [hf, sub_add_cancel],
rw this,
end
end