Download data/lean/OAI/Computability/BinPacking/Model.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/BinPacking/Model.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/BinPacking/Model.lean
-
curl -L -o Model.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/BinPacking/Model.lean
11.5 kB
| import Mathlib | |
| namespace OAI | |
| noncomputable section | |
| namespace BinPackingGames.Foundations.Complexity | |
| def encodeWord (n : Nat) : List Bool := List.replicate n true ++ [false] | |
| end BinPackingGames.Foundations.Complexity | |
| namespace BinPackingGames.Foundations.Complexity.MachineFiniteAlphabet | |
| open Turing | |
| def FiniteAlphabet (M : FinTM2) : Prop := β k, Finite (M.Ξ k) | |
| end BinPackingGames.Foundations.Complexity.MachineFiniteAlphabet | |
| namespace BinPackingGames.Foundations.Complexity.CookLevin | |
| def pairBits (input : List Bool Γ List Bool) : List Bool := | |
| encodeWord input.1.length ++ input.1 ++ input.2 | |
| structure NPVerifier where | |
| witnessBound : Polynomial Nat | |
| verify : (List Bool Γ List Bool) β Bool | |
| computation : Turing.TM2ComputableInPolyTime pairBits (fun bit => [bit]) verify | |
| finiteAlphabet : (k : computation.tm.K) β Fintype (computation.tm.Ξ k) | |
| def NPVerifier.Accepts (verifier : NPVerifier) (input : List Bool) : Prop := | |
| β witness : List Bool, | |
| witness.length β€ verifier.witnessBound.eval input.length β§ | |
| verifier.verify (input, witness) = true | |
| def InNP (language : List Bool β Prop) : Prop := | |
| β verifier : NPVerifier, β input, language input β verifier.Accepts input | |
| end BinPackingGames.Foundations.Complexity.CookLevin | |
| namespace BinPackingCompleteness.BinaryEncoding | |
| def bitsValue : List Bool β Nat | |
| | [] => 0 | |
| | b :: bits => Nat.bit b (bitsValue bits) | |
| def frame : List Bool β List Bool | |
| | [] => [false] | |
| | b :: bits => true :: b :: frame bits | |
| def parseFrame : List Bool β Option (List Bool Γ List Bool) | |
| | false :: rest => some ([], rest) | |
| | true :: b :: rest => do | |
| let (bits, trailing) β parseFrame rest | |
| return (b :: bits, trailing) | |
| | _ => none | |
| def nameBits (name : Nat) : List Bool := frame name.bits | |
| def parseName (input : List Bool) : Option (Nat Γ List Bool) := do | |
| let (digits, rest) β parseFrame input | |
| let name := bitsValue digits | |
| if digits = name.bits then some (name, rest) else none | |
| end BinPackingCompleteness.BinaryEncoding | |
| namespace BinPackingGap | |
| open scoped BigOperators | |
| structure Instance where | |
| n : β | |
| size : Fin n β β | |
| size_pos : β i, 0 < size i | |
| size_le_one : β i, size i β€ 1 | |
| namespace Instance | |
| abbrev Item (I : Instance) := Fin I.n | |
| end Instance | |
| structure Packing (I : Instance) (b : β) where | |
| assignment : I.Item β Fin b | |
| capacity : β j, (β i, if assignment i = j then I.size i else 0) β€ 1 | |
| def HasPacking (I : Instance) (b : β) : Prop := Nonempty (Packing I b) | |
| def singletonPacking (I : Instance) : Packing I I.n where | |
| assignment i := i | |
| capacity j := by | |
| have hsum : (β i, if i = j then I.size i else 0) = I.size j := by | |
| calc | |
| (β i, if i = j then I.size i else 0) = | |
| (if j = j then I.size j else 0) := | |
| Finset.sum_eq_single j (fun i _ hij => ite_eq_right hij) | |
| (fun h => False.elim (h (Finset.mem_univ _))) | |
| _ = I.size j := ite_eq_left rfl | |
| rw [hsum] | |
| exact I.size_le_one j | |
| theorem exists_packing (I : Instance) : β b, HasPacking I b := | |
| β¨I.n, β¨singletonPacking Iβ©β© | |
| noncomputable def opt (I : Instance) : β := by | |
| classical | |
| exact Nat.find (exists_packing I) | |
| def IndividualConfiguration (I : Instance) := | |
| {H : Finset I.Item // β i β H, I.size i β€ 1} | |
| noncomputable instance (I : Instance) : Fintype (IndividualConfiguration I) := | |
| inferInstanceAs (Fintype {H : Finset I.Item // β i β H, I.size i β€ 1}) | |
| def NumericalSize (I : Instance) := β₯(Finset.univ.image I.size) | |
| instance (I : Instance) : Fintype (NumericalSize I) := | |
| inferInstanceAs (Fintype β₯(Finset.univ.image I.size)) | |
| namespace NumericalSize | |
| variable {I : Instance} | |
| theorem positive (s : NumericalSize I) : 0 < s.val := by | |
| obtain β¨i, _, hiβ© := Finset.mem_image.mp s.property | |
| rw [β hi] | |
| exact I.size_pos i | |
| def multiplicity (s : NumericalSize I) : β := | |
| (Finset.univ.filter fun i : I.Item => I.size i = s.val).card | |
| def capacityBound (s : NumericalSize I) : β := | |
| Classical.choose (exists_nat_gt (1 / s.val)) | |
| theorem capacityBound_spec (s : NumericalSize I) : 1 / s.val < (s.capacityBound : β) := | |
| Classical.choose_spec (exists_nat_gt (1 / s.val)) | |
| end NumericalSize | |
| def TypeConfiguration (I : Instance) := | |
| {c : NumericalSize I β β // β s, s.val * (c s : β) β€ 1} | |
| namespace TypeConfiguration | |
| variable {I : Instance} | |
| theorem count_lt_bound (c : TypeConfiguration I) (s : NumericalSize I) : | |
| c.val s < s.capacityBound := by | |
| have hterm : s.val * (c.val s : β) β€ β t, t.val * (c.val t : β) := by | |
| exact Finset.single_le_sum (fun t _ => mul_nonneg (le_of_lt t.positive) (Nat.cast_nonneg _)) | |
| (Finset.mem_univ s) | |
| have hquot : (c.val s : β) β€ 1 / s.val := by | |
| apply (le_div_iffβ s.positive).mpr | |
| simpa [mul_comm] using hterm.trans c.property | |
| have hlt := hquot.trans_lt s.capacityBound_spec | |
| exact_mod_cast hlt | |
| def boundedCounts (c : TypeConfiguration I) : β s : NumericalSize I, Fin s.capacityBound := | |
| fun s => β¨c.val s, c.count_lt_bound sβ© | |
| theorem boundedCounts_injective : | |
| Function.Injective | |
| (boundedCounts : TypeConfiguration I β β s : NumericalSize I, Fin s.capacityBound) := by | |
| intro c d h | |
| apply Subtype.ext | |
| funext s | |
| exact congrArg Fin.val (congrFun h s) | |
| instance : Finite (TypeConfiguration I) := by | |
| classical | |
| exact Finite.of_injective boundedCounts boundedCounts_injective | |
| instance : Fintype (TypeConfiguration I) := Fintype.ofFinite _ | |
| end TypeConfiguration | |
| namespace FractionalCover | |
| open Set | |
| variable {I C : Type*} [Fintype I] [Fintype C] | |
| def Feasible (a : C β I β β) (d : I β β) (w : C β β) : Prop := | |
| (β c, 0 β€ w c) β§ β i, d i β€ β c, a c i * w c | |
| def objective (w : C β β) : β := β c, w c | |
| def objectiveSet (a : C β I β β) (d : I β β) : Set β := | |
| {v | β w, Feasible a d w β§ objective w = v} | |
| def value (a : C β I β β) (d : I β β) : β := | |
| sInf (objectiveSet a d) | |
| end FractionalCover | |
| def individualMatrix (I : Instance) (H : IndividualConfiguration I) (i : I.Item) : β := | |
| if i β H.val then 1 else 0 | |
| def individualLP (I : Instance) : β := | |
| FractionalCover.value (individualMatrix I) (fun _ => 1) | |
| def typeMatrix (I : Instance) (c : TypeConfiguration I) (s : NumericalSize I) : β := | |
| c.val s | |
| def typeLP (I : Instance) : β := | |
| FractionalCover.value (typeMatrix I) (fun s => (s.multiplicity : β)) | |
| abbrev RawInstance := List (β Γ β) | |
| abbrev RawReductionOutput := β Γ RawInstance | |
| namespace RawInstance | |
| def Valid (r : RawInstance) : Prop := | |
| β q β r, 0 < q.1 β§ q.1 β€ q.2 | |
| def ratValue (q : β Γ β) : β := (q.1 : β) / (q.2 : β) | |
| theorem den_pos {r : RawInstance} (hr : r.Valid) {q : β Γ β} (hq : q β r) : | |
| 0 < q.2 := lt_of_lt_of_le (hr q hq).1 (hr q hq).2 | |
| def toInstance (r : RawInstance) (hr : r.Valid) : Instance where | |
| n := r.length | |
| size i := ratValue (r.get i) | |
| size_pos i := div_pos (Nat.cast_pos.mpr (hr _ (List.get_mem r i)).1) | |
| (Nat.cast_pos.mpr (den_pos hr (List.get_mem r i))) | |
| size_le_one i := (div_le_oneβ (Nat.cast_pos.mpr (den_pos hr (List.get_mem r i)))).mpr | |
| (Nat.cast_le.mpr (hr _ (List.get_mem r i)).2) | |
| end RawInstance | |
| structure RawPacking where | |
| bins : β | |
| assignments : List β | |
| namespace RawPacking | |
| def load (r : RawInstance) (a : List β) (j : β) : β := | |
| ((r.zip a).map fun qa => | |
| if qa.2 = j then RawInstance.ratValue qa.1 else 0).sum | |
| def Feasible (r : RawInstance) (p : RawPacking) : Prop := | |
| p.assignments.length = r.length β§ | |
| (β j β p.assignments, j < p.bins) β§ | |
| β j < p.bins, load r p.assignments j β€ 1 | |
| end RawPacking | |
| namespace BinaryEncoding | |
| abbrev natBits := BinPackingCompleteness.BinaryEncoding.nameBits | |
| abbrev parseNat := BinPackingCompleteness.BinaryEncoding.parseName | |
| def listBits {Ξ± : Type} (encode : Ξ± β List Bool) : List Ξ± β List Bool | |
| | [] => [false] | |
| | a :: rest => true :: (encode a ++ listBits encode rest) | |
| def pairBits {Ξ± Ξ² : Type} (ea : Ξ± β List Bool) (eb : Ξ² β List Bool) | |
| (p : Ξ± Γ Ξ²) : List Bool := ea p.1 ++ eb p.2 | |
| def optionBits {Ξ± : Type} (encode : Ξ± β List Bool) : Option Ξ± β List Bool | |
| | none => [false] | |
| | some a => true :: encode a | |
| abbrev Parser (Ξ± : Type) := List Bool β Option (Ξ± Γ List Bool) | |
| def parsePair {Ξ± Ξ² : Type} (pa : Parser Ξ±) (pb : Parser Ξ²) : Parser (Ξ± Γ Ξ²) := | |
| fun input => do | |
| let (a, rest) β pa input | |
| let (b, suffix) β pb rest | |
| return ((a, b), suffix) | |
| def parseListAux {Ξ± : Type} (parse : Parser Ξ±) : Nat β Parser (List Ξ±) | |
| | 0, _ => none | |
| | _ + 1, [] => none | |
| | _ + 1, false :: rest => some ([], rest) | |
| | fuel + 1, true :: rest => do | |
| let (a, rest) β parse rest | |
| let (as, suffix) β parseListAux parse fuel rest | |
| return (a :: as, suffix) | |
| def parseList {Ξ± : Type} (parse : Parser Ξ±) : Parser (List Ξ±) := | |
| fun input => parseListAux parse input.length input | |
| def decode {Ξ± : Type} (parse : Parser Ξ±) (input : List Bool) : Option Ξ± := do | |
| let (a, rest) β parse input | |
| if rest.isEmpty then some a else none | |
| def rawInstanceBits (r : RawInstance) : List Bool := | |
| listBits (pairBits natBits natBits) r | |
| def assignmentBits (a : List Nat) : List Bool := listBits natBits a | |
| def rawPackingBits (p : RawPacking) : List Bool := | |
| natBits p.bins ++ assignmentBits p.assignments | |
| def packingResultBits (p : Option RawPacking) : List Bool := optionBits rawPackingBits p | |
| def rawReductionOutputBits (out : RawReductionOutput) : List Bool := | |
| natBits out.1 ++ rawInstanceBits out.2 | |
| def parseRawInstance : Parser RawInstance := parseList (parsePair parseNat parseNat) | |
| def decodeRawInstance := decode parseRawInstance | |
| end BinaryEncoding | |
| open Turing BinPackingGames.Foundations.Complexity | |
| abbrev BitLanguage := List Bool β Prop | |
| def InP (L : BitLanguage) : Prop := | |
| β decider : List Bool β Bool, | |
| β computation : TM2ComputableInPolyTime | |
| (id : List Bool β List Bool) (fun b => [b]) decider, | |
| MachineFiniteAlphabet.FiniteAlphabet computation.tm β§ | |
| β x, L x β decider x = true | |
| def ClassP : Set BitLanguage := {L | InP L} | |
| def ClassNP : Set BitLanguage := {L | CookLevin.InNP L} | |
| def PEqualsNP : Prop := ClassP = ClassNP | |
| structure AbsoluteAdditiveAlgorithm (c : Nat) where | |
| run : List Bool β Option RawPacking | |
| computation : TM2ComputableInPolyTime (id : List Bool β List Bool) | |
| BinaryEncoding.packingResultBits run | |
| finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computation.tm | |
| guarantee : β (r : RawInstance) (hr : r.Valid), β p : RawPacking, | |
| run (BinaryEncoding.rawInstanceBits r) = some p β§ | |
| RawPacking.Feasible r p β§ p.bins β€ opt (r.toInstance hr) + c | |
| structure PackingGapReduction (c : Nat) (L : BitLanguage) where | |
| reduce : List Bool β RawReductionOutput | |
| computation : TM2ComputableInPolyTime (id : List Bool β List Bool) | |
| BinaryEncoding.rawReductionOutputBits reduce | |
| finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computation.tm | |
| valid : β x, (reduce x).2.Valid | |
| itemCount : β x, (reduce x).2.length = 5 * (reduce x).1 | |
| itemLower : β x i, | |
| (1 / 6 : β) < ((reduce x).2.toInstance (valid x)).size i | |
| completeness : β x, L x β | |
| HasPacking ((reduce x).2.toInstance (valid x)) (reduce x).1 | |
| soundness : β x, Β¬L x β | |
| Β¬HasPacking ((reduce x).2.toInstance (valid x)) ((reduce x).1 + c) | |
| def PackingGapNPHard (c : Nat) : Prop := | |
| β L : BitLanguage, CookLevin.InNP L β Nonempty (PackingGapReduction c L) | |
| end BinPackingGap | |
| end | |
| end OAI | |