File size: 14,074 Bytes
8df23f4 | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | import OAI.Computability.BinPacking.Inventory.CurrentMatches
import OAI.Computability.BinPacking.Inventory.InventoryScores
import OAI.Computability.BinPacking.Inventory.LabelConservation
namespace OAI
noncomputable section
namespace BinPackingGap
open scoped BigOperators
variable {I : Instance} {b n : ℕ}
abbrev RoleItem (cls : I.Item → Subclass) (r : Role) :=
{i : I.Item // (cls i).role = r}
theorem roleItem_label_count (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (r : Role)
(roleLabel : RoleItem cls r → Fin n)
(hspec : ∀ i, label i.val = some (roleLabel i)) (v : Fin n) :
((Finset.univ.filter fun i => roleLabel i = v).card : ℤ) =
labelledRoleCount cls label r v := by
classical
have himage : (Finset.univ.filter fun i => roleLabel i = v).image Subtype.val =
Finset.univ.filter (fun i => (cls i).role = r ∧ label i = some v) := by
ext i
constructor
· intro hi
obtain ⟨a, ha, rfl⟩ := Finset.mem_image.mp hi
have hv := (Finset.mem_filter.mp ha).2
exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, a.property,
(hspec a).trans (congrArg some hv)⟩
· intro hi
obtain ⟨hr, hv⟩ := (Finset.mem_filter.mp hi).2
let a : RoleItem cls r := ⟨i, hr⟩
have ha : roleLabel a = v := Option.some.inj ((hspec a).symm.trans hv)
exact Finset.mem_image.mpr ⟨a, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ha⟩, rfl⟩
have hcard : (Finset.univ.filter fun i => roleLabel i = v).card =
(Finset.univ.filter fun i => (cls i).role = r ∧ label i = some v).card := by
rw [← himage, Finset.card_image_of_injective _ Subtype.val_injective]
rw [hcard]
simp [labelledRoleCount]
structure RoleLabelData (cls : I.Item → Subclass) (label : I.Item → Option (Fin n)) where
row : RoleItem cls .x → Fin n
anchor : RoleItem cls .anchor → Fin n
localLabel : RoleItem cls .«local» → Fin n
row_spec : ∀ i, label i.val = some (row i)
anchor_spec : ∀ i, label i.val = some (anchor i)
local_spec : ∀ i, label i.val = some (localLabel i)
global_spec : ∀ i : RoleItem cls .«global», label i.val = none
flag_spec : ∀ i : RoleItem cls .flag, label i.val = none
namespace RoleLabelData
theorem row_count {cls : I.Item → Subclass} {label : I.Item → Option (Fin n)}
(L : RoleLabelData cls label) (v : Fin n) :
((Finset.univ.filter fun i => L.row i = v).card : ℤ) =
labelledRoleCount cls label .x v :=
roleItem_label_count cls label .x L.row L.row_spec v
theorem anchor_count {cls : I.Item → Subclass} {label : I.Item → Option (Fin n)}
(L : RoleLabelData cls label) (v : Fin n) :
((Finset.univ.filter fun i => L.anchor i = v).card : ℤ) =
labelledRoleCount cls label .anchor v :=
roleItem_label_count cls label .anchor L.anchor L.anchor_spec v
theorem local_count {cls : I.Item → Subclass} {label : I.Item → Option (Fin n)}
(L : RoleLabelData cls label) (v : Fin n) :
((Finset.univ.filter fun i => L.localLabel i = v).card : ℤ) =
labelledRoleCount cls label .«local» v :=
roleItem_label_count cls label .«local» L.localLabel L.local_spec v
def of_valid (cls : I.Item → Subclass) (label : I.Item → Option (Fin n))
(hlabeled : ∀ i, (cls i).role = .x ∨ (cls i).role = .anchor ∨
(cls i).role = .«local» → ∃ v, label i = some v)
(hunlabeled : ∀ i, (cls i).role = .«global» ∨ (cls i).role = .flag →
label i = none) : RoleLabelData cls label where
row i := Classical.choose (hlabeled i.val (Or.inl i.property))
anchor i := Classical.choose (hlabeled i.val (Or.inr (Or.inl i.property)))
localLabel i := Classical.choose (hlabeled i.val (Or.inr (Or.inr i.property)))
row_spec i := Classical.choose_spec (hlabeled i.val (Or.inl i.property))
anchor_spec i := Classical.choose_spec (hlabeled i.val (Or.inr (Or.inl i.property)))
local_spec i := Classical.choose_spec (hlabeled i.val (Or.inr (Or.inr i.property)))
global_spec i := hunlabeled i.val (Or.inl i.property)
flag_spec i := hunlabeled i.val (Or.inr i.property)
end RoleLabelData
namespace Packing
def roleItemAt (p : Packing I b) (cls : I.Item → Subclass) (r : Role)
(t : p.TableBin cls) : RoleItem cls r :=
⟨p.itemAtRole cls t r, p.itemAtRole_role cls t r⟩
theorem roleItemAt_injective (p : Packing I b) (cls : I.Item → Subclass) (r : Role) :
Function.Injective (p.roleItemAt cls r) := by
intro t u h
exact p.itemAtRole_injective cls r (congrArg Subtype.val h)
theorem roleItemAt_labels (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label)
(t : p.TableBin cls) (r : Role) :
label (p.itemAtRole cls t r) = tableRoleLabel
(L.row (p.roleItemAt cls .x t))
(L.localLabel (p.roleItemAt cls .«local» t))
(L.anchor (p.roleItemAt cls .anchor t)) r := by
cases r with
| x => exact L.row_spec (p.roleItemAt cls .x t)
| anchor => exact L.anchor_spec (p.roleItemAt cls .anchor t)
| «global» => exact L.global_spec (p.roleItemAt cls .«global» t)
| «local» => exact L.local_spec (p.roleItemAt cls .«local» t)
| flag => exact L.flag_spec (p.roleItemAt cls .flag t)
theorem roleItemAt_labels_le (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label)
(coordinate : I.Item → ℚ)
(hsize : ∀ i, I.size i = encodedSize (cls i) (label i) (coordinate i))
(hw : ∀ i, |coordinate i| ≤ 6) (t : p.TableBin cls) :
L.row (p.roleItemAt cls .x t) ≤ L.anchor (p.roleItemAt cls .anchor t) ∧
L.localLabel (p.roleItemAt cls .«local» t) ≤ L.anchor (p.roleItemAt cls .anchor t) :=
p.table_labels_le_anchor cls label coordinate hsize hw t _ _ _
(p.roleItemAt_labels cls label L t)
theorem outside_roleItems_image_subset (p : Packing I b) (cls : I.Item → Subclass)
(r : Role) :
(LabelConservation.outsideItems (p.roleItemAt cls r)).image Subtype.val ⊆
p.outsideTableItems cls := by
classical
intro i hi
obtain ⟨a, ha, rfl⟩ := Finset.mem_image.mp hi
apply Finset.mem_filter.mpr
refine ⟨Finset.mem_univ _, ?_⟩
intro ht
have haout := (Finset.mem_sdiff.mp ha).2
apply haout
let t : p.TableBin cls := ⟨p.assignment a.val, ht⟩
refine Finset.mem_image.mpr ⟨t, Finset.mem_univ _, ?_⟩
apply Subtype.ext
exact p.itemAtRole_eq_of_mem cls t r a.val (p.mem_own_bin a.val) a.property
theorem outside_roleItems_card_le (p : Packing I b) (cls : I.Item → Subclass) (r : Role) :
(LabelConservation.outsideItems (p.roleItemAt cls r)).card ≤
(p.outsideTableItems cls).card := by
classical
rw [← Finset.card_image_of_injective (LabelConservation.outsideItems (p.roleItemAt cls r)) Subtype.val_injective]
exact Finset.card_le_card (p.outside_roleItems_image_subset cls r)
end Packing
namespace Coverage
theorem goodTuplesAt_iff_roleLabels (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label)
(v : Fin n) (t : p.TableBin cls) :
t ∈ goodTuplesAt p cls label v ↔
L.anchor (p.roleItemAt cls .anchor t) = v ∧
L.row (p.roleItemAt cls .x t) = v ∧
L.localLabel (p.roleItemAt cls .«local» t) = v := by
rw [mem_goodTuplesAt]
have hx := L.row_spec (p.roleItemAt cls .x t)
have ha := L.anchor_spec (p.roleItemAt cls .anchor t)
have hd := L.local_spec (p.roleItemAt cls .«local» t)
change label (p.itemAtRole cls t .x) = some _ at hx
change label (p.itemAtRole cls t .anchor) = some _ at ha
change label (p.itemAtRole cls t .«local») = some _ at hd
simp only [Packing.IsGoodTableBin, hx, ha, hd, Option.some.injEq]
constructor
· rintro ⟨⟨hr, hl⟩, hv⟩
exact ⟨hv, hr.trans hv, hl.trans hv⟩
· rintro ⟨hv, hr, hl⟩
exact ⟨⟨hr.trans hv.symm, hl.trans hv.symm⟩, hv⟩
def badRoleAnchors (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label) (v : Fin n) :
Finset (RoleItem cls .anchor) := by
classical
exact LabelConservation.badAnchors L.anchor L.row L.localLabel
(p.roleItemAt cls .anchor) (p.roleItemAt cls .x) (p.roleItemAt cls .«local») v
theorem mem_badRoleAnchors (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label)
(v : Fin n) (a : RoleItem cls .anchor) :
a ∈ badRoleAnchors p cls label L v ↔ L.anchor a = v ∧
¬ ∃ t, p.roleItemAt cls .anchor t = a ∧
L.row (p.roleItemAt cls .x t) = v ∧
L.localLabel (p.roleItemAt cls .«local» t) = v := by
classical
exact LabelConservation.mem_badAnchors_iff L.anchor L.row L.localLabel
(p.roleItemAt cls .anchor) (p.roleItemAt cls .x) (p.roleItemAt cls .«local»)
(p.roleItemAt_injective cls .anchor) v a
theorem badRoleAnchors_image_eq (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label) (v : Fin n) :
(badRoleAnchors p cls label L v).image Subtype.val = badAnchorsAt p cls label v := by
classical
ext i
constructor
· intro hi
obtain ⟨a, ha, rfl⟩ := Finset.mem_image.mp hi
obtain ⟨hv, hnot⟩ := (mem_badRoleAnchors p cls label L v a).mp ha
apply (mem_badAnchorsAt p cls label v a.val).mpr
refine ⟨(mem_anchorItemsAt cls label v a.val).mpr ⟨a.property, ?_⟩, ?_⟩
· rw [L.anchor_spec, hv]
· intro hgood
obtain ⟨t, ht, hat⟩ := Finset.mem_image.mp hgood
obtain ⟨_, hr, hd⟩ := (goodTuplesAt_iff_roleLabels p cls label L v t).mp ht
exact hnot ⟨t, Subtype.ext hat, hr, hd⟩
· intro hi
obtain ⟨hiA, hiBad⟩ := (mem_badAnchorsAt p cls label v i).mp hi
obtain ⟨hr, hv⟩ := (mem_anchorItemsAt cls label v i).mp hiA
let a : RoleItem cls .anchor := ⟨i, hr⟩
have hva : L.anchor a = v := Option.some.inj ((L.anchor_spec a).symm.trans hv)
refine Finset.mem_image.mpr ⟨a, ?_, rfl⟩
apply (mem_badRoleAnchors p cls label L v a).mpr
refine ⟨hva, ?_⟩
rintro ⟨t, hat, hrow, hlocal⟩
apply hiBad
refine Finset.mem_image.mpr ⟨t, ?_, congrArg Subtype.val hat⟩
apply (goodTuplesAt_iff_roleLabels p cls label L v t).mpr
exact ⟨by simpa only [hat] using hva, hrow, hlocal⟩
@[simp] theorem badRoleAnchors_card (p : Packing I b) (cls : I.Item → Subclass)
(label : I.Item → Option (Fin n)) (L : RoleLabelData cls label) (v : Fin n) :
(badRoleAnchors p cls label L v).card = (badAnchorsAt p cls label v).card := by
classical
rw [← badRoleAnchors_image_eq p cls label L v, Finset.card_image_of_injective _ Subtype.val_injective]
variable (p : Packing I b) (cls : I.Item → Subclass)
variable (label : I.Item → Option (Fin n)) (L : RoleLabelData cls label)
variable (coordinate : I.Item → ℚ)
variable (hsize : ∀ i, I.size i = encodedSize (cls i) (label i) (coordinate i))
variable (hw : ∀ i, |coordinate i| ≤ 6) (B c : ℕ)
variable (hcard : I.n = 5 * B) (hb : b ≤ B + c)
variable (htotal : (∑ i, primaryScore (cls i)) = 0)
variable (hInvR : ∀ v, (Finset.univ.filter fun a => L.anchor a = v).card =
(Finset.univ.filter fun r => L.row r = v).card)
variable (hInvD : ∀ v, (Finset.univ.filter fun a => L.anchor a = v).card =
(Finset.univ.filter fun d => L.localLabel d = v).card)
include L coordinate hsize hw B hcard hb htotal hInvR hInvD
theorem badAnchorsAt_card_le (v : Fin n) :
(badAnchorsAt p cls label v).card ≤
3 * (25 * (c + 1) * (1 + 5 * primaryBound)) := by
classical
have hOutside := (p.outside_roleItems_card_le cls .anchor).trans
(p.outsideTableItems_card_lt cls label coordinate hsize hw B c hcard hb htotal).le
have hleR (t : p.TableBin cls) :
(L.row (p.roleItemAt cls .x t)).val ≤
(L.anchor (p.roleItemAt cls .anchor t)).val :=
(p.roleItemAt_labels_le cls label L coordinate hsize hw t).1
have hleD (t : p.TableBin cls) :
(L.localLabel (p.roleItemAt cls .«local» t)).val ≤
(L.anchor (p.roleItemAt cls .anchor t)).val :=
(p.roleItemAt_labels_le cls label L coordinate hsize hw t).2
have h := LabelConservation.badAnchors_card_le_three_mul L.anchor L.row L.localLabel
(p.roleItemAt cls .anchor) (p.roleItemAt cls .x) (p.roleItemAt cls .«local»)
(p.roleItemAt_injective cls .anchor) (p.roleItemAt_injective cls .x)
(p.roleItemAt_injective cls .«local») hleR hleD hInvR hInvD
(25 * (c + 1) * (1 + 5 * primaryBound)) hOutside v
change (badRoleAnchors p cls label L v).card ≤ _ at h
simpa only [badRoleAnchors_card] using h
theorem badAnchorsAt_subset_card_le (v : Fin n) (s : Finset I.Item)
(hs : s ⊆ badAnchorsAt p cls label v) :
s.card ≤ 3 * (25 * (c + 1) * (1 + 5 * primaryBound)) :=
(Finset.card_le_card hs).trans
(badAnchorsAt_card_le p cls label L coordinate hsize hw B c hcard hb htotal hInvR hInvD v)
theorem anchor_subset_loss_le (v : Fin n) (s : Finset I.Item)
(hs : s ⊆ anchorItemsAt (I := I) cls label v) :
(s \ roleItems p cls (goodTuplesAt p cls label v) .anchor).card ≤
3 * (25 * (c + 1) * (1 + 5 * primaryBound)) := by
apply badAnchorsAt_subset_card_le p cls label L coordinate hsize hw B c
hcard hb htotal hInvR hInvD v
intro i hi
obtain ⟨his, hin⟩ := Finset.mem_sdiff.mp hi
exact Finset.mem_sdiff.mpr ⟨hs his, hin⟩
theorem badAnchors_global_card_le :
((Finset.univ : Finset (Fin n)).biUnion (badAnchorsAt p cls label)).card ≤
3 * n * (25 * (c + 1) * (1 + 5 * primaryBound)) := by
classical
calc
_ ≤ ∑ v : Fin n, (badAnchorsAt p cls label v).card := Finset.card_biUnion_le
_ ≤ ∑ _v : Fin n, 3 * (25 * (c + 1) * (1 + 5 * primaryBound)) := by
apply Finset.sum_le_sum
intro v _
exact badAnchorsAt_card_le p cls label L coordinate hsize hw B c
hcard hb htotal hInvR hInvD v
_ = _ := by simp [Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc]
end Coverage
end BinPackingGap
end
end OAI
|