-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.v
1745 lines (1616 loc) · 61.9 KB
/
machine.v
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set Implicit Arguments.
Unset Strict Implicit.
Require Import QArith NArith.
Require Import ProofIrrelevance FunctionalExtensionality. (*FIXME: don't need funext*)
From mathcomp Require Import ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Import GRing.Theory Num.Def Num.Theory.
Require Import OUVerT.dist MWU.weights
OUVerT.numerics OUVerT.bigops games MWU.weightslang server smooth.
(** FIXME: This definition should replace [upto_oracle_eq] in weightslang.v *)
Inductive upto_oracle_eq (A : finType) T T' chanty chanty'
: state A T chanty -> state A T' chanty' -> Prop :=
| mkUptoOracleEq :
forall s s',
SCosts s = SCosts s' ->
SPrevCosts s = SPrevCosts s' ->
SWeights s = SWeights s' ->
SEpsilon s = SEpsilon s' ->
SOutputs s = SOutputs s' ->
upto_oracle_eq s s'.
Lemma upto_oracle_sym {A T ct T' ct'}
(s : state A T ct) (s' : state A T' ct')
: upto_oracle_eq s s' ->
upto_oracle_eq s' s.
Proof.
inversion 1; subst.
split => //.
Qed.
Lemma upto_oracle_trans {A T T'' T' ct ct'' ct'}
(s : state A T ct) (s'' : state A T'' ct'') (s' : state A T' ct')
: upto_oracle_eq s s'' ->
upto_oracle_eq s'' s' ->
upto_oracle_eq s s'.
Proof.
inversion 1; subst.
inversion 1; subst.
split => //.
{ by rewrite -H6. }
{ by rewrite -H7. }
{ by rewrite -H8. }
{ by rewrite -H9. }
by rewrite -H10.
Qed.
Section machine_semantics.
Local Open Scope ring_scope.
Variable A : finType.
Variable a0 : A.
Variable N : nat.
Context `{Hgame : game A N rat_realFieldType}.
Record ClientPkg : Type :=
mkClientPkg
{ sent : option (dist A rat_realFieldType)
; received : option {ffun A -> rat}
; received_ok :
forall cost_vec,
received = Some cost_vec ->
forall a, (0%:R <= cost_vec a <= 1%:R)%R
}.
Program Definition init_ClientPkg : ClientPkg :=
@mkClientPkg None None _.
Definition simple_oracle_recv
(pkg : ClientPkg)
(_ : unit)
(cost_vec : {ffun A -> rat})
(pkg' : ClientPkg)
: Prop
:= [/\ pkg.(received) = Some cost_vec
, pkg'.(received) = None
& pkg'.(sent) = pkg.(sent)].
Definition simple_oracle_send
(pkg : ClientPkg)
(d : dist A rat_realFieldType)
(_ : unit)
(pkg' : ClientPkg)
: Prop
:= [/\ pkg.(sent) = None
, pkg'.(sent) = Some d
& pkg'.(received) = pkg.(received)].
Program Instance simpleClientOracle : ClientOracle A ClientPkg unit :=
@weightslang.mkOracle
_ _ _
simple_oracle_recv
simple_oracle_send
_.
Next Obligation.
move: H; rewrite /simple_oracle_recv.
case: st => [sent recv rok]; case => /= H H1 H2.
case: (andP (rok _ H a)) => H3 H4.
rewrite ler_norml; apply/andP; split => //.
apply: ler_trans; last by apply: H3.
by [].
Qed.
Definition client_step
: com A -> state A ClientPkg unit -> com A -> state A ClientPkg unit -> Prop :=
@step' A a0 ClientPkg unit simpleClientOracle.
Definition client_state :=
(com A * @weightslang.state A ClientPkg unit)%type.
Definition upd {A : finType} {T : Type}
(a : A) (t : T) (s : {ffun A -> T}) :=
finfun (fun b => if a==b then t else s b).
Record machine_state : Type :=
mkMachineState
{ clients : {ffun 'I_N -> client_state};
hist : seq {ffun 'I_N -> dist A rat_realFieldType}
}.
Definition all_clients_have_sent
(m : machine_state)
(f : {ffun 'I_N -> dist A rat_realFieldType})
: Prop :=
forall i : 'I_N,
let: (c,s) := m.(clients) i in
s.(SOracleSt).(received) = None /\
s.(SOracleSt).(sent) = Some (f i).
(*FIXME: put in numerics*)
Lemma rat_to_R_opp_neq (n:nat) : rat_to_R n%:R = (-1)%R -> False.
Proof.
move => H.
suff: Rdefinitions.Rle 0 (rat_to_R n%:R).
{ move => H2.
have H3: (Rdefinitions.Rlt (-1) 0).
{ apply: Reals.RIneq.Ropp_lt_gt_0_contravar.
apply: Reals.RIneq.Rgt_lt.
apply: Reals.RIneq.Rlt_0_1. }
have H4: (Rdefinitions.Rlt (-1) (-1))%R.
{ apply: Reals.RIneq.Rlt_le_trans; first by apply: H3.
rewrite -H; apply: H2. }
apply: (Reals.RIneq.Rlt_irrefl _ H4). }
rewrite -rat_to_R0; apply: rat_to_R_le.
change ((0:rat) <= n%:R).
apply: ler0n.
Qed.
Section cost_vec.
Variable f : {ffun 'I_N -> dist A rat_realFieldType}.
Definition expUnilateral (i : 'I_N) (a : A) :=
\sum_(p : {ffun 'I_N -> A} | p i == a)
(\prod_(j : 'I_N | i!=j) f j (p j)) * (cost i p).
Definition cost_vec (i : 'I_N) : {ffun A -> rat} :=
[ffun a => expUnilateral i a].
Lemma marginal_unfold (F : {ffun 'I_N -> A} -> rat) i :
let P t (p : {ffun 'I_N -> A}) := p i == t in
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | P p.1 p.2) (F p.2) =
\sum_(p : {ffun 'I_N -> A}) (F p).
Proof.
move => P.
set (G (x : A) y := F y).
have ->:
\sum_(p | P p.1 p.2) F p.2 =
\sum_(p | predT p.1 && P p.1 p.2) G p.1 p.2 by apply: eq_big.
rewrite -pair_big_dep /= /G /P.
have ->:
\sum_i0 \sum_(j : {ffun 'I_N -> A} | j i == i0) F j =
\sum_i0 \sum_(j : {ffun 'I_N -> A} | predT j && (j i == i0)) F j.
{ by apply: eq_big. }
rewrite -partition_big //.
Qed.
(*FIXME: the following two lemmas can be generalize go BigOp form*)
Lemma prod_split (i : 'I_N) (y : {ffun 'I_N -> A}) :
\prod_(j in [set i]) (f j) (y j) *
\prod_(j in [set~ i]) (f j) (y j) = \prod_(j < N) (f j) (y j).
Proof.
have ->:
\prod_(j < N) (f j) (y j) =
\prod_(j in [predU (pred1 i) & [set~ i]]) (f j) (y j).
{ apply: congr_big => // j; rewrite /in_mem /=.
case H: (j == i).
{ by have ->: j \in pred1 i = true by rewrite /pred1 /in_mem /= H. }
have ->: j \in [set~ i] by rewrite in_setC1 H.
by rewrite orbC. }
rewrite bigU /=; last first.
{ by rewrite disjoint1 in_setC1; apply/negP; rewrite eq_refl. }
f_equal.
apply: congr_big => //; first by move => j; rewrite in_set1.
Qed.
Lemma sum_split (i : 'I_N) (y : {ffun 'I_N -> A}) :
\sum_(j in [set i]) (f j) (y j) +
\sum_(j in [set~ i]) (f j) (y j) = \sum_(j < N) (f j) (y j).
Proof.
have ->:
\sum_(j < N) (f j) (y j) =
\sum_(j in [predU (pred1 i) & [set~ i]]) (f j) (y j).
{ apply: congr_big => // j; rewrite /in_mem /=.
case H: (j == i).
{ by have ->: j \in pred1 i = true by rewrite /pred1 /in_mem /= H. }
have ->: j \in [set~ i] by rewrite in_setC1 H.
by rewrite orbC. }
rewrite bigU /=; last first.
{ by rewrite disjoint1 in_setC1; apply/negP; rewrite eq_refl. }
f_equal.
apply: congr_big => //; first by move => j; rewrite in_set1.
Qed.
(*END FIXME*)
Lemma neqS (T : eqType) (z i : T) : (z!=i) = (i!=z).
Proof.
apply/negP; case H: (z == i) => /=.
by move: (eqP H) => -> /=; rewrite eq_refl.
by case H2: (i == z) => //; move: (eqP H2) => H2'; rewrite H2' eq_refl in H.
Qed.
Lemma cost_vec_unfold i :
expectedValue (f i) [eta (cost_vec i)] =
expectedValue (prod_dist f) [eta (cost) i].
Proof.
rewrite /expectedValue/expectedCondValue/cost_vec.
have ->:
\sum_t
(f i) t *
[ffun a => \sum_(p : {ffun 'I_N -> A} | p i == a)
\prod_(j < N | i != j) (f j) (p j) * (cost) i p] t =
\sum_t
(\sum_(p : {ffun 'I_N -> A} | p i == t)
(f i) t * (\prod_(j < N | i != j) (f j) (p j) * (cost) i p)).
{ by apply: congr_big => // x _; rewrite ffunE // big_distrr. }
rewrite /prod_dist/=/prod_pmf.
have ->:
\sum_t [ffun p : {ffun 'I_N -> A} =>
\prod_(i0 < N) (f i0) (p i0)] t * (cost) i t =
\sum_(p : {ffun 'I_N -> A}) (\prod_(i0 < N) (f i0) (p i0)) * (cost) i p.
{ apply: congr_big => // x _; rewrite ffunE //. }
set (F t (p : {ffun 'I_N -> A}) :=
(f i) t *
(\prod_(j < N | i != j) (f j) (p j) * (cost) i p)).
set (P t (p : {ffun 'I_N -> A}) := p i == t).
change
(\sum_(t | predT t) \sum_(p : {ffun 'I_N -> A} | P t p) (F t p) =
\sum_(p : {ffun 'I_N -> A}) \prod_(i0 < N) (f i0) (p i0) * (cost) i p).
rewrite pair_big_dep /= /F.
have ->:
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | P p.1 p.2)
(f i) p.1 *
(\prod_(j < N | i != j) (f j) (p.2 j) * (cost) i p.2) =
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | P p.1 p.2)
((f i) p.1 * (\prod_(j < N | i != j) (f j) (p.2 j))) * (cost) i p.2.
{ by apply: congr_big => // x _; rewrite mulrA. }
have H:
forall p : [finType of (A * {ffun 'I_N -> A})],
P p.1 p.2 ->
(f i) p.1 * \prod_(j < N | i != j) (f j) (p.2 j) =
\prod_(j < N) (f j) (p.2 j).
{ clear - i f => [][] x y /=; set (F j := (f j) x).
have ->: (f i) x = \prod_(j < N | j == i) (F j) by rewrite big_pred1_eq.
have ->:
\prod_(j < N | j == i) F j =
\prod_(j in [set x : 'I_N | x==i]) F j.
{ by apply: congr_big => // z; rewrite in_set1. }
move => Hp; rewrite -(prod_split i); f_equal.
{ apply: congr_big => // z; rewrite in_set1 /F; move/eqP => ->.
by move: Hp; rewrite /P; move/eqP => ->. }
by apply: congr_big => // z; rewrite in_setC1 neqS. }
have ->:
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | P p.1 p.2)
(f i) p.1 * \prod_(j < N | i != j) (f j) (p.2 j) * (cost) i p.2 =
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | P p.1 p.2)
\prod_(j < N) (f j) (p.2 j) * (cost) i p.2.
{ apply: congr_big => // x Hx; rewrite H //. }
clear F.
set (G (x : {ffun 'I_N -> A}) := \prod_(j < N) (f j) (x j) * (cost) i x).
change (\sum_(p | P p.1 p.2) G p.2 =
\sum_(p : {ffun 'I_N -> A}) \prod_(i0 < N) (f i0) (p i0) * (cost) i p).
by rewrite marginal_unfold.
Qed.
End cost_vec.
Inductive server_sent_cost_vector
(i : 'I_N) (f : {ffun 'I_N -> dist A rat_realFieldType})
: machine_state -> machine_state -> Prop :=
| mkAllClientsExpCost :
forall (m m' : machine_state) c s c' s',
m.(clients) i = (c,s) ->
m'.(clients) i = (c',s') ->
c=c' ->
upto_oracle_eq s s' ->
(* acknowledge receipt of distribution *)
s'.(SOracleSt).(sent) = None ->
(* send new cost vector *)
s'.(SOracleSt).(received) = Some (cost_vec f i)->
server_sent_cost_vector i f m m'.
Inductive machine_step : machine_state -> machine_state -> Prop :=
(** Step client [i], as long as it hasn't yet sent a distribution. *)
| MSClientStep :
forall (i : 'I_N) c s c' s' (m : machine_state),
m.(clients) i = (c,s) ->
s.(SOracleSt).(sent) = None ->
client_step c s c' s' ->
machine_step
m
(@mkMachineState
(upd i (c',s') m.(clients))
m.(hist)
)
(** Once all clients have committed to a distribution,
calculate their new cost vectors and reset [sent] to None (thus
acknowledging the send). *)
| MSExpectedCost :
forall f m m',
all_clients_have_sent m f ->
(forall i, server_sent_cost_vector i f m m') ->
m'.(hist) = [:: f & m.(hist)] ->
machine_step m m'.
(** Final states are entered by running all clients to CSkip (MW
clients have all sent) but not executing a server step.
The clients' received cost vector buffers therefore remain empty. *)
Inductive final_state : machine_state -> Prop :=
| mkFinalState :
forall m : machine_state,
(forall (i : 'I_N),
exists s,
m.(clients) i = (CSkip,s) /\
(exists d, sent s.(SOracleSt) = Some d) /\
received s.(SOracleSt) = None) ->
final_state m.
Inductive machine_step_plus : machine_state -> machine_state -> Prop :=
| step1 :
forall m m',
machine_step m m' ->
machine_step_plus m m'
| step_trans :
forall m m'' m',
machine_step m m'' ->
machine_step_plus m'' m' ->
machine_step_plus m m'.
Lemma machine_step_CSkip (m m' : machine_state) i s :
machine_step m m' ->
clients m i = (CSkip,s) ->
exists s', clients m' i = (CSkip,s').
Proof.
inversion 1; subst.
{ case Heq: (i0 == i).
{ move: (eqP Heq) => Heq'; subst i0.
move => H4.
rewrite H0 in H4; inversion H4; subst; simpl.
inversion H2. }
by move => H4; simpl; exists s; rewrite /upd ffunE Heq. }
move: H2 => Hhist.
move => H4; move: (H1 i); inversion 1; subst.
rewrite H4 in H3; inversion H3; subst. clear H3.
by exists s'.
Qed.
Lemma machine_step_plus_CSkip (m m' : machine_state) i s :
machine_step_plus m m' ->
clients m i = (CSkip,s) ->
exists s', clients m' i = (CSkip,s').
Proof.
move => Hstep; move: s; induction Hstep.
{ by move => s H2; case: (machine_step_CSkip H H2) => s' H3; exists s'. }
move => s H2; case: (machine_step_CSkip H H2) => s'' H3.
by case: (IHHstep _ H3) => s' H4; exists s'.
Qed.
(* The per-client history relation *)
Inductive distHistRel :
'I_N ->
seq {ffun 'I_N -> dist A rat_realFieldType} ->
seq {ffun A -> rat} ->
Prop :=
| distHistRel_nil :
forall i : 'I_N,
distHistRel i nil nil
| distHistRel_cons :
forall (i : 'I_N) ds cs f,
distHistRel i ds cs ->
distHistRel i [:: f & ds] [:: cost_vec f i & cs].
Definition costvec_of_clientpkg (c : ClientPkg) : seq {ffun A -> rat} :=
match c.(received) with
| None => nil
| Some c => [:: c]
end.
Definition sent_of_clientpkg (c : ClientPkg) : seq (dist A rat_realFieldType) :=
match c.(sent) with
| None => nil
| Some d => [:: d]
end.
Lemma client_step_all_costs'_inv c s c' s' :
client_step c s c' s' ->
costvec_of_clientpkg s.(SOracleSt) ++ all_costs' s =
costvec_of_clientpkg s'.(SOracleSt) ++ all_costs' s'.
Proof.
induction 1; subst => //.
{ simpl; rewrite /costvec_of_clientpkg.
inversion Hrecv; subst; rewrite H H0 /all_costs' //. }
inversion H; subst; simpl.
rewrite /costvec_of_clientpkg.
inversion H2; subst => //.
Qed.
Lemma machine_step_histRel_inv m m' i :
machine_step m m' ->
(forall i,
distHistRel
i m.(hist)
(costvec_of_clientpkg (m.(clients) i).2.(SOracleSt) ++
all_costs' (m.(clients) i).2)) ->
distHistRel
i m'.(hist)
(costvec_of_clientpkg (m'.(clients) i).2.(SOracleSt) ++
all_costs' (m'.(clients) i).2).
Proof.
inversion 1; subst => /=.
{ (*client step*)
rewrite /upd ffunE; case Heq: (i0 == i) => //=.
move: (eqP Heq) => Heq'; subst i0; clear Heq.
by move/(_ i); rewrite H0 /=; rewrite (client_step_all_costs'_inv H2). }
(*server step*)
move => H3; move: (H1 i); inversion 1; subst.
move: (H3 i); rewrite H6 /= /costvec_of_clientpkg H10 /= H2 H5 /=.
move: (H0 i); rewrite H5; case => -> Hsent /= Hx.
constructor => //.
inversion H8; subst; move: Hx; rewrite /all_costs'/all_costs0/all_costs.
move: (SCostsOk s); move: (SCostsOk s').
rewrite H7 H11 => pf1 pf2.
by have ->: pf1 = pf2 by apply: proof_irrelevance.
Qed.
Lemma machine_step_plus_histRel m m' :
machine_step_plus m m' ->
(forall i,
distHistRel
i m.(hist)
(costvec_of_clientpkg (m.(clients) i).2.(SOracleSt) ++
all_costs' (m.(clients) i).2)) ->
forall i,
distHistRel
i m'.(hist)
(costvec_of_clientpkg (m'.(clients) i).2.(SOracleSt) ++
all_costs' (m'.(clients) i).2).
Proof.
induction 1; first by move => Hx i; apply: (machine_step_histRel_inv i H).
move => Hx; apply: IHmachine_step_plus.
by move => i; apply: (machine_step_histRel_inv i H).
Qed.
Inductive outHistRel :
'I_N ->
seq {ffun 'I_N -> dist A rat_realFieldType} ->
seq (dist A rat_realFieldType) ->
Prop :=
| outHistRel_nil :
forall i : 'I_N,
outHistRel i nil nil
| outHistRel_cons :
forall (i : 'I_N) f fs ds,
outHistRel i fs ds ->
outHistRel i [:: f & fs] [:: f i & ds].
Definition head_dist (l : seq (dist A rat_realFieldType)) d :=
match l with
| nil => False
| d' :: _ => d=d'
end.
Inductive machineClientHistRel :
'I_N ->
state A ClientPkg unit ->
seq {ffun 'I_N -> dist A rat_realFieldType} ->
Prop :=
| sentNone :
forall (i : 'I_N) s h,
sent s.(SOracleSt) = None ->
outHistRel i h s.(SOutputs) ->
machineClientHistRel i s h
| sentSome :
forall (i : 'I_N) s h d,
sent s.(SOracleSt) = Some d ->
head_dist s.(SOutputs) d ->
outHistRel i h (behead s.(SOutputs)) ->
machineClientHistRel i s h.
Lemma machine_step_machineClientHistRel m m' i :
machine_step m m' ->
machineClientHistRel i (m.(clients) i).2 m.(hist) ->
machineClientHistRel i (m'.(clients) i).2 m'.(hist).
Proof.
inversion 1; subst.
{ case Heq: (i0 == i).
{ move: (eqP Heq) => Heq'; subst i0; clear Heq.
inversion 1; subst; last first.
{ by rewrite H0 /= in H4; rewrite H1 in H4. }
rewrite /upd ffunE eq_refl /=.
clear H H3; rewrite H0 /= in H4 H5; clear H0.
induction H2; try solve[constructor => //].
{ constructor => //.
by inversion Hrecv; subst; rewrite H2. }
{ apply: sentSome => //.
inversion H; subst; rewrite H2 => //. }
move: (IHstep' H4 H4 H5); inversion 1; subst.
{ constructor => //. }
apply: sentSome => //.
apply: H.
apply: H0. }
by rewrite /= /upd ffunE Heq. }
inversion 1; subst.
{ by move: (H0 i); move: H4; case: (clients m i) => c s /= ->; case. }
move: (H1 i); inversion 1; subst; apply: sentNone; first by rewrite H9.
rewrite H2; rewrite H9 /=; move: (H0 i); rewrite H8 /=; case => Hx Hy.
inversion H11; subst; rewrite -H17.
rewrite H8 /= in H4 H5 H6; clear - H4 H5 H6 Hy.
move: H5 H6; case: (SOutputs _) => // a l /= <-.
by rewrite H4 in Hy; inversion Hy; subst; constructor.
Qed.
Lemma machine_step_plus_machineClientHistRel m m' :
machine_step_plus m m' ->
(forall i, machineClientHistRel i (m.(clients) i).2 m.(hist)) ->
forall i, machineClientHistRel i (m'.(clients) i).2 m'.(hist).
Proof.
induction 1; first by move => Hx i; apply: (machine_step_machineClientHistRel H).
move => Hx; apply: IHmachine_step_plus.
by move => i; apply: (machine_step_machineClientHistRel H).
Qed.
Definition ffun_of_list A (l : list A) : {ffun 'I_(size l) -> A} :=
finfun (fun i => tnth (in_tuple l) i).
Section regret.
Variable m : machine_state.
Variable pf : (0:rat) < (size m.(hist))%:R.
Definition timeAvg_fun :=
finfun (fun i : 'I_(size (hist m)) =>
prod_dist (tnth (in_tuple m.(hist)) i)).
(* The time-averaged \sigma distribution *)
Definition sigmaT : dist [finType of {ffun 'I_N -> A}] rat_realFieldType
:= timeAvg_dist pf timeAvg_fun.
(* Client i has regret at most \eps *)
Definition client_regret_eps (eps : rat) (i : 'I_N) : Prop :=
forall a : A,
expectedCost i sigmaT <=
expectedUnilateralCost i sigmaT [ffun=> a] + eps.
Definition expectedUni i a :=
expectedValue
sigmaT
(fun t => (cost) i (upd i a t)).
Lemma expectedUni_Unilateral a i :
expectedUni i a = expectedUnilateralCost i sigmaT [ffun=> a].
Proof.
rewrite /expectedUni/expectedUnilateralCost.
rewrite /expectedValue/expectedCondValue.
apply: congr_big => // x _.
rewrite /upd /games.upd; f_equal; f_equal; apply/ffunP => y.
rewrite !ffunE; case: (i == y) => //.
Qed.
Definition machine_regret_eps (eps : rat) : Prop :=
forall i : 'I_N, client_regret_eps eps i.
End regret.
(** [NOTE: Costs]:
Costs are generated by MW as:
c_(T+1) :: c_T :: ... :: c_2 :: c_1 :: c_bogus
and actions as:
d_(T+1) :: d_T :: ... :: ... :: d_1 :: d_init
Throwing away c_bogus and d_(T+1), we get:
c_(T+1) :: c_T :: ... :: c_2 :: c_1
d_T :: ... :: ... :: d_1 :: d_init *)
Fixpoint state_expCost3_aux
(A : finType)
(cs : seq {c : {ffun A -> rat} & forall a : A, `|c a| <= 1})
(ds : seq (dist A rat_realFieldType))
: rat :=
match cs, ds with
| [::], [::] => 0
| [:: c & cs'], [:: d & ds'] =>
expectedValue d [eta projT1 c] +
state_expCost3_aux cs' ds'
| _, _ => 0
end.
Lemma state_expCost13_aux cs (ds : seq (dist A rat_realFieldType)) :
(0 < size cs)%N ->
(0 < size (behead ds))%N ->
state_expCost1_aux cs ds = rat_to_R (state_expCost3_aux cs (behead ds)).
Proof.
case: cs => // c cs => _.
case: ds => // d ds; case: ds => // d' ds _.
simpl.
elim: cs c d d' ds.
{ move => c d d'; case => /=; first by rewrite rat_to_R_plus rat_to_R0.
move => _ _; rewrite rat_to_R_plus rat_to_R0 //. }
move => c' cs IH c d d'; case => /=.
{ rewrite rat_to_R_plus rat_to_R0 //. }
move => d'' ds /=; rewrite !rat_to_R_plus IH // !rat_to_R_plus //.
Qed.
Lemma state_expCost13 (s : state A ClientPkg unit) :
(0 < size (all_costs0 s))%N ->
(0 < size (behead (SOutputs s)))%N ->
state_expCost1 (all_costs0 s) s =
rat_to_R (state_expCost3_aux (all_costs0 s) (behead (SOutputs s))).
Proof.
move => H1 H2; rewrite /state_expCost1 state_expCost13_aux //.
Qed.
Lemma big_sum_index_enum T (l : list T) f :
big_sum
(index_enum (ordinal_finType (size l)))
(fun i => rat_to_R (f (tnth (in_tuple l) i))) =
big_sum
l
(fun x => rat_to_R (f x)).
Proof. rewrite -2!rat_to_R_sum; symmetry; rewrite big_tnth //. Qed.
Lemma timeAvg_fun_big_sum t i m :
rat_to_R (\sum_(i0 < size (hist m)) ((timeAvg_fun m) i0) t * (cost) i t) =
big_sum (hist m) (fun h =>
rat_to_R (prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N) h t * (cost) i t)).
Proof.
rewrite rat_to_R_sum /timeAvg_fun.
set (f c :=
[ffun i0 => prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N)
(tnth (in_tuple (hist m)) i0)] c t * (cost) i t).
set (q := index_enum (ordinal_finType (size (hist m)))).
set (h :=
(fun h : {ffun 'I_N -> dist A rat_realFieldType} =>
rat_to_R
((prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N) h) t *
(cost) i t))).
change (big_sum q (fun x => rat_to_R (f x)) = big_sum (hist m) h).
move: (big_sum_index_enum (hist m)) => H.
rewrite -/q in H; rewrite /f.
set (g (e : {ffun 'I_N -> dist A rat_realFieldType}) :=
prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N) e t * (cost) i t).
have ->:
big_sum q
(fun x : ordinal_finType (size (hist m)) =>
rat_to_R
([ffun i0 => prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N)
(tnth (in_tuple (hist m)) i0)] x t *
(cost) i t)) =
big_sum q (fun x => rat_to_R (g (tnth (in_tuple (hist m)) x))).
{ apply: big_sum_ext => // x; rewrite ffunE //. }
rewrite (H g) //.
Qed.
Lemma timeAvg_fun_big_sum' i m :
rat_to_R (\sum_(i0 < size (hist m)) (\sum_t ((timeAvg_fun m) i0) t * (cost) i t)) =
big_sum (hist m) (fun h =>
rat_to_R
(expectedValue
(prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N) h)
(fun t => (cost) i t))).
Proof.
rewrite rat_to_R_sum /timeAvg_fun /expectedValue/expectedCondValue.
symmetry; rewrite -big_sum_index_enum; apply: big_sum_ext => // x.
f_equal; apply: congr_big => // y _; rewrite ffunE //.
Qed.
Lemma state_expCost1_sigmaT i m (pf : 0 < (size (hist m))%:R) :
let: s := (m.(clients) i).2 in
(0 < size (all_costs0 s))%N ->
(0 < size (behead (SOutputs s)))%N ->
outHistRel i m.(hist) (behead s.(SOutputs)) ->
distHistRel i m.(hist) (all_costs' s) ->
Rdefinitions.Rmult
(rat_to_R (1/(size (hist m))%:R))
(state_expCost1 (all_costs0 s) s) =
rat_to_R (expectedCost i (sigmaT pf)).
Proof.
move => H H1; rewrite state_expCost13 // /sigmaT.
rewrite /expectedCost expectedValue_timeAvg'.
rewrite 3!rat_to_R_mul => H2 H3; f_equal.
rewrite timeAvg_fun_big_sum'; clear pf.
rewrite /all_costs'/all_costs0/all_costs/= in H2|-*.
destruct ((clients m) i).2; simpl in *.
rewrite /all_costs'/all_costs0 /= in H3.
destruct SOutputs; try solve[simpl in H1 => //].
simpl in H2; clear H H1.
revert H2 H3.
move: (existT _ _).
move: (hist m) SPrevCosts SOutputs; elim.
{ move => SPrev SOuts; inversion 1; subst.
destruct SPrev; try solve[inversion 1]; simpl.
by rewrite rat_to_R0. }
move => a l IH SPrev SOut; inversion 1; subst.
destruct SPrev; try solve[inversion 1]; simpl; inversion 1; subst.
rewrite rat_to_R_plus /=; f_equal.
{ by f_equal; rewrite cost_vec_unfold. }
apply: IH => //.
Qed.
Lemma sum_upd_lemma1 i i0 x (F : {ffun 'I_N -> A} -> {ffun 'I_N -> A} -> rat) :
(forall f, F (upd i x f) (upd i x f) = F f (upd i x f)) ->
\sum_(f : {ffun 'I_N -> A} | f i == i0) F f (upd i x f) =
\sum_(f : {ffun 'I_N -> A} | f i == x) F f (upd i x f).
Proof.
move => Hf; case Hneq: (i0 == x); first by move: (eqP Hneq) => <-.
rewrite -big_filter.
rewrite -[\sum_(_ | _ i == x) _ _ _]big_filter.
have ->:
\sum_(i1 <-
[seq i1 : {ffun 'I_N -> A} <- index_enum
(finfun_of_finType (ordinal_finType N) A)
| i1 i == i0]) F i1 (upd i x i1) =
\sum_(i1 <-
(map (upd i x)
[seq i1 : {ffun 'I_N -> A} <- index_enum
(finfun_of_finType (ordinal_finType N) A)
| i1 i == i0])) F i1 (upd i x i1).
{ elim: (index_enum (finfun_of_finType (ordinal_finType N) A)).
{ by rewrite !big_nil. }
move => a l IH /=; case: (a i == i0) => //=.
rewrite 2!big_cons; f_equal; last by apply: IH.
have ->: upd i x (upd i x a) = upd i x a.
{ rewrite /upd; apply/ffunP => y; rewrite !ffunE; case: (i == y) => //. }
by rewrite Hf. }
have H2:
perm_eq
(map (upd i x)
[seq i1 : {ffun 'I_N -> A} <- index_enum
(finfun_of_finType (ordinal_finType N) A)
| i1 i == i0])
([seq i1 : {ffun 'I_N -> A} <- index_enum
(finfun_of_finType (ordinal_finType N) A)
| i1 i == x]).
{ apply: uniq_perm_eq.
{ rewrite map_inj_in_uniq; first by apply: enum_uniq.
move => y z; rewrite 2!mem_filter; case/andP=> H1 H2; case/andP=> H3 H4.
move: (eqP H1) => H1'; subst i0; move: (eqP H3) => H3'.
move/ffunP => H5; apply/ffunP => q; move: (H5 q); rewrite !ffunE.
case H6: (i == q) => //.
move: (eqP H6) => H6'; subst q => //. }
{ rewrite filter_index_enum.
apply: enum_uniq. }
move =>y; apply/mapP; case H2: (y \in _).
{ rewrite mem_filter in H2.
case: (andP H2); move/eqP => H3 H4; clear H2.
exists (upd i i0 y).
{ rewrite mem_filter; apply/andP; split; first by rewrite ffunE eq_refl.
apply: mem_index_enum. }
have ->: upd i x (upd i i0 y) = upd i x y.
{ apply/ffunP => q; rewrite !ffunE; case: (i == q) => //. }
apply/ffunP => q; rewrite ffunE; case H: (i == q) => //.
{ move: (eqP H) => Heq; subst q => //. }}
case => z.
move => H3 H4; subst y.
rewrite mem_filter in H3; case: (andP H3); move/eqP => H4 H5; clear H3; subst i0.
rewrite mem_filter in H2; move: H2; rewrite andb_false_iff; case.
{ rewrite ffunE eq_refl eq_refl //. }
by rewrite mem_index_enum. }
apply: eq_big_perm => //.
Qed.
Lemma sum_upd_lemma2 i i0 x (F : {ffun 'I_N -> A} -> {ffun 'I_N -> A} -> rat) :
(forall f, F (upd i x f) (upd i x f) = F f (upd i x f)) ->
\sum_(f : {ffun 'I_N -> A} | f i == i0) (F f (upd i x f)) =
\sum_(f : {ffun 'I_N -> A} | f i == x) (F f f).
Proof.
move => Hf; rewrite sum_upd_lemma1 //.
apply: congr_big => // z; move/eqP => H; rewrite /upd -H; f_equal.
by apply/ffunP => y; rewrite ffunE; case H2: (i == y) => //; move: (eqP H2) ->.
Qed.
Lemma cost_vec_unfold2 a i x :
(cost_vec a i) x =
\sum_i0
(prod_pmf (T:=A) (rty:=rat_realFieldType) (n:=N) a) i0 *
(cost) i (upd i x i0).
Proof.
rewrite /prod_pmf.
have ->:
\sum_i0
[ffun p : {ffun 'I_N -> A} =>
\prod_(i1 < N) (a i1) (p i1)] i0 * (cost) i (upd i x i0) =
\sum_(i0 : {ffun 'I_N -> A})
(\prod_(i1 < N) (a i1) (i0 i1)) * (cost) i (upd i x i0).
{ apply: congr_big => // y _; rewrite ffunE //. }
rewrite /cost_vec ffunE /expUnilateral.
set (F (i0 : {ffun 'I_N -> A}) :=
\prod_(i1 < N) (a i1) (i0 i1) * (cost) i (upd i x i0)).
change
(\sum_(p : {ffun 'I_N -> A} | p i == x)
\prod_(j < N | i != j) (a j) (p j) * (cost) i p =
\sum_(i0 : {ffun 'I_N -> A}) F i0).
rewrite -(marginal_unfold F i) /F.
set (Q (x : A) (y : {ffun 'I_N -> A}) := y i == x).
have ->:
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | p.2 i == p.1)
\prod_(i1 < N) (a i1) (p.2 i1) * (cost) i (upd i x p.2) =
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | predT p.1 && (Q p.1 p.2))
\prod_(i1 < N) (a i1) (p.2 i1) * (cost) i (upd i x p.2).
{ apply: congr_big => //. }
clear F.
set (F (p1:A) (p2:{ffun 'I_N -> A}) :=
\prod_(i1 < N) (a i1) (p2 i1) * (cost) i (upd i x p2)).
change
(\sum_(p : {ffun 'I_N -> A} | p i == x)
\prod_(j < N | i != j) (a j) (p j) * (cost) i p =
\sum_(p : [finType of (A * {ffun 'I_N -> A})] | predT p.1 && (Q p.1 p.2))
F p.1 p.2).
rewrite -pair_big_dep /= /F /Q.
have ->:
\sum_i0 \sum_(j : {ffun 'I_N -> A} | j i == i0)
\prod_(i1 < N) (a i1) (j i1) * (cost) i (upd i x j) =
\sum_i0 \sum_(j : {ffun 'I_N -> A} | j i == i0)
(\prod_(i1 in [set i]) (a i1) (j i1) * \prod_(i1 in [set~ i]) (a i1) (j i1)) *
(cost) i (upd i x j).
{ apply: congr_big => // z _; apply: congr_big => // q; move/eqP => Heq.
rewrite -(prod_split a i) //. }
have ->:
\sum_i0 \sum_(j : {ffun 'I_N -> A} | j i == i0)
(\prod_(i1 in [set i]) (a i1) (j i1) * \prod_(i1 in [set~ i]) (a i1) (j i1)) *
(cost) i (upd i x j) =
\sum_i0 \sum_(j : {ffun 'I_N -> A} | j i == i0)
(\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j)) * a i i0.
{ apply: congr_big => // z _; apply: congr_big => // q; move/eqP => Heq.
rewrite [_ * (a i) z]mulrC mulrA; f_equal; f_equal.
{ by rewrite big_set1 Heq. }
by apply: congr_big => // r; rewrite in_setC1 neqS. }
have ->:
\sum_i0
\sum_(j : {ffun 'I_N -> A}| j i == i0)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j) * (a i) i0 =
\sum_i0
((a i) i0) *
\sum_(j : {ffun 'I_N -> A}| j i == i0)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j).
{ apply: congr_big => // z _.
rewrite -mulr_suml mulrC //. }
have H:
forall i0,
\sum_(j : {ffun 'I_N -> A} | j i == i0)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j) =
\sum_(j : {ffun 'I_N -> A} | j i == x)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i j.
{ move => ix.
set (G (j0 j:{ffun 'I_N -> A}) :=
\prod_(i1 < N | i != i1) (a i1) (j0 i1) * (cost) i j).
have ->:
\sum_(j:{ffun 'I_N -> A} | j i == ix)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j) =
\sum_(j:{ffun 'I_N -> A} | j i == ix) (G j (upd i x j)) by apply: congr_big.
rewrite sum_upd_lemma2 //.
move => f; rewrite /G; clear - f.
set (F1 i1 := (a i1) ((upd i x f) i1) * (cost) i (upd i x f)).
set (F2 i1 := (a i1) (f i1) * (cost) i (upd i x f)).
f_equal.
apply: eq_big => // y; rewrite /upd ffunE; case: (i == y) => //. }
have ->:
\sum_i0
((a i) i0) *
(\sum_(j : {ffun 'I_N -> A}| j i == i0)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i (upd i x j)) =
\sum_i0
((a i) i0) *
(\sum_(j : {ffun 'I_N -> A}| j i == x)
\prod_(i1 < N | i != i1) (a i1) (j i1) * (cost) i j).
{ apply: congr_big => // z _; f_equal; rewrite H //. }
rewrite -mulr_suml dist_normalized mul1r //.
Qed.
Lemma OPT_sigmaT_min i m (pf : 0 < (size (hist m))%:R) :
let: s := (m.(clients) i).2 in
(0 < size (all_costs0 s))%N ->
(0 < size (behead (SOutputs s)))%N ->
outHistRel i m.(hist) (behead s.(SOutputs)) ->
distHistRel i m.(hist) (all_costs' s) ->
OPTR a0 s =
rat_to_R ((size (hist m))%:R * extrema.min xpredT (expectedUni pf i) a0).
Proof.
move => H H1 H2 H3.
rewrite /OPTR/OPT/astar/best_action.
rewrite /extrema.min.
have ->:
extrema.arg_min xpredT
(fun a : A => \sum_(c0 <- all_costs' ((clients m) i).2) c0 a) a0 =
extrema.arg_min xpredT (expectedUni (m:=m) pf i) a0.
{ have Heq:
(fun a : A =>
1 / (size (hist m))%:R *
\sum_(c0 <- all_costs' ((clients m) i).2) c0 a) =1
(expectedUni (m:=m) pf i).
{ move => x; rewrite /expectedUni /sigmaT.
rewrite expectedValue_timeAvg; f_equal.
rewrite exchange_big /=.
rewrite /timeAvg_fun.
set (F x j :=
\sum_i0
(prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N) j i0) *
(cost) i (upd i x i0)).
have ->:
\sum_(j < size (hist m))
\sum_i0
[ffun i1 => prod_dist (T:=A) (rty:=rat_realFieldType) (n:=N)
(tnth (in_tuple (hist m)) i1)] j i0 *
(cost) i (upd i x i0) =
\sum_(j < size (hist m) | predT (tnth (in_tuple (hist m)) j))
(F x (tnth (in_tuple (hist m)) j)).
{ apply: congr_big => // y _; rewrite ffunE //. }
rewrite -big_tnth /F.
rewrite /all_costs'/all_costs0/all_costs/= in H2|-*.
destruct ((clients m) i).2; simpl in *.
rewrite /all_costs'/all_costs0 /= in H3.
destruct SOutputs; try solve[simpl in H1 => //].
simpl in H2; clear H H1.
revert H2 H3.
move: (existT _ _).
move: (hist m) x SPrevCosts SOutputs; elim.
{ move => x SPrev SOuts; inversion 1; subst.
inversion 1; subst; simpl.
by rewrite !big_nil. }
move => a l IH x SPrev SOut; inversion 1; subst.
inversion 1; subst.
rewrite !big_cons /=; f_equal.
{ apply: cost_vec_unfold2. }
destruct SPrev; try solve[inversion H6].
simpl in H6.
inversion H6; subst. clear H6.
destruct s0; simpl in *.
apply: IH => //.
apply: H4. }
have Hc : ((0 : rat) < 1 / (size (hist m))%:R).
{ apply: mulr_gt0 => //.
rewrite invr_gt0 //. }
rewrite -(extrema.arg_min_const _ _ _ Hc).
apply: extrema.arg_min_ext => //.
}
move: (extrema.arg_min _ _ _) => x.
rewrite rat_to_R_mul rat_to_R_sum /expectedUni /sigmaT.
rewrite expectedValue_timeAvg rat_to_R_mul exchange_big /= rat_to_R_sum.
rewrite -Reals.Raxioms.Rmult_assoc.
have ->:
(Rdefinitions.Rmult
(rat_to_R (size (hist m))%:R)
(rat_to_R (1 / (size (hist m))%:R)) = 1%R).
{ rewrite rat_to_R_mul rat_to_R1 Reals.Raxioms.Rmult_1_l.
have Hr: (rat_to_R (size (hist m))%:R <> 0%R).
{ case: (size (hist m)) pf => // n _.
rewrite mulrS rat_to_R_plus rat_to_R1.
move => H4; clear - H4.
apply RIneq.Rplus_opp_r_uniq in H4.
apply: rat_to_R_opp_neq; apply: H4. }
rewrite rat_to_R_inv.
{ move: (rat_to_R (size (hist m))%:R) Hr => r.
apply: RIneq.Rinv_r. }
move: Hr; move: (size _) => n Hr.
by apply/negP; move/eqP => Heq; rewrite Heq rat_to_R0 in Hr. }
rewrite Reals.Raxioms.Rmult_1_l.
clear pf.
rewrite /all_costs'/all_costs0/all_costs/= in H2|-*.
destruct ((clients m) i).2; simpl in *.
rewrite /all_costs'/all_costs0 /= in H3.
destruct SOutputs; try solve[simpl in H1 => //].
simpl in H2; clear H H1.
revert H2 H3.
move: (existT _ _).
rewrite /timeAvg_fun.
set (F c :=