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 package org.sat4j.pb;
29
30 import org.sat4j.core.ASolverFactory;
31 import org.sat4j.minisat.core.IOrder;
32 import org.sat4j.minisat.core.IPhaseSelectionStrategy;
33 import org.sat4j.minisat.learning.ClauseOnlyLearning;
34 import org.sat4j.minisat.learning.MiniSATLearning;
35 import org.sat4j.minisat.learning.NoLearningButHeuristics;
36 import org.sat4j.minisat.orders.PhaseInLastLearnedClauseSelectionStrategy;
37 import org.sat4j.minisat.orders.RSATPhaseSelectionStrategy;
38 import org.sat4j.minisat.orders.UserFixedPhaseSelectionStrategy;
39 import org.sat4j.minisat.orders.VarOrderHeap;
40 import org.sat4j.minisat.restarts.ArminRestarts;
41 import org.sat4j.minisat.restarts.MiniSATRestarts;
42 import org.sat4j.minisat.uip.FirstUIP;
43 import org.sat4j.pb.constraints.AbstractPBDataStructureFactory;
44 import org.sat4j.pb.constraints.CompetMinHTmixedClauseCardConstrDataStructureFactory;
45 import org.sat4j.pb.constraints.CompetResolutionPBMixedHTClauseCardConstrDataStructure;
46 import org.sat4j.pb.constraints.CompetResolutionPBMixedWLClauseCardConstrDataStructure;
47 import org.sat4j.pb.constraints.PBMaxCBClauseCardConstrDataStructure;
48 import org.sat4j.pb.constraints.PBMaxClauseAtLeastConstrDataStructure;
49 import org.sat4j.pb.constraints.PBMaxClauseCardConstrDataStructure;
50 import org.sat4j.pb.constraints.PBMaxDataStructure;
51 import org.sat4j.pb.constraints.PBMinClauseCardConstrDataStructure;
52 import org.sat4j.pb.constraints.PBMinDataStructure;
53 import org.sat4j.pb.constraints.PuebloPBMinClauseAtLeastConstrDataStructure;
54 import org.sat4j.pb.constraints.PuebloPBMinClauseCardConstrDataStructure;
55 import org.sat4j.pb.constraints.PuebloPBMinDataStructure;
56 import org.sat4j.pb.core.PBDataStructureFactory;
57 import org.sat4j.pb.core.PBSolverCP;
58 import org.sat4j.pb.core.PBSolverCautious;
59 import org.sat4j.pb.core.PBSolverClause;
60 import org.sat4j.pb.core.PBSolverResCP;
61 import org.sat4j.pb.core.PBSolverResolution;
62 import org.sat4j.pb.core.PBSolverWithImpliedClause;
63 import org.sat4j.pb.orders.VarOrderHeapObjective;
64 import org.sat4j.specs.ISolver;
65 import org.sat4j.tools.DimacsOutputSolver;
66
67
68
69
70
71
72
73 public class SolverFactory extends ASolverFactory<IPBSolver> {
74
75
76
77
78 private static final long serialVersionUID = 1L;
79
80
81 private static SolverFactory instance;
82
83
84
85
86
87
88 private SolverFactory() {
89
90 }
91
92 private static synchronized void createInstance() {
93 if (instance == null) {
94 instance = new SolverFactory();
95 }
96 }
97
98
99
100
101
102
103 public static SolverFactory instance() {
104 if (instance == null) {
105 createInstance();
106 }
107 return instance;
108 }
109
110
111
112
113
114 public static PBSolverResolution newPBResAllPB() {
115 return newPBRes(new PBMaxDataStructure());
116 }
117
118
119
120
121
122 public static PBSolverCP newPBCPAllPB() {
123 return newPBCP(new PBMaxDataStructure());
124 }
125
126
127
128
129 public static IPBSolver newOPBStringSolver() {
130 return new OPBStringSolver();
131 }
132
133
134
135
136
137
138 public static PBSolverCP newPBCPMixedConstraints() {
139 return newPBCP(new PBMaxClauseCardConstrDataStructure());
140 }
141
142
143
144
145
146
147
148 public static PBSolverCP newPBCPMixedConstraintsObjective() {
149 return newPBCP(new PBMaxClauseCardConstrDataStructure(),
150 new VarOrderHeapObjective());
151 }
152
153 public static PBSolverCP newCompetPBCPMixedConstraintsObjective() {
154 return newPBCP(new PBMaxClauseCardConstrDataStructure(),
155 new VarOrderHeapObjective());
156 }
157
158
159
160
161
162
163
164
165
166 public static PBSolverCP newPBCPMixedConstraintsObjectiveLearnJustClauses() {
167 ClauseOnlyLearning<PBDataStructureFactory> learning = new ClauseOnlyLearning<PBDataStructureFactory>();
168 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning,
169 new PBMaxClauseCardConstrDataStructure(),
170 new VarOrderHeapObjective());
171 learning.setSolver(solver);
172 return solver;
173 }
174
175 public static PBSolverCP newCompetPBCPMixedConstraintsObjectiveLearnJustClauses() {
176 ClauseOnlyLearning<PBDataStructureFactory> learning = new ClauseOnlyLearning<PBDataStructureFactory>();
177 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning,
178 new PBMaxClauseCardConstrDataStructure(),
179 new VarOrderHeapObjective());
180 learning.setSolver(solver);
181 return solver;
182 }
183
184 private static PBSolverCP newPBKiller(IPhaseSelectionStrategy phase) {
185 ClauseOnlyLearning<PBDataStructureFactory> learning = new ClauseOnlyLearning<PBDataStructureFactory>();
186 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning,
187 new PBMaxClauseCardConstrDataStructure(),
188 new VarOrderHeapObjective(phase));
189 learning.setSolver(solver);
190 return solver;
191 }
192
193 public static PBSolverCP newPBKillerRSAT() {
194 return newPBKiller(new RSATPhaseSelectionStrategy());
195 }
196
197 public static PBSolverCP newPBKillerClassic() {
198 return newPBKiller(new PhaseInLastLearnedClauseSelectionStrategy());
199 }
200
201 public static PBSolverCP newPBKillerFixed() {
202 return newPBKiller(new UserFixedPhaseSelectionStrategy());
203 }
204
205 private static PBSolverCP newCompetPBKiller(IPhaseSelectionStrategy phase) {
206 ClauseOnlyLearning<PBDataStructureFactory> learning = new ClauseOnlyLearning<PBDataStructureFactory>();
207 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning,
208 new PBMaxClauseCardConstrDataStructure(),
209 new VarOrderHeapObjective(phase));
210 learning.setSolver(solver);
211 return solver;
212 }
213
214 public static PBSolverCP newCompetPBKillerRSAT() {
215 return newCompetPBKiller(new RSATPhaseSelectionStrategy());
216 }
217
218 public static PBSolverCP newCompetPBKillerClassic() {
219 return newCompetPBKiller(new PhaseInLastLearnedClauseSelectionStrategy());
220 }
221
222 public static PBSolverCP newCompetPBKillerFixed() {
223 return newCompetPBKiller(new UserFixedPhaseSelectionStrategy());
224 }
225
226
227
228
229
230
231
232
233 public static PBSolverCP newMiniLearningOPBClauseCardConstrMaxSpecificOrderIncrementalReductionToClause() {
234
235 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
236
237 PBSolverCP solver = new PBSolverClause(new FirstUIP(), learning,
238 new PBMaxClauseCardConstrDataStructure(),
239 new VarOrderHeapObjective());
240 learning.setDataStructureFactory(solver.getDSFactory());
241 learning.setVarActivityListener(solver);
242 return solver;
243 }
244
245
246
247
248
249
250
251
252 public static PBSolverCP newPBCPMixedConstraintsObjectiveNoLearning() {
253 NoLearningButHeuristics<PBDataStructureFactory> learning = new NoLearningButHeuristics<PBDataStructureFactory>();
254
255 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning,
256 new PBMaxClauseCardConstrDataStructure(),
257 new VarOrderHeapObjective());
258 learning.setSolver(solver);
259 learning.setVarActivityListener(solver);
260 return solver;
261 }
262
263 public static PBSolverResolution newPBResMixedConstraintsObjective() {
264 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
265 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
266 learning, new PBMaxClauseCardConstrDataStructure(),
267 new VarOrderHeapObjective(), new MiniSATRestarts());
268 learning.setDataStructureFactory(solver.getDSFactory());
269 learning.setVarActivityListener(solver);
270 return solver;
271 }
272
273 public static PBSolverResolution newCompetPBResWLMixedConstraintsObjective() {
274 return newCompetPBResMixedConstraintsObjective(new CompetResolutionPBMixedWLClauseCardConstrDataStructure());
275 }
276
277 public static PBSolverResolution newCompetPBResHTMixedConstraintsObjective() {
278 return newCompetPBResMixedConstraintsObjective(new CompetResolutionPBMixedHTClauseCardConstrDataStructure());
279 }
280
281 public static PBSolverResolution newCompetPBResMixedConstraintsObjective(
282 PBDataStructureFactory dsf) {
283 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
284 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
285 learning, dsf, new VarOrderHeapObjective(
286 new RSATPhaseSelectionStrategy()), new ArminRestarts());
287 learning.setDataStructureFactory(solver.getDSFactory());
288 learning.setVarActivityListener(solver);
289 return solver;
290 }
291
292 public static PBSolverResolution newPBResHTMixedConstraintsObjective() {
293 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
294 AbstractPBDataStructureFactory ds = new CompetResolutionPBMixedHTClauseCardConstrDataStructure();
295 ds.setNormalizer(AbstractPBDataStructureFactory.NO_COMPETITION);
296 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
297 learning, ds, new VarOrderHeapObjective(),
298 new MiniSATRestarts());
299 learning.setDataStructureFactory(solver.getDSFactory());
300 learning.setVarActivityListener(solver);
301 return solver;
302 }
303
304 public static PBSolverResolution newCompetPBResMinHTMixedConstraintsObjective() {
305 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
306 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
307 learning,
308 new CompetMinHTmixedClauseCardConstrDataStructureFactory(),
309 new VarOrderHeapObjective(), new MiniSATRestarts());
310 learning.setDataStructureFactory(solver.getDSFactory());
311 learning.setVarActivityListener(solver);
312 return solver;
313 }
314
315 public static PBSolverResolution newPBResMinHTMixedConstraintsObjective() {
316 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
317 AbstractPBDataStructureFactory ds = new CompetMinHTmixedClauseCardConstrDataStructureFactory();
318 ds.setNormalizer(AbstractPBDataStructureFactory.NO_COMPETITION);
319 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
320 learning, ds, new VarOrderHeapObjective(),
321 new MiniSATRestarts());
322 learning.setDataStructureFactory(solver.getDSFactory());
323 learning.setVarActivityListener(solver);
324 return solver;
325 }
326
327 public static PBSolverResolution newCompetPBResMixedConstraintsObjectiveExpSimp() {
328 PBSolverResolution solver = newPBResMixedConstraintsObjective();
329 solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
330 return solver;
331 }
332
333 public static PBSolverResolution newCompetPBResWLMixedConstraintsObjectiveExpSimp() {
334 PBSolverResolution solver = newCompetPBResWLMixedConstraintsObjective();
335 solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
336 return solver;
337 }
338
339 public static PBSolverResolution newPBResHTMixedConstraintsObjectiveExpSimp() {
340 PBSolverResolution solver = newPBResHTMixedConstraintsObjective();
341 solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
342 return solver;
343 }
344
345 public static PBSolverResolution newCompetPBResMinHTMixedConstraintsObjectiveExpSimp() {
346 PBSolverResolution solver = newCompetPBResMinHTMixedConstraintsObjective();
347 solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
348 return solver;
349 }
350
351
352
353
354
355
356
357
358 public static PBSolverClause newPBCPMixedConstraintsReduceToClause() {
359 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
360 PBSolverClause solver = new PBSolverClause(new FirstUIP(), learning,
361 new PBMaxClauseCardConstrDataStructure(), new VarOrderHeap());
362 learning.setDataStructureFactory(solver.getDSFactory());
363 learning.setVarActivityListener(solver);
364 return solver;
365 }
366
367
368
369
370
371
372
373
374 public static PBSolverCautious newPBCPMixedConstraintsCautious(int bound) {
375 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
376 PBSolverCautious solver = new PBSolverCautious(new FirstUIP(),
377 learning, new PBMaxClauseCardConstrDataStructure(),
378 new VarOrderHeapObjective(), bound);
379 learning.setDataStructureFactory(solver.getDSFactory());
380 learning.setVarActivityListener(solver);
381 return solver;
382 }
383
384 public static PBSolverCautious newPBCPMixedConstraintsCautious() {
385 return newPBCPMixedConstraintsCautious(PBSolverCautious.BOUND);
386 }
387
388
389
390
391
392
393
394
395 public static PBSolverResCP newPBCPMixedConstraintsResCP(long bound) {
396 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
397 PBSolverResCP solver = new PBSolverResCP(new FirstUIP(), learning,
398 new PBMaxClauseCardConstrDataStructure(),
399 new VarOrderHeapObjective(), bound);
400 learning.setDataStructureFactory(solver.getDSFactory());
401 learning.setVarActivityListener(solver);
402 solver.setSimplifier(solver.EXPENSIVE_SIMPLIFICATION);
403 return solver;
404 }
405
406 public static PBSolverResCP newPBCPMixedConstraintsResCP() {
407 return newPBCPMixedConstraintsResCP(PBSolverResCP.MAXCONFLICTS);
408 }
409
410
411
412
413
414
415
416 public static PBSolverWithImpliedClause newPBCPMixedConstrainsImplied() {
417 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
418 PBSolverWithImpliedClause solver = new PBSolverWithImpliedClause(
419 new FirstUIP(), learning,
420 new PBMaxClauseCardConstrDataStructure(), new VarOrderHeap());
421 learning.setDataStructureFactory(solver.getDSFactory());
422 learning.setVarActivityListener(solver);
423 return solver;
424 }
425
426
427
428
429
430
431
432
433 public static PBSolverCP newMiniOPBClauseAtLeastConstrMax() {
434 return newPBCP(new PBMaxClauseAtLeastConstrDataStructure());
435 }
436
437
438
439
440
441 public static PBSolverCP newMiniOPBCounterBasedClauseCardConstrMax() {
442 return newPBCP(new PBMaxCBClauseCardConstrDataStructure());
443 }
444
445
446
447
448
449 public static PBSolverResolution newPBResAllPBWL() {
450 return newPBRes(new PBMinDataStructure());
451 }
452
453
454
455
456
457 public static PBSolverCP newPBCPAllPBWL() {
458 return newPBCP(new PBMinDataStructure());
459 }
460
461
462
463
464
465 public static PBSolverResolution newPBResAllPBWLPueblo() {
466 return newPBRes(new PuebloPBMinDataStructure());
467 }
468
469 private static PBSolverResolution newPBRes(PBDataStructureFactory dsf) {
470 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
471 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
472 learning, dsf, new VarOrderHeap(), new MiniSATRestarts());
473 learning.setDataStructureFactory(solver.getDSFactory());
474 learning.setVarActivityListener(solver);
475 return solver;
476 }
477
478
479
480
481
482 public static PBSolverCP newPBCPAllPBWLPueblo() {
483 return newPBCP(new PuebloPBMinDataStructure());
484 }
485
486
487
488
489
490 public static PBSolverCP newMiniOPBClauseCardMinPueblo() {
491 return newPBCP(new PuebloPBMinClauseCardConstrDataStructure());
492 }
493
494
495
496
497
498 public static PBSolverCP newMiniOPBClauseCardMin() {
499 return newPBCP(new PBMinClauseCardConstrDataStructure());
500 }
501
502
503
504
505
506 public static PBSolverCP newMiniOPBClauseAtLeastMinPueblo() {
507 return newPBCP(new PuebloPBMinClauseAtLeastConstrDataStructure());
508 }
509
510 private static PBSolverCP newPBCP(PBDataStructureFactory dsf, IOrder order) {
511 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
512 PBSolverCP solver = new PBSolverCP(new FirstUIP(), learning, dsf, order);
513 learning.setDataStructureFactory(solver.getDSFactory());
514 learning.setVarActivityListener(solver);
515 solver.setRestartStrategy(new ArminRestarts());
516 solver.setLearnedConstraintsDeletionStrategy(solver.glucose);
517 return solver;
518 }
519
520 private static PBSolverCP newPBCP(PBDataStructureFactory dsf) {
521 return newPBCP(dsf, new VarOrderHeap());
522 }
523
524
525
526
527
528
529
530 public static IPBSolver newCuttingPlanes() {
531 return newCompetPBCPMixedConstraintsObjective();
532 }
533
534
535
536
537
538
539
540 public static IPBSolver newResolution() {
541 return newResolutionGlucose();
542 }
543
544
545
546
547
548
549
550
551 public static IPBSolver newResolutionGlucose() {
552 PBSolverResolution solver = newCompetPBResWLMixedConstraintsObjectiveExpSimp();
553 solver.setLearnedConstraintsDeletionStrategy(solver.glucose);
554 return solver;
555 }
556
557
558
559
560
561
562
563
564 public static IPBSolver newResolutionSimpleRestarts() {
565 PBSolverResolution solver = newCompetPBResWLMixedConstraintsObjectiveExpSimp();
566 solver.setLearnedConstraintsDeletionStrategy(solver.glucose);
567 solver.setRestartStrategy(new MiniSATRestarts());
568 return solver;
569 }
570
571
572
573
574
575
576
577
578
579 public static IPBSolver newResolutionMaxMemory() {
580 return newCompetPBResWLMixedConstraintsObjectiveExpSimp();
581 }
582
583
584
585
586
587
588
589
590
591 public static IPBSolver newDefault() {
592 return newResolutionGlucose();
593 }
594
595
596
597
598
599
600
601
602
603 public static IPBSolver newDefaultNonNormalized() {
604 return newPBResHTMixedConstraintsObjectiveExpSimp();
605 }
606
607 @Override
608 public IPBSolver defaultSolver() {
609 return newDefault();
610 }
611
612
613
614
615
616
617
618
619 public static IPBSolver newLight() {
620 return newCompetPBResMixedConstraintsObjectiveExpSimp();
621 }
622
623 @Override
624 public IPBSolver lightSolver() {
625 return newLight();
626 }
627
628 public static ISolver newDimacsOutput() {
629 return new DimacsOutputSolver();
630 }
631
632 public static IPBSolver newEclipseP2() {
633 MiniSATLearning<PBDataStructureFactory> learning = new MiniSATLearning<PBDataStructureFactory>();
634 PBSolverResolution solver = new PBSolverResolution(new FirstUIP(),
635 learning,
636 new CompetResolutionPBMixedHTClauseCardConstrDataStructure(),
637 new VarOrderHeapObjective(new RSATPhaseSelectionStrategy()),
638 new ArminRestarts());
639 learning.setDataStructureFactory(solver.getDSFactory());
640 learning.setVarActivityListener(solver);
641 solver.setTimeoutOnConflicts(300);
642 solver.setVerbose(false);
643 return new OptToPBSATAdapter(new PseudoOptDecorator(solver));
644 }
645
646 }