-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil_solvers.py
1578 lines (1201 loc) · 50.7 KB
/
util_solvers.py
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
import json, multiprocessing, queue, random, re, sys
import util_common
SOLVER_JSON_WRITE = 'json-write'
SOLVER_DIMACS_WRITE_CNF = 'dimacs-write-cnf'
SOLVER_DIMACS_WRITE_WCNF = 'dimacs-write-wcnf'
SOLVER_DIMACS_READ = 'dimacs-read'
SOLVER_Z3_OPTIMIZE = 'z3-opt'
SOLVER_Z3_SOLVE = 'z3-slv'
SOLVER_CVC5 = 'cvc5'
SOLVER_SCIPY = 'scipy'
SOLVER_CVXPY = 'cvxpy'
SOLVER_CLINGO_FE = 'clingo-fe'
SOLVER_CLINGO_BE = 'clingo-be'
SOLVER_PYSAT_FM = 'pysat-fm'
SOLVER_PYSAT_RC2 = 'pysat-rc2'
SOLVER_PYSAT_FM_BOOL = 'pysat-fm-boolonly'
SOLVER_PYSAT_RC2_BOOL = 'pysat-rc2-boolonly'
SOLVER_PYSAT_MC = 'pysat-minicard'
SOLVER_LIST = [SOLVER_JSON_WRITE, SOLVER_DIMACS_WRITE_CNF, SOLVER_DIMACS_WRITE_WCNF, SOLVER_DIMACS_READ, SOLVER_Z3_OPTIMIZE, SOLVER_Z3_SOLVE, SOLVER_CVC5, SOLVER_SCIPY, SOLVER_CVXPY, SOLVER_CLINGO_FE, SOLVER_CLINGO_BE, SOLVER_PYSAT_FM, SOLVER_PYSAT_RC2, SOLVER_PYSAT_FM_BOOL, SOLVER_PYSAT_RC2_BOOL, SOLVER_PYSAT_MC]
SOLVER_NOTEST_LIST = [SOLVER_JSON_WRITE, SOLVER_DIMACS_WRITE_CNF, SOLVER_DIMACS_WRITE_WCNF, SOLVER_DIMACS_READ]
def try_import_z3():
global z3
try:
import z3
return True
except ImportError:
z3 = None
del z3
return False
def try_import_cvc5():
global cvc5
try:
import cvc5.pythonic
return True
except ImportError:
cvc5 = None
del cvc5
return False
def try_import_scipy():
global numpy
global scipy
try:
import numpy
import scipy
return True
except ImportError:
numpy = None
del numpy
scipy = None
del scipy
return False
def try_import_cvxpy():
global numpy
global cvxpy
try:
import numpy
import cvxpy
return True
except ImportError:
numpy = None
del numpy
cvxpy = None
del cvxpy
return False
def try_import_clingo():
global clingo
try:
import clingo
return True
except ImportError:
clingo = None
del clingo
return False
def try_import_pysat():
global pysat
try:
import pysat.card
import pysat.formula
import pysat.examples.fm
import pysat.examples.rc2
import pysat.solvers
return True
except ImportError:
pysat = None
del pysat
return False
def solver_id_to_solver(solver_id):
if solver_id == SOLVER_JSON_WRITE:
return WriteJsonSolver()
elif solver_id == SOLVER_DIMACS_WRITE_CNF:
return CnfWriteDimacsSolver()
elif solver_id == SOLVER_DIMACS_WRITE_WCNF:
return WcnfWriteDimacsSolver()
elif solver_id == SOLVER_DIMACS_READ:
return ReadDimacsSolver()
elif solver_id == SOLVER_Z3_OPTIMIZE:
return OptimizeZ3Solver()
elif solver_id == SOLVER_Z3_SOLVE:
return SolveZ3Solver()
elif solver_id == SOLVER_CVC5:
return CVC5Solver()
elif solver_id == SOLVER_SCIPY:
return SciPySolver()
elif solver_id == SOLVER_CVXPY:
return CvxPySolver()
elif solver_id == SOLVER_CLINGO_FE:
return FrontendClingoSolver()
elif solver_id == SOLVER_CLINGO_BE:
return BackendClingoSolver()
elif solver_id == SOLVER_PYSAT_FM:
return FMPySatSolver()
elif solver_id == SOLVER_PYSAT_RC2:
return RC2PySatSolver()
elif solver_id == SOLVER_PYSAT_FM_BOOL:
return BoolOnlyFMPySatSolver()
elif solver_id == SOLVER_PYSAT_RC2_BOOL:
return BoolOnlyRC2PySatSolver()
elif solver_id == SOLVER_PYSAT_MC:
return MiniCardPySatSolver()
else:
util_common.check(False, 'solver ' + solver_id + ' unrecognized.')
def solver_takes_filename(solver):
return isinstance(solver, _SolverFilename)
def get_one_set(solver, vv_map):
set_val, found = None, False
for val, vv in vv_map.items():
if solver.get_var(vv):
util_common.check(not found, 'multiple values')
set_val, found = val, True
util_common.check(found, 'no value')
return set_val
def are_all_set(solver, vvs):
util_common.check(len(vvs) != 0, 'no vars to check')
for vv in vvs:
if not solver.get_var(vv):
return False
return True
class _Solver:
def __init__(self, solver_id):
self._solver_id = solver_id
def get_id(self):
return self._solver_id
def make_var(self):
util_common.check(False, 'unimplemented')
def make_conj(self, vvs, settings):
util_common.check(False, 'unimplemented')
def cnstr_implies_disj(self, in_vv, in_vv_setting, out_vvs, out_vv_settings, weight):
util_common.check(False, 'unimplemented')
def cnstr_count(self, vvs, settings, lo, hi, weight):
util_common.check(False, 'unimplemented')
def solve(self):
util_common.check(False, 'unimplemented')
def get_var(self, vv):
util_common.check(False, 'unimplemented')
def get_objective(self):
util_common.check(False, 'unimplemented')
def get_solver_id_used(self):
util_common.check(False, 'unimplemented')
def supports_weights(self):
return False
def supports_xforms(self):
return False
def _wrap_var(vv):
return ('v', vv)
def _wrap_conj(vv):
return ('c', vv)
def _is_var(vv):
return vv[0] == 'v'
def _is_conj(vv):
return vv[0] == 'c'
def _unwrap_var(vv):
util_common.check(type(vv) == tuple and len(vv) == 2 and vv[0] == 'v', 'unwrap')
return vv[1]
def _unwrap_conj(vv):
util_common.check(type(vv) == tuple and len(vv) == 2 and vv[0] == 'c', 'unwrap')
return vv[1]
def _unwrap_any(vv):
util_common.check(type(vv) == tuple and len(vv) == 2 and vv[0] in ['v', 'c'], 'unwrap')
return vv[1]
def _unwrap_lit_lconj(vv, setting, negate_func):
vv = _unwrap_any(vv)
if not setting:
vv = negate_func(vv)
return vv
def _unwrap_lit_lconjs(vvs, settings, negate_func):
if settings in [True, False]:
settings = [settings for vv in vvs]
else:
util_common.check(len(vvs) == len(settings), 'different vvs and settings lengths')
return [_unwrap_lit_lconj(vv, setting, negate_func) for vv, setting in zip(vvs, settings)]
def _is_xform_t(vv):
return vv[0] in ['t2', 't3']
def _unwrap_xform_t(vv):
util_common.check(type(vv) == tuple and vv[0] in ['t2', 't3'], 'unwrap')
return vv[1:]
def _is_xform_x2(vv):
return vv[0] in ['x2']
def _unwrap_xform_x2(vv):
util_common.check(type(vv) == tuple and vv[0] in ['x2'], 'unwrap')
return vv[1:]
class _SolverImpl(_Solver):
def __init__(self, solver_id, supports_weights, supports_xforms):
super().__init__(solver_id)
self._supports_weights = supports_weights
self._supports_xforms = supports_xforms
self._result = None
self._objective = None
def make_var(self):
return _wrap_var(self._IMPL_make_var())
def make_conj(self, vvs, settings):
util_common.check(len(vvs) > 0, 'empty conj')
return _wrap_conj(self._IMPL_make_conj(_unwrap_lit_lconjs(vvs, settings, self._IMPL_negate_var_conj)))
def cnstr_implies_disj(self, in_vv, in_vv_setting, out_vvs, out_vv_settings, weight):
if not self._supports_weights:
util_common.check(weight is None, 'solver does not support weights')
else:
util_common.check(weight is None or type(weight) == int, 'weights must be integers')
return self._IMPL_cnstr_implies_disj(_unwrap_lit_lconj(in_vv, in_vv_setting, self._IMPL_negate_var_conj), _unwrap_lit_lconjs(out_vvs, out_vv_settings, self._IMPL_negate_var_conj_for_implies_out), weight)
def cnstr_count(self, vvs, settings, lo, hi, weight):
util_common.check(0 <= lo and lo <= hi and hi <= len(vvs), 'count')
if not self._supports_weights:
util_common.check(weight is None, 'solver does not support weights')
else:
util_common.check(weight is None or type(weight) == int, 'weights must be integers')
return self._IMPL_cnstr_count(_unwrap_lit_lconjs(vvs, settings, self._IMPL_negate_var_conj), lo, hi, weight)
def solve(self):
return self._IMPL_solve()
def get_var(self, vv):
return self._IMPL_get_var(_unwrap_var(vv))
def get_objective(self):
return self._objective
def get_solver_id_used(self):
return self.get_id()
def supports_weights(self):
return self._supports_weights
def supports_xforms(self):
return self._supports_xforms
def _IMPL_negate_var_conj_for_implies_out(self, ll):
return self._IMPL_negate_var_conj(ll)
def _IMPL_negate_var_conj(self, ll):
util_common.check(False, 'unimplemented')
def _IMPL_make_var(self):
util_common.check(False, 'unimplemented')
def _IMPL_make_conj(self, lls):
util_common.check(False, 'unimplemented')
def _IMPL_cnstr_implies_disj(self, ll_in, lls_out, weight):
util_common.check(False, 'unimplemented')
def _IMPL_cnstr_count(self, lls, lo, hi, weight):
util_common.check(False, 'unimplemented')
def _IMPL_solve(self):
util_common.check(False, 'unimplemented')
def _IMPL_get_var(self, vv):
util_common.check(False, 'unimplemented')
class _SolverFilename:
def __init__(self):
self._filename = None
def set_filename(self, filename):
self._filename = filename
def file_write(self, data):
if self._filename is None:
print(data)
else:
print('writing to', self._filename)
with open(self._filename, 'wt') as f:
f.write(data)
def file_read(self):
if self._filename is None:
return sys.stdin.read()
else:
print('reading from', self._filename)
with open(self._filename, 'rt') as f:
return f.read()
class PortfolioSolver(_Solver):
RES_SOLN = 'soln'
RES_NOSOLN = 'nosoln'
RES_ERROR = 'error'
def __init__(self, solver_ids, timeout):
super().__init__('portfolio:' + ';'.join(solver_ids))
self._solver_ids = solver_ids
self._timeout = timeout
self._solver_var_conjs = []
self._solver_commands = []
self._result = None
self._objective = None
self._solver_id_used = None
def make_var(self):
self._solver_var_conjs.append(_wrap_var(None))
return len(self._solver_var_conjs) - 1
def make_conj(self, vvs, settings):
util_common.check(len(vvs) > 0, 'empty conj')
self._solver_var_conjs.append(_wrap_conj((tuple(vvs), settings)))
return len(self._solver_var_conjs) - 1
def cnstr_implies_disj(self, in_vv, in_vv_setting, out_vvs, out_vv_settings, weight):
util_common.check(weight is None or type(weight) == int, 'weights must be integers')
self._solver_commands.append(('cnstr_implies_disj', (in_vv, in_vv_setting, out_vvs, out_vv_settings, weight)))
def cnstr_count(self, vvs, settings, lo, hi, weight):
util_common.check(0 <= lo and lo <= hi and hi <= len(vvs), 'count')
util_common.check(weight is None or type(weight) == int, 'weights must be integers')
self._solver_commands.append(('cnstr_count', (vvs, settings, lo, hi, weight)))
def solve(self):
q = multiprocessing.Queue()
procs = [(multiprocessing.Process(target=PortfolioSolver.run_solver, args=(q, index, solver_id, self._solver_var_conjs, self._solver_commands))) for (index, solver_id) in enumerate(self._solver_ids)]
for proc in procs:
proc.start()
result = None
try:
result = q.get(timeout=self._timeout)
except queue.Empty:
util_common.write_portfolio('portfolio timeout\n')
for proc in procs:
proc.kill()
if result[0] == PortfolioSolver.RES_SOLN:
index, self._result, self._objective = result[1]
self._solver_id_used = self._solver_ids[index]
util_common.write_portfolio('portfolio using %d %s\n' % (index, self._solver_id_used))
return True
elif result[0] == PortfolioSolver.RES_NOSOLN:
return False
elif result[0] == PortfolioSolver.RES_ERROR:
err = result[1]
raise err
else:
util.check(False, 'unexpected result')
def get_var(self, vv):
return self._result[vv]
def get_objective(self):
return self._objective
def get_solver_id_used(self):
return self._solver_id_used
@staticmethod
def run_solver(s_q, s_index, s_solver_id, s_solver_var_conjs, s_solver_commands):
s_ret = (PortfolioSolver.RES_NOSOLN,)
try:
util_common.write_portfolio('portfolio starting %d %s\n' % (s_index, s_solver_id))
s_solver = solver_id_to_solver(s_solver_id)
s_var_conj_map = {}
def translate_vars(_vvv):
nonlocal s_var_conj_map
if type(_vvv) in [list, tuple]:
return [s_var_conj_map[_vv] for _vv in _vvv]
else:
return s_var_conj_map[_vvv]
s_vars_inds = []
s_conjs_inds = []
for s_ind, s_var_conj in enumerate(s_solver_var_conjs):
if _is_var(s_var_conj):
s_vars_inds.append(s_ind)
elif _is_conj(s_var_conj):
s_conjs_inds.append(s_ind)
else:
util_common.check(False, 'var_conj')
random.Random(s_index).shuffle(s_vars_inds)
for s_ind in s_vars_inds:
util_common.check(_is_var(s_solver_var_conjs[s_ind]), 'var')
s_var_conj_map[s_ind] = s_solver.make_var()
for s_ind in s_conjs_inds:
util_common.check(_is_conj(s_solver_var_conjs[s_ind]), 'conj')
s_info = _unwrap_conj(s_solver_var_conjs[s_ind])
s_var_conj_map[s_ind] = s_solver.make_conj(translate_vars(s_info[0]), s_info[1])
for s_func_name, s_args in s_solver_commands:
if s_func_name == 'cnstr_implies_disj':
s_solver.cnstr_implies_disj(translate_vars(s_args[0]), s_args[1], translate_vars(s_args[2]), s_args[3], s_args[4])
elif s_func_name == 'cnstr_count':
s_solver.cnstr_count(translate_vars(s_args[0]), s_args[1], s_args[2], s_args[3], s_args[4])
else:
util_common.check(False, 's_func_name')
if s_solver.solve():
s_vars_set = {}
for s_ind, s_var_conj in enumerate(s_solver_var_conjs):
if _is_var(s_var_conj):
s_vars_set[s_ind] = s_solver.get_var(translate_vars(s_ind))
s_ret = (PortfolioSolver.RES_SOLN, (s_index, s_vars_set, s_solver.get_objective()))
util_common.write_portfolio('portfolio finishing %d %s\n' % (s_index, s_solver_id))
except Exception as e:
s_ret = (PortfolioSolver.RES_ERROR, e)
util_common.write_portfolio('portfolio error %d %s %s\n' % (s_index, s_solver_id, e))
s_q.put(s_ret)
class WriteJsonSolver(_SolverImpl, _SolverFilename):
def __init__(self):
_SolverImpl.__init__(self, SOLVER_JSON_WRITE, True, False)
_SolverFilename.__init__(self)
self._curr_id = 0
self._output = {}
self._output['var'] = []
self._output['conj'] = []
self._output['cnstr_implies_disj'] = []
self._output['cnstr_count'] = []
def _IMPL_negate_var_conj(self, ll):
return '~' + ll
def _IMPL_make_var(self):
self._curr_id += 1
ret = 'v%d' % self._curr_id
self._output['var'].append({'id':ret})
return ret
def _IMPL_make_conj(self, lls):
self._curr_id += 1
ret = 'c%d' % self._curr_id
self._output['conj'].append({'id':ret, 'of':lls})
return ret
def _IMPL_cnstr_implies_disj(self, in_ll, out_lls, weight):
print_weight = weight if weight is not None else 0
self._output['cnstr_implies_disj'].append({'if':in_ll, 'then':out_lls, 'weight':print_weight})
def _IMPL_cnstr_count(self, lls, lo, hi, weight):
print_weight = weight if weight is not None else 0
self._output['cnstr_count'].append({'of':lls, 'min':lo, 'max':hi, 'weight':print_weight})
def _IMPL_solve(self):
self.file_write(json.dumps(self._output, indent=2) + '\n')
return False
class _Z3Solver(_SolverImpl):
Z3_OPTION_SOLVE = 'solve'
Z3_OPTION_OPTIMIZE = 'optimize'
def __init__(self, solver_id, solver_option):
util_common.check(try_import_z3(), 'z3 not available')
util_common.check(solver_option in [_Z3Solver.Z3_OPTION_OPTIMIZE, _Z3Solver.Z3_OPTION_SOLVE], 'invalid option for solver: ' + solver_option)
self._option = solver_option
if self._option == _Z3Solver.Z3_OPTION_OPTIMIZE:
super().__init__(solver_id, True, True)
self._s = z3.Optimize()
else:
super().__init__(solver_id, False, True)
self._s = z3.Solver()
def _help_add_cnstr_weight(self, cnstr, weight):
if weight is None:
self._s.add(cnstr)
else:
self._s.add_soft(cnstr, weight)
def _IMPL_negate_var_conj(self, ll):
return z3.Not(ll)
def _IMPL_make_var(self):
return z3.FreshBool()
def _IMPL_make_conj(self, lls):
if len(lls) == 1:
return lls[0]
else:
return z3.And(*lls)
def _IMPL_cnstr_implies_disj(self, in_ll, out_lls, weight):
if self._option != _Z3Solver.Z3_OPTION_OPTIMIZE:
util_common.check(weight is None, 'solver does not support weights')
self._help_add_cnstr_weight(z3.Implies(in_ll, z3.Or(*out_lls)), weight)
def _IMPL_cnstr_count(self, lls, lo, hi, weight):
if self._option != _Z3Solver.Z3_OPTION_OPTIMIZE:
util_common.check(weight is None, 'solver does not support weights')
if len(lls) == 0:
pass
elif len(lls) == 1:
if lo == 0 and hi == 1:
pass
elif lo == 0 and hi == 0:
self._help_add_cnstr_weight(z3.Not(lls[0]), weight)
elif lo == 1 and hi == 1:
self._help_add_cnstr_weight(lls[0], weight)
else:
util_common.check(False, 'count vars')
else:
lls_count = [(vv, 1) for vv in lls]
if lo == hi:
self._help_add_cnstr_weight(z3.PbEq(lls_count, lo), weight)
else:
if lo == 0:
pass
elif lo == 1:
self._help_add_cnstr_weight(z3.Or(lls), weight)
else:
self._help_add_cnstr_weight(z3.PbGe(lls_count, lo), weight)
if hi < len(lls):
self._help_add_cnstr_weight(z3.PbLe(lls_count, hi), weight)
def _IMPL_solve(self):
if self._option == _Z3Solver.Z3_OPTION_OPTIMIZE:
def on_model(_m):
util_common.write_time('.')
self._s.set_on_model(on_model)
chk = self._s.check()
util_common.write_time('\n')
util_common.write_time(str(chk) + '\n')
if chk == z3.unsat:
return False
if chk == z3.unknown:
util_common.write_time(str(self._s.reason_unknown()) + '\n')
return False
self._result = self._s.model()
if self._option == _Z3Solver.Z3_OPTION_OPTIMIZE:
objs = [self._s.model().evaluate(obj) for obj in self._s.objectives()]
else:
objs = []
if len(objs) == 0:
self._objective = 0
else:
util_common.check(len(objs) == 1, 'cost length')
self._objective = objs[0].as_long()
return True
def _IMPL_get_var(self, vv):
return bool(self._result[vv])
def make_var_xform(self, dims):
if dims == 2:
return ('t2', z3.FreshReal(), z3.FreshReal())
elif dims == 3:
return ('t3', z3.FreshReal(), z3.FreshReal(), z3.FreshReal())
elif dims == 'x2':
return ('x2', z3.FreshReal(), z3.FreshReal(), z3.FreshReal(), z3.FreshReal(), z3.FreshReal(), z3.FreshReal())
def get_var_pos_xform(self, vv):
if _is_xform_t(vv):
vv = _unwrap_xform_t(vv)
return tuple([self._result[ee].numerator_as_long() / self._result[ee].denominator_as_long() for ee in vv])
elif _is_xform_x2(vv):
vv = _unwrap_xform_x2(vv)
return tuple([self._result[vv[2]].numerator_as_long() / self._result[vv[2]].denominator_as_long(),
self._result[vv[5]].numerator_as_long() / self._result[vv[5]].denominator_as_long()])
def _xform_type(self, xforms):
xtypes = dict.fromkeys([vv[0] for vv in xforms])
util_common.check(len(xtypes) == 1, 'xform types')
xtype = next(iter(xtypes))
util_common.check(xtype in ['t2', 't3', 'x2'], 'xform types')
return xtype
def _xform_xtype_t(self, xtype):
return xtype in ['t2', 't3']
def cnstr_ident_dist_xform(self, missing_conds, xforms, minlinfdist):
def far(_p0, _p1):
return z3.Or([z3.Or(_i0 - _i1 <= -minlinfdist, _i0 - _i1 >= minlinfdist) for _i0, _i1 in zip(_p0, _p1)])
missing_conds = [_unwrap_var(vv) for vv in missing_conds]
util_common.check(len(missing_conds) == len(xforms), 'lengths')
if self._xform_xtype_t(self._xform_type(xforms)):
xforms = [_unwrap_xform_t(vv) for vv in xforms]
self._s.add(z3.Not(missing_conds[0]))
self._s.add(z3.And([p0 == 0 for p0 in xforms[0]]))
for ii in range(len(xforms)):
for jj in range(ii + 1, len(xforms)):
self._s.add(z3.Implies(z3.And(z3.Not(missing_conds[ii]), z3.Not(missing_conds[jj])),
far(xforms[ii], xforms[jj])))
else:
xforms = [_unwrap_xform_x2(vv) for vv in xforms]
self._s.add(z3.Not(missing_conds[0]))
self._s.add(z3.And(xforms[0][0] == 1, xforms[0][1] == 0, xforms[0][2] == 0,
xforms[0][3] == 0, xforms[0][4] == 1, xforms[0][5] == 0))
for ii in range(len(xforms)):
for jj in range(ii + 1, len(xforms)):
self._s.add(z3.Implies(z3.And(z3.Not(missing_conds[ii]), z3.Not(missing_conds[jj])),
far([xforms[ii][2], xforms[ii][5]], [xforms[jj][2], xforms[jj][5]])))
def cnstr_implies_xform(self, cond, xform0, xform1, dxform, primary):
def close(_p0, _p1):
return z3.And([z3.And(_i0 - _i1 >= -0.01, _i0 - _i1 <= 0.01) for _i0, _i1 in zip(_p0, _p1)])
if self._xform_xtype_t(self._xform_type([xform0, xform1])):
xform0 = _unwrap_xform_t(xform0)
xform1 = _unwrap_xform_t(xform1)
util_common.check(len(dxform) == len(xform0) == len(xform1), 'dims')
if primary:
xform_apply = z3.And([p1 == p0 + dd for dd, p0, p1 in zip(dxform, xform0, xform1)])
else:
xform_apply = close(xform1, [p0 + dd for dd, p0 in zip(dxform, xform0)])
else:
xform0 = _unwrap_xform_x2(xform0)
xform1 = _unwrap_xform_x2(xform1)
util_common.check(6 == len(dxform) == len(xform0) == len(xform1), 'dims')
if primary:
xform_apply = [xform1[0] == (xform0[0] * dxform[0] + xform0[1] * dxform[3]),
xform1[1] == (xform0[0] * dxform[1] + xform0[1] * dxform[4]),
xform1[2] == (xform0[0] * dxform[2] + xform0[1] * dxform[5] + xform0[2]),
xform1[3] == (xform0[3] * dxform[0] + xform0[4] * dxform[3]),
xform1[4] == (xform0[3] * dxform[1] + xform0[4] * dxform[4]),
xform1[5] == (xform0[3] * dxform[2] + xform0[4] * dxform[5] + xform0[5])]
else:
xform_apply = [close([xform1[2], xform1[5]],
[xform0[0] * dxform[2] + xform0[1] * dxform[5] + xform0[2],
xform0[3] * dxform[2] + xform0[4] * dxform[5] + xform0[5]])]
cond = _unwrap_var(cond)
self._s.add(z3.Implies(cond, z3.And(xform_apply)))
class OptimizeZ3Solver(_Z3Solver):
def __init__(self):
super().__init__(SOLVER_Z3_OPTIMIZE, _Z3Solver.Z3_OPTION_OPTIMIZE)
class SolveZ3Solver(_Z3Solver):
def __init__(self):
super().__init__(SOLVER_Z3_SOLVE, _Z3Solver.Z3_OPTION_SOLVE)
class CVC5Solver(_SolverImpl):
def __init__(self):
util_common.check(try_import_cvc5(), 'cvc5 not available')
super().__init__(SOLVER_CVC5, False, False)
self._s = cvc5.pythonic.SimpleSolver()
def _IMPL_negate_var_conj(self, ll):
return cvc5.pythonic.Not(ll)
def _IMPL_make_var(self):
return cvc5.pythonic.FreshBool()
def _IMPL_make_conj(self, lls):
if len(lls) == 1:
return lls[0]
else:
return cvc5.pythonic.And(*lls)
def _IMPL_cnstr_implies_disj(self, in_ll, out_lls, weight):
util_common.check(weight is None, 'solver does not support weights')
if len(out_lls) == 0:
self._s.add(cvc5.pythonic.Implies(in_ll, False))
elif len(out_lls) == 1:
self._s.add(cvc5.pythonic.Implies(in_ll, out_lls[0]))
else:
self._s.add(cvc5.pythonic.Implies(in_ll, cvc5.pythonic.Or(*out_lls)))
def _IMPL_cnstr_count(self, lls, lo, hi, weight):
util_common.check(weight is None, 'solver does not support weights')
if len(lls) == 0:
pass
elif len(lls) == 1:
if lo == 0 and hi == 1:
pass
elif lo == 0 and hi == 0:
self._s.add(cvc5.pythonic.Not(lls[0]))
elif lo == 1 and hi == 1:
self._s.add(lls[0])
else:
util_common.check(False, 'count vars')
else:
lls_if = sum([cvc5.pythonic.If(ll, 1, 0) for ll in lls])
if lo == hi:
self._s.add(lls_if == lo)
else:
if lo == 0:
pass
elif lo == 1:
self._s.add(cvc5.pythonic.Or(lls))
else:
self._s.add(lls_if >= lo)
if hi < len(lls):
self._s.add(lls_if <= hi)
def _IMPL_solve(self):
chk = self._s.check()
util_common.write_time('\n')
util_common.write_time(str(chk) + '\n')
if chk == cvc5.pythonic.unsat:
return False
if chk == cvc5.pythonic.unknown:
util_common.write_time(str(self._s.reason_unknown()) + '\n')
return False
self._result = self._s.model()
self._objective = 0
return True
def _IMPL_get_var(self, vv):
return bool(self._result[vv])
class _MilpSolver(_SolverImpl):
def __init__(self, solver_id):
super().__init__(solver_id, True, False)
self._curr_id = 0
self._weights = []
self._constraints = []
def _help_new_var(self):
self._curr_id += 1
return self._curr_id
def _IMPL_negate_var_conj(self, ll):
return -ll
def _IMPL_make_var(self):
return self._help_new_var()
def _IMPL_make_conj(self, lls):
if len(lls) == 1:
return lls[0]
else:
vv = self._help_new_var()
n = len(lls)
coefs, inds, lo, hi = [], [], None, 0
coefs.append(n)
inds.append(abs(vv) - 1)
for ll in lls:
inds.append(abs(ll) - 1)
if ll > 0:
coefs.append(-1)
elif ll < 0:
coefs.append(1)
hi += 1
else:
util_common.check(False, 'no id 0')
self._constraints.append((coefs, inds, lo, hi))
coefs, inds, lo, hi = [], [], None, n - 1
coefs.append(-n)
inds.append(abs(vv) - 1)
for ll in lls:
inds.append(abs(ll) - 1)
if ll > 0:
coefs.append(1)
elif ll < 0:
coefs.append(-1)
hi -= 1
else:
util_common.check(False, 'no id 0')
self._constraints.append((coefs, inds, lo, hi))
return vv
def _IMPL_cnstr_implies_disj(self, in_ll, out_lls, weight):
coefs, inds, lo, hi = [], [], None, 0
inds.append(abs(in_ll) - 1)
if in_ll > 0:
coefs.append(1)
elif in_ll < 0:
coefs.append(-1)
hi -= 1
else:
util_common.check(False, 'no id 0')
for ll in out_lls:
inds.append(abs(ll) - 1)
if ll > 0:
coefs.append(-1)
elif ll < 0:
coefs.append(1)
hi += 1
else:
util_common.check(False, 'no id 0')
if weight is not None:
weight_var = self._help_new_var()
self._weights.append((weight, abs(weight_var) - 1))
inds.append(abs(weight_var) - 1)
coefs.append(-1)
self._constraints.append((coefs, inds, lo, hi))
def _IMPL_cnstr_count(self, lls, lo, hi, weight):
if len(lls) > 0:
coefs, inds, lo, hi = [], [], lo, hi
for ll in lls:
inds.append(abs(ll) - 1)
if ll > 0:
coefs.append(1)
elif ll < 0:
coefs.append(-1)
lo -= 1
hi -= 1
else:
util_common.check(False, 'no id 0')
if weight is not None:
weight_var1 = self._help_new_var()
self._weights.append((weight, abs(weight_var1) - 1))
weight_var2 = self._help_new_var()
self._weights.append((weight, abs(weight_var2) - 1))
self._constraints.append((coefs + [ len(lls)], inds + [abs(weight_var1) - 1], lo, None))
self._constraints.append((coefs + [-len(lls)], inds + [abs(weight_var2) - 1], None, hi))
else:
self._constraints.append((coefs, inds, lo, hi))
def _IMPL_solve(self):
soln = self._do_solve()
if soln is None:
return False
self._result, self._objective = soln
return True
def _IMPL_get_var(self, vv):
return self._result[vv - 1] > 0
class SciPySolver(_MilpSolver):
def __init__(self):
util_common.check(try_import_scipy(), 'scipy not available')
super().__init__(SOLVER_SCIPY)
def _do_solve(self):
c = numpy.zeros(self._curr_id)
for coef, ind in self._weights:
c[ind] += coef
A = numpy.zeros((len(self._constraints), self._curr_id))
b_l = numpy.zeros(len(self._constraints))
b_u = numpy.zeros(len(self._constraints))
for ii, (coefs, inds, lo, hi) in enumerate(self._constraints):
for coef, ind in zip(coefs, inds):
A[ii][ind] += coef
if lo is None:
b_l[ii] = -numpy.inf
else:
b_l[ii] = lo
if hi is None:
b_u[ii] = numpy.inf
else:
b_u[ii] = hi
constraints = scipy.optimize.LinearConstraint(A, b_l, b_u)
integrality = numpy.ones(self._curr_id, dtype=numpy.uint8)
bounds = scipy.optimize.Bounds(numpy.zeros(self._curr_id), numpy.ones(self._curr_id))
res = scipy.optimize.milp(c=c, constraints=constraints, integrality=integrality, bounds=bounds)
if res.status != 0:
return None
util_common.check(int(res.fun) == res.fun, 'non-integer objective')
return (res.x > 0.5), int(res.fun)
class CvxPySolver(_MilpSolver):
def __init__(self):
util_common.check(try_import_cvxpy(), 'cvxpy not available')