math / data /lean /ComparatorChallenges /BinPackingGap.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
12.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
section
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
theorem main_results :
(βˆ€ c : β„•, βˆƒ I : Instance, βˆƒ B : β„•,
I.n = 5 * B ∧
(βˆ€ i, (1 / 6 : β„š) < I.size i ∧ I.size i < 1) ∧
B + c < opt I ∧ individualLP I = (B : ℝ) ∧ typeLP I = (B : ℝ)) ∧
(βˆ€ c : Nat, PackingGapNPHard c) ∧
((βˆƒ c : Nat, Nonempty (AbsoluteAdditiveAlgorithm c)) ↔ PEqualsNP) ∧
(PEqualsNP β†’ βˆƒ algorithm : AbsoluteAdditiveAlgorithm 0,
(βˆ€ (items : RawInstance) (valid : items.Valid), βˆƒ packing : RawPacking,
algorithm.run (BinaryEncoding.rawInstanceBits items) = some packing ∧
packing.Feasible items ∧ packing.bins = opt (items.toInstance valid)) ∧
algorithm.run (BinaryEncoding.rawInstanceBits []) = some ⟨0, []⟩ ∧
(βˆ€ input : List Bool, BinaryEncoding.decodeRawInstance input = none β†’
algorithm.run input = none) ∧
(βˆ€ items : RawInstance, Β¬ items.Valid β†’
algorithm.run (BinaryEncoding.rawInstanceBits items) = none)) := by
sorry
end BinPackingGap
end
end OAI