math / data /lean /OAI /Computability /BinPacking /Inventory /PhysicalPoolLift.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
9.05 kB
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.Ring
import OAI.Computability.BinPacking.Configurations.ConfigurationLP
namespace OAI
noncomputable section
open scoped BigOperators
namespace BinPackingGap
structure PoolTemplates (I : Instance) (T U F : Type*) where
base : T β†’ Finset I.Item
globalPool : U β†’ Finset I.Item
flagPool : F β†’ Finset I.Item
globalSpecies : T β†’ U
flagSpecies : T β†’ F
global_nonempty : βˆ€ t, (globalPool (globalSpecies t)).Nonempty
flag_nonempty : βˆ€ t, (flagPool (flagSpecies t)).Nonempty
base_global : βˆ€ t, Disjoint (base t) (globalPool (globalSpecies t))
base_flag : βˆ€ t, Disjoint (base t) (flagPool (flagSpecies t))
global_flag : βˆ€ t,
Disjoint (globalPool (globalSpecies t)) (flagPool (flagSpecies t))
capacity : βˆ€ t (u : {i // i ∈ globalPool (globalSpecies t)})
(f : {i // i ∈ flagPool (flagSpecies t)}),
(βˆ‘ i ∈ base t βˆͺ {u.val, f.val}, I.size i) ≀ 1
namespace PoolTemplates
variable {I : Instance} {T U F : Type*} [Fintype T]
variable (D : PoolTemplates I T U F)
abbrev Globals (t : T) := {i // i ∈ D.globalPool (D.globalSpecies t)}
abbrev Flags (t : T) := {i // i ∈ D.flagPool (D.flagSpecies t)}
abbrev Sample := (t : T) Γ— (D.Globals t Γ— D.Flags t)
def column (t : T) (u : D.Globals t) (f : D.Flags t) : IndividualConfiguration I :=
⟨D.base t βˆͺ {u.val, f.val}, D.capacity t u f⟩
def sampleColumn (s : D.Sample) : IndividualConfiguration I :=
D.column s.1 s.2.1 s.2.2
def templateMass (t : T) : ℝ :=
1 / (2 * ((D.globalPool (D.globalSpecies t)).card : ℝ) *
((D.flagPool (D.flagSpecies t)).card : ℝ))
def sampleMass (s : D.Sample) : ℝ := D.templateMass s.1
def weights : IndividualConfiguration I β†’ ℝ :=
FractionalCover.pushforward D.sampleColumn D.sampleMass
omit [Fintype T] in
theorem templateMass_nonneg (t : T) : 0 ≀ D.templateMass t := by
unfold templateMass
positivity
omit [Fintype T] in
theorem global_card_ne_zero (t : T) :
((D.globalPool (D.globalSpecies t)).card : ℝ) β‰  0 := by
exact_mod_cast (Finset.card_pos.mpr (D.global_nonempty t)).ne'
omit [Fintype T] in
theorem flag_card_ne_zero (t : T) :
((D.flagPool (D.flagSpecies t)).card : ℝ) β‰  0 := by
exact_mod_cast (Finset.card_pos.mpr (D.flag_nonempty t)).ne'
omit [Fintype T] in
theorem sum_templateMass (t : T) :
(βˆ‘ _u : D.Globals t, βˆ‘ _f : D.Flags t, D.templateMass t) = (1 / 2 : ℝ) := by
have hu := D.global_card_ne_zero t
have hf := D.flag_card_ne_zero t
simp only [Finset.sum_const, Finset.card_univ, Fintype.card_coe, nsmul_eq_mul]
unfold templateMass
field_simp [hu, hf]
theorem objective_weights :
FractionalCover.objective D.weights = (Fintype.card T : ℝ) / 2 := by
rw [weights, FractionalCover.objective_pushforward]
unfold FractionalCover.objective sampleMass
rw [Fintype.sum_sigma]
simp only [Fintype.sum_prod_type, sum_templateMass]
simp [div_eq_mul_inv]
theorem sum_pool_eq (S : Finset I.Item) (i : I.Item) (a : ℝ) :
(βˆ‘ u : {j // j ∈ S}, if i = u.val then a else 0) =
if i ∈ S then a else 0 := by
classical
by_cases hi : i ∈ S
Β· have he : βˆ€ u : {j // j ∈ S}, i = u.val ↔ u = ⟨i, hi⟩ := by
intro u
constructor
Β· intro h
exact Subtype.ext h.symm
Β· intro h
exact (congrArg Subtype.val h).symm
simp only [he]
simp [hi]
Β· have he : βˆ€ u : {j // j ∈ S}, i β‰  u.val := by
intro u h
exact hi (h.symm β–Έ u.property)
simp [he, hi]
omit [Fintype T] in
theorem template_coverage (t : T) (i : I.Item) :
(βˆ‘ u : D.Globals t, βˆ‘ f : D.Flags t,
(if i ∈ (D.column t u f).val then (1 : ℝ) else 0) * D.templateMass t) =
(if i ∈ D.base t then (1 / 2 : ℝ) else 0) +
(if i ∈ D.globalPool (D.globalSpecies t) then
1 / (2 * ((D.globalPool (D.globalSpecies t)).card : ℝ)) else 0) +
(if i ∈ D.flagPool (D.flagSpecies t) then
1 / (2 * ((D.flagPool (D.flagSpecies t)).card : ℝ)) else 0) := by
classical
have hu := D.global_card_ne_zero t
have hf := D.flag_card_ne_zero t
by_cases hb : i ∈ D.base t
Β· have hgu : i βˆ‰ D.globalPool (D.globalSpecies t) :=
fun h => Finset.disjoint_left.mp (D.base_global t) hb h
have hfl : i βˆ‰ D.flagPool (D.flagSpecies t) :=
fun h => Finset.disjoint_left.mp (D.base_flag t) hb h
simpa [column, hb, hgu, hfl] using D.sum_templateMass t
· by_cases hg : i ∈ D.globalPool (D.globalSpecies t)
Β· have hfl : i βˆ‰ D.flagPool (D.flagSpecies t) :=
fun h => Finset.disjoint_left.mp (D.global_flag t) hg h
have hcol : βˆ€ u : D.Globals t, βˆ€ f : D.Flags t,
i ∈ (D.column t u f).val ↔ i = u.val := by
intro u f
have hn : i β‰  f.val := fun h => hfl (h.symm β–Έ f.property)
simp [column, hb, hn]
simp only [hcol, ite_mul, one_mul, zero_mul]
rw [Finset.sum_comm]
simp_rw [sum_pool_eq]
simp only [hb, hg, hfl, ite_false, ite_true, zero_add, add_zero,
Finset.sum_const, Finset.card_univ, Fintype.card_coe, nsmul_eq_mul]
unfold templateMass
field_simp [hu, hf]
· by_cases hfl : i ∈ D.flagPool (D.flagSpecies t)
Β· have hcol : βˆ€ u : D.Globals t, βˆ€ f : D.Flags t,
i ∈ (D.column t u f).val ↔ i = f.val := by
intro u f
have hn : i β‰  u.val := fun h => hg (h.symm β–Έ u.property)
simp [column, hb, hn]
simp only [hcol, ite_mul, one_mul, zero_mul]
simp_rw [sum_pool_eq]
simp only [hb, hg, hfl, ite_false, ite_true, zero_add,
Finset.sum_const, Finset.card_univ, Fintype.card_coe, nsmul_eq_mul]
unfold templateMass
field_simp [hu, hf]
Β· have hcol : βˆ€ u : D.Globals t, βˆ€ f : D.Flags t,
i βˆ‰ (D.column t u f).val := by
intro u f
have hnu : i β‰  u.val := fun h => hg (h.symm β–Έ u.property)
have hnf : i β‰  f.val := fun h => hfl (h.symm β–Έ f.property)
simp [column, hb, hnu, hnf]
simp [hcol, hb, hg, hfl]
theorem sum_by_species {S : Type*} [Fintype S] [DecidableEq S]
(species : T β†’ S) (a : S β†’ ℝ) :
(βˆ‘ t, a (species t)) =
βˆ‘ s, ((Finset.univ.filter (fun t => species t = s)).card : ℝ) * a s := by
classical
calc
(βˆ‘ t, a (species t)) = βˆ‘ t, βˆ‘ s, if species t = s then a s else 0 := by simp
_ = βˆ‘ s, βˆ‘ t, if species t = s then a s else 0 := Finset.sum_comm
_ = _ := by
apply Finset.sum_congr rfl
intro s _
rw [← Finset.sum_filter]
simp
theorem sum_pool_demands {S : Type*} [Fintype S] [DecidableEq S]
(species : T β†’ S) (pool : S β†’ Finset I.Item)
(hdemand : βˆ€ s, (Finset.univ.filter (fun t => species t = s)).card =
2 * (pool s).card) (i : I.Item) :
(βˆ‘ t, if i ∈ pool (species t) then
1 / (2 * ((pool (species t)).card : ℝ)) else 0) =
βˆ‘ s, if i ∈ pool s then (1 : ℝ) else 0 := by
classical
rw [sum_by_species species (fun s =>
if i ∈ pool s then 1 / (2 * ((pool s).card : ℝ)) else 0)]
apply Finset.sum_congr rfl
intro s _
rw [hdemand s]
by_cases hi : i ∈ pool s
Β· have hs : ((pool s).card : ℝ) β‰  0 := by
exact_mod_cast (Finset.card_pos.mpr ⟨i, hi⟩).ne'
simp only [hi, ite_true, Nat.cast_mul, Nat.cast_ofNat]
field_simp [hs]
Β· simp [hi]
variable [Fintype U] [Fintype F] [DecidableEq U] [DecidableEq F]
theorem weights_feasible
(labeled : Finset I.Item)
(hbase : βˆ€ i, (Finset.univ.filter (fun t => i ∈ D.base t)).card =
if i ∈ labeled then 2 else 0)
(hglobal : βˆ€ u, (Finset.univ.filter (fun t => D.globalSpecies t = u)).card =
2 * (D.globalPool u).card)
(hflag : βˆ€ f, (Finset.univ.filter (fun t => D.flagSpecies t = f)).card =
2 * (D.flagPool f).card)
(hpartition : βˆ€ i,
(if i ∈ labeled then (1 : ℝ) else 0) +
(βˆ‘ u, if i ∈ D.globalPool u then (1 : ℝ) else 0) +
(βˆ‘ f, if i ∈ D.flagPool f then (1 : ℝ) else 0) = 1) :
IndividualFeasible I D.weights := by
classical
refine ⟨FractionalCover.pushforward_nonneg D.sampleColumn
(fun s => D.templateMass_nonneg s.1), ?_⟩
intro i
change 1 ≀ βˆ‘ H, individualMatrix I H i * D.weights H
rw [weights, FractionalCover.coverage_pushforward]
change 1 ≀ βˆ‘ s : D.Sample,
(if i ∈ (D.column s.1 s.2.1 s.2.2).val then (1 : ℝ) else 0) * D.templateMass s.1
rw [Fintype.sum_sigma]
simp only [Fintype.sum_prod_type, template_coverage, Finset.sum_add_distrib]
rw [sum_pool_demands D.globalSpecies D.globalPool hglobal,
sum_pool_demands D.flagSpecies D.flagPool hflag]
have hb : (βˆ‘ t, if i ∈ D.base t then (1 / 2 : ℝ) else 0) =
if i ∈ labeled then (1 : ℝ) else 0 := by
rw [← Finset.sum_filter]
simp only [Finset.sum_const, nsmul_eq_mul, hbase]
split_ifs <;> norm_num
simpa only [hb, hpartition i] using (le_refl (1 : ℝ))
end PoolTemplates
end BinPackingGap
end
end OAI