File size: 11,523 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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