math / data /lean /OAI /Algebra /DepthFive /BalancedSchedule.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
16.5 kB
import Mathlib
namespace OAI
noncomputable section
open scoped BigOperators
namespace Problem335
namespace BalancedSchedule
/-- Number of `U` layers following the `i`th `V` layer. -/
def chunkLength (k s i : β„•) : β„• :=
1 + ((i + 1) * s / k - i * s / k)
lemma quotient_mono (k s : β„•) {i j : β„•} (hij : i ≀ j) :
i * s / k ≀ j * s / k :=
Nat.div_le_div_right (Nat.mul_le_mul_right s hij)
lemma chunkLength_pos (k s i : β„•) : 0 < chunkLength k s i := by
unfold chunkLength
omega
lemma chunkLength_le_two {k s : β„•} (hs : s < k) (i : β„•) :
chunkLength k s i ≀ 2 := by
have hk : 0 < k := lt_of_le_of_lt (Nat.zero_le s) hs
have hrem := Nat.mod_lt (i * s) hk
have hdiv := Nat.mod_add_div (i * s) k
have hnext : (i + 1) * s < (i * s / k + 2) * k := by nlinarith
have hq : (i + 1) * s / k < i * s / k + 2 :=
(Nat.div_lt_iff_lt_mul hk).2 hnext
unfold chunkLength
omega
lemma chunkLength_eq_one_or_two {k s : β„•} (hs : s < k) (i : β„•) :
chunkLength k s i = 1 ∨ chunkLength k s i = 2 := by
have hp := chunkLength_pos k s i
have hl := chunkLength_le_two hs i
omega
/-- The number of `U` layers in consecutive complete chunks. -/
lemma sum_chunkLength (k s a r : β„•) :
(βˆ‘ j ∈ Finset.range r, chunkLength k s (a + j)) =
r + ((a + r) * s / k - a * s / k) := by
induction r with
| zero => simp
| succ r ih =>
rw [Finset.sum_range_succ, ih]
have h₁ := quotient_mono k s (show a ≀ a + r by omega)
have hβ‚‚ := quotient_mono k s (show a + r ≀ a + (r + 1) by omega)
unfold chunkLength
have heq : a + r + 1 = a + (r + 1) := by omega
rw [heq]
omega
lemma sum_all_chunkLength {k : β„•} (hk : 0 < k) (s : β„•) :
(βˆ‘ j ∈ Finset.range k, chunkLength k s j) = k + s := by
simpa [Nat.mul_div_cancel_left s hk] using sum_chunkLength k s 0 k
/-- A complete-chunk block has discrepancy strictly less than `ρ = k / (k+s)`. -/
lemma complete_chunks_discrepancy_lt {k : β„•} (hk : 0 < k) (s a r : β„•) :
(r : ℝ) - (k : ℝ) / (k + s) *
(βˆ‘ j ∈ Finset.range r, (chunkLength k s (a + j) : ℝ)) <
(k : ℝ) / (k + s) := by
have hm := quotient_mono k s (show a ≀ a + r by omega)
have hsum : (βˆ‘ j ∈ Finset.range r, (chunkLength k s (a + j) : ℝ)) =
(r : ℝ) + (((a + r) * s / k : β„•) : ℝ) - ((a * s / k : β„•) : ℝ) := by
rw [← Nat.cast_sum, sum_chunkLength]
rw [Nat.cast_add, Nat.cast_sub hm]
ring
rw [hsum]
have hdβ‚€ := Nat.mod_add_div (a * s) k
have hd₁ := Nat.mod_add_div ((a + r) * s) k
have hr₁ := Nat.mod_lt ((a + r) * s) hk
have hdβ‚€' : ((a * s % k : β„•) : ℝ) + (k : ℝ) * ((a * s / k : β„•) : ℝ) =
(a : ℝ) * s := by exact_mod_cast hdβ‚€
have hd₁' : (((a + r) * s % k : β„•) : ℝ) + (k : ℝ) *
(((a + r) * s / k : β„•) : ℝ) = ((a : ℝ) + r) * s := by
exact_mod_cast hd₁
have hr₁' : (((a + r) * s % k : β„•) : ℝ) < (k : ℝ) := by exact_mod_cast hr₁
have hrβ‚€' : 0 ≀ ((a * s % k : β„•) : ℝ) := Nat.cast_nonneg _
have hk' : (0 : ℝ) < k := by exact_mod_cast hk
have hks : (0 : ℝ) < k + s := by positivity
apply (mul_lt_mul_iff_leftβ‚€ hks).1
field_simp
nlinarith
/-- The relative density used in the mixed rank measure. -/
def rho (k s : β„•) : ℝ := (k : ℝ) / (k + s)
/-- `true` denotes a `V` layer and `false` denotes a `U` layer. -/
def chunk (k s i : β„•) : List Bool :=
true :: List.replicate (chunkLength k s i) false
/-- The first `r` complete chunks of the balanced schedule. -/
def wordPrefix (k s : β„•) : β„• β†’ List Bool
| 0 => []
| r + 1 => wordPrefix k s r ++ chunk k s r
/-- The complete balanced layer schedule. -/
def word (k s : β„•) : List Bool := wordPrefix k s k
/-- The number of `V` layers minus `ρ` times the number of `U` layers. -/
def discrepancy (k s : β„•) (w : List Bool) : ℝ :=
(w.count true : ℝ) - rho k s * (w.count false : ℝ)
@[simp] lemma count_true_chunk (k s i : β„•) : (chunk k s i).count true = 1 := by
simp [chunk, List.count_replicate]
@[simp] lemma count_false_chunk (k s i : β„•) :
(chunk k s i).count false = chunkLength k s i := by
simp [chunk]
@[simp] lemma count_true_wordPrefix (k s r : β„•) :
(wordPrefix k s r).count true = r := by
induction r with
| zero => simp [wordPrefix]
| succ r ih => simp [wordPrefix, List.count_append, ih]
@[simp] lemma count_false_wordPrefix (k s r : β„•) :
(wordPrefix k s r).count false = r + r * s / k := by
induction r with
| zero => simp [wordPrefix]
| succ r ih =>
simp only [wordPrefix, List.count_append, ih, count_false_chunk]
have hm : r * s / k ≀ (r + 1) * s / k := quotient_mono k s (by omega)
unfold chunkLength
omega
@[simp] lemma discrepancy_nil (k s : β„•) : discrepancy k s [] = 0 := by
simp [discrepancy]
@[simp] lemma discrepancy_append (k s : β„•) (w z : List Bool) :
discrepancy k s (w ++ z) = discrepancy k s w + discrepancy k s z := by
simp only [discrepancy, List.count_append, Nat.cast_add]
ring
@[simp] lemma discrepancy_chunk (k s i : β„•) :
discrepancy k s (chunk k s i) = 1 - rho k s * chunkLength k s i := by
simp [discrepancy]
lemma discrepancy_wordPrefix_eq_mod {k : β„•} (hk : 0 < k) (s r : β„•) :
discrepancy k s (wordPrefix k s r) = ((r * s % k : β„•) : ℝ) / (k + s) := by
simp only [discrepancy, count_true_wordPrefix, count_false_wordPrefix, Nat.cast_add]
have hd := Nat.mod_add_div (r * s) k
have hd' : ((r * s % k : β„•) : ℝ) + (k : ℝ) * ((r * s / k : β„•) : ℝ) =
(r : ℝ) * s := by exact_mod_cast hd
have hk' : (0 : ℝ) < k := by exact_mod_cast hk
have hks : (0 : ℝ) < k + s := by positivity
unfold rho
field_simp
nlinarith
lemma discrepancy_wordPrefix_nonneg {k : β„•} (hk : 0 < k) (s r : β„•) :
0 ≀ discrepancy k s (wordPrefix k s r) := by
rw [discrepancy_wordPrefix_eq_mod hk]
positivity
lemma discrepancy_wordPrefix_lt_rho {k : β„•} (hk : 0 < k) (s r : β„•) :
discrepancy k s (wordPrefix k s r) < rho k s := by
rw [discrepancy_wordPrefix_eq_mod hk]
have hk' : (0 : ℝ) < k := by exact_mod_cast hk
have hks : (0 : ℝ) < k + s := by positivity
apply (div_lt_div_iff_of_pos_right hks).2
exact_mod_cast Nat.mod_lt (r * s) hk
lemma take_chunk_discrepancy (k s i p : β„•) (hp : 0 < p) :
discrepancy k s ((chunk k s i).take p) =
1 - rho k s * (min (p - 1) (chunkLength k s i) : β„•) := by
simp [chunk, List.take_cons hp, discrepancy, List.count_replicate]
/-- Every prefix of the balanced schedule stays in a strip of width `1+ρ`. -/
lemma take_wordPrefix_discrepancy_bounds {k : β„•} (hk : 0 < k) (s r p : β„•) :
0 ≀ discrepancy k s ((wordPrefix k s r).take p) ∧
discrepancy k s ((wordPrefix k s r).take p) < 1 + rho k s := by
have hrho : 0 ≀ rho k s := by unfold rho; positivity
induction r with
| zero => simp [wordPrefix]; positivity
| succ r ih =>
change 0 ≀ discrepancy k s ((wordPrefix k s r ++ chunk k s r).take p) ∧
discrepancy k s ((wordPrefix k s r ++ chunk k s r).take p) < 1 + rho k s
by_cases hp : p ≀ (wordPrefix k s r).length
Β· simpa only [List.take_append_of_le_length hp] using ih
Β· have hlt : (wordPrefix k s r).length < p := by omega
rw [List.take_append, List.take_of_length_le hlt.le, discrepancy_append]
rw [take_chunk_discrepancy k s r _ (by omega)]
have hmin : ((min (p - (wordPrefix k s r).length - 1) (chunkLength k s r) : β„•) : ℝ) ≀
(chunkLength k s r : ℝ) := by exact_mod_cast Nat.min_le_right _ _
have hmul := mul_le_mul_of_nonneg_left hmin hrho
have hnonneg := discrepancy_wordPrefix_nonneg hk s (r + 1)
simp only [wordPrefix, discrepancy_append, discrepancy_chunk] at hnonneg
have hupper := discrepancy_wordPrefix_lt_rho hk s r
have hprod : 0 ≀ rho k s *
((min (p - (wordPrefix k s r).length - 1) (chunkLength k s r) : β„•) : ℝ) := by positivity
constructor <;> linarith
/-- Every interval of layers has discrepancy strictly less than `1+ρ`. -/
lemma interval_discrepancy_lt {k : β„•} (hk : 0 < k) (s r a len : β„•) :
discrepancy k s (((wordPrefix k s r).drop a).take len) < 1 + rho k s := by
have h₁ := (take_wordPrefix_discrepancy_bounds hk s r a).1
have hβ‚‚ := (take_wordPrefix_discrepancy_bounds hk s r (a + len)).2
rw [List.take_add, discrepancy_append] at hβ‚‚
linarith
lemma infix_discrepancy_lt {k : β„•} (hk : 0 < k) (s r : β„•) {w : List Bool}
(hw : w <:+: wordPrefix k s r) : discrepancy k s w < 1 + rho k s := by
rcases hw with ⟨l, z, heq⟩
have h := interval_discrepancy_lt hk s r l.length w.length
rw [← heq, List.append_assoc, List.drop_left, List.take_left] at h
exact h
@[simp] lemma word_count_true (k s : β„•) : (word k s).count true = k := by
simp [word]
@[simp] lemma word_count_false {k : β„•} (hk : 0 < k) (s : β„•) :
(word k s).count false = k + s := by
simp [word, Nat.mul_div_cancel_left s hk]
lemma word_length {k : β„•} (hk : 0 < k) (s : β„•) :
(word k s).length = 2 * k + s := by
rw [← List.count_true_add_count_false, word_count_true, word_count_false hk]
omega
@[simp] lemma discrepancy_word {k : β„•} (hk : 0 < k) (s : β„•) :
discrepancy k s (word k s) = 0 := by
rw [word, discrepancy_wordPrefix_eq_mod hk]
simp
@[simp] lemma chunk_head (k s i : β„•) : (chunk k s i).head? = some true := rfl
@[simp] lemma chunk_last (k s i : β„•) : (chunk k s i).getLast? = some false := by
have hp := chunkLength_pos k s i
simp [chunk, List.getLast?_cons, List.getLast?_replicate, Nat.ne_of_gt hp]
lemma wordPrefix_head {r : β„•} (hr : 0 < r) (k s : β„•) :
(wordPrefix k s r).head? = some true := by
induction r with
| zero => omega
| succ r ih =>
cases r with
| zero => simp [wordPrefix]
| succ r =>
change (wordPrefix k s (r + 1) ++ chunk k s (r + 1)).head? = some true
rw [List.head?_append, ih (by omega)]
rfl
lemma wordPrefix_last {r : β„•} (hr : 0 < r) (k s : β„•) :
(wordPrefix k s r).getLast? = some false := by
cases r with
| zero => omega
| succ r => simp [wordPrefix, List.getLast?_append]
lemma replicate_no_adjacent_V (d : β„•) :
(List.replicate d false).IsChain (fun a b => a = false ∨ b = false) := by
rw [List.isChain_iff_getElem]
simp
lemma chunk_no_adjacent_V (k s i : β„•) :
(chunk k s i).IsChain (fun a b => a = false ∨ b = false) := by
unfold chunk
apply (List.isChain_cons).2
constructor
Β· intro y hy
right
exact List.eq_of_mem_replicate (List.mem_of_mem_head? hy)
Β· exact replicate_no_adjacent_V _
/-- No two `V` layers are adjacent, a key independence property of the schedule. -/
lemma wordPrefix_no_adjacent_V (k s r : β„•) :
(wordPrefix k s r).IsChain (fun a b => a = false ∨ b = false) := by
induction r with
| zero => simp [wordPrefix]
| succ r ih =>
apply ih.append (chunk_no_adjacent_V k s r)
intro x hx y hy
cases r with
| zero => simp [wordPrefix] at hx
| succ r =>
left
have hlast := wordPrefix_last (r := r + 1) (by omega) k s
rw [hlast] at hx
simpa [eq_comm] using hx
lemma word_no_adjacent_V (k s : β„•) :
(word k s).IsChain (fun a b => a = false ∨ b = false) :=
wordPrefix_no_adjacent_V k s k
/-- Finite-indexed form of the balanced layer partition. -/
def indexedWord {k : β„•} (hk : 0 < k) (s : β„•) : Fin (2 * k + s) β†’ Bool :=
fun i => (word k s).get ⟨i.val, by simp [word_length hk]⟩
/-- The finite set of the `V` layers. -/
def vLayers {k : β„•} (hk : 0 < k) (s : β„•) : Finset (Fin (2 * k + s)) :=
Finset.univ.filter (fun i => indexedWord hk s i = true)
/-- The finite set of the `U` layers. -/
def uLayers {k : β„•} (hk : 0 < k) (s : β„•) : Finset (Fin (2 * k + s)) :=
Finset.univ.filter (fun i => indexedWord hk s i = false)
lemma card_vLayers {k : β„•} (hk : 0 < k) (s : β„•) : (vLayers hk s).card = k := by
let v : List.Vector Bool (2 * k + s) := ⟨word k s, word_length hk s⟩
have h := Fin.card_filter_univ_eq_vector_get_eq_count true v
change (vLayers hk s).card = (word k s).count true at h
simpa using h
lemma card_uLayers {k : β„•} (hk : 0 < k) (s : β„•) : (uLayers hk s).card = k + s := by
let v : List.Vector Bool (2 * k + s) := ⟨word k s, word_length hk s⟩
have h := Fin.card_filter_univ_eq_vector_get_eq_count false v
change (uLayers hk s).card = (word k s).count false at h
simpa [word_count_false hk] using h
lemma layer_partition {k : β„•} (hk : 0 < k) (s : β„•) :
vLayers hk s βˆͺ uLayers hk s = Finset.univ := by
ext i
simp only [vLayers, uLayers, Finset.mem_union, Finset.mem_filter,
Finset.mem_univ, true_and, iff_true]
cases indexedWord hk s i <;> simp
lemma layer_disjoint {k : β„•} (hk : 0 < k) (s : β„•) :
Disjoint (vLayers hk s) (uLayers hk s) := by
apply Finset.disjoint_left.2
intro i hi hj
simp only [vLayers, uLayers, Finset.mem_filter, Finset.mem_univ, true_and] at hi hj
rw [hi] at hj
cases hj
lemma indexedWord_no_adjacent_V {k : β„•} (hk : 0 < k) (s : β„•)
(i j : Fin (2 * k + s)) (hij : i.val + 1 = j.val) :
indexedWord hk s i = false ∨ indexedWord hk s j = false := by
have hlt : i.val + 1 < (word k s).length := by
rw [word_length hk, hij]
exact j.isLt
have h := List.isChain_iff_getElem.mp (word_no_adjacent_V k s) i.val hlt
simpa only [indexedWord, List.get_eq_getElem, hij] using h
/-- Adjacent layer positions in either order. -/
def adjacent {n : β„•} (i j : Fin n) : Prop := i.val + 1 = j.val ∨ j.val + 1 = i.val
lemma vLayers_independent {k : β„•} (hk : 0 < k) (s : β„•)
{i j : Fin (2 * k + s)} (hi : i ∈ vLayers hk s) (hij : adjacent i j) :
j βˆ‰ vLayers hk s := by
intro hj
have hi' : indexedWord hk s i = true := (Finset.mem_filter.mp hi).2
have hj' : indexedWord hk s j = true := (Finset.mem_filter.mp hj).2
rcases hij with hij | hji
Β· have h := indexedWord_no_adjacent_V hk s i j hij
simp [hi', hj'] at h
Β· have h := indexedWord_no_adjacent_V hk s j i hji
simp [hi', hj'] at h
/-- Internal vertices adjacent to a layer. Vertex `j` is between layers `j` and `j+1`. -/
def internalEnds {n : β„•} (i : Fin n) : Finset (Fin (n - 1)) :=
Finset.univ.filter (fun j => j.val = i.val ∨ j.val + 1 = i.val)
lemma internalEnds_disjoint_of_not_adjacent {n : β„•} {i j : Fin n}
(hne : i β‰  j) (hij : Β¬ adjacent i j) :
Disjoint (internalEnds i) (internalEnds j) := by
apply Finset.disjoint_left.2
intro v hvi hvj
have hi := (Finset.mem_filter.mp hvi).2
have hj := (Finset.mem_filter.mp hvj).2
have hne' : i.val β‰  j.val := fun h => hne (Fin.ext h)
unfold adjacent at hij
omega
/-- Distinct selected V layers depend on disjoint internal-vertex coordinates. -/
lemma vLayers_internalEnds_disjoint {k : β„•} (hk : 0 < k) (s : β„•)
{i j : Fin (2 * k + s)} (hi : i ∈ vLayers hk s) (hj : j ∈ vLayers hk s)
(hne : i β‰  j) : Disjoint (internalEnds i) (internalEnds j) := by
apply internalEnds_disjoint_of_not_adjacent hne
exact fun hadj => vLayers_independent hk s hi hadj hj
/-- The left end of an interior layer is an internal vertex. -/
lemma mem_internalEnds_left {n : β„•} (i : Fin n) (hi : 0 < i.val) :
(⟨i.val - 1, by omega⟩ : Fin (n - 1)) ∈ internalEnds i := by
simp only [internalEnds, Finset.mem_filter, Finset.mem_univ, true_and]
right
exact Nat.sub_add_cancel hi
/-- The right end of a nonfinal layer is an internal vertex. -/
lemma mem_internalEnds_right {n : β„•} (i : Fin n) (hi : i.val + 1 < n) :
(⟨i.val, by omega⟩ : Fin (n - 1)) ∈ internalEnds i := by
simp [internalEnds]
lemma card_internalEnds_le_two {n : β„•} (i : Fin n) : (internalEnds i).card ≀ 2 := by
let f : Fin (n - 1) β†’ Bool := fun v => v.val = i.val
have hinj : Set.InjOn f (internalEnds i : Set (Fin (n - 1))) := by
intro a ha b hb hab
have ha' := (Finset.mem_filter.mp ha).2
have hb' := (Finset.mem_filter.mp hb).2
have hab' : (decide (a.val = i.val)) = decide (b.val = i.val) := hab
apply Fin.ext
by_cases hai : a.val = i.val
Β· have hbi : b.val = i.val := by simpa [hai] using hab'.symm
omega
Β· have hbi : b.val β‰  i.val := by simpa [hai] using hab'.symm
omega
calc
(internalEnds i).card = ((internalEnds i).image f).card :=
(Finset.card_image_of_injOn hinj).symm
_ ≀ Fintype.card Bool := Finset.card_le_univ _
_ = 2 := by decide
end BalancedSchedule
end Problem335
end
end OAI