1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.sat4j.maxsat;
20
21 import static java.lang.System.out;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.ParseException;
27 import org.apache.commons.cli.PosixParser;
28 import org.sat4j.AbstractLauncher;
29 import org.sat4j.AbstractOptimizationLauncher;
30 import org.sat4j.maxsat.reader.WDimacsReader;
31 import org.sat4j.opt.MaxSatDecorator;
32 import org.sat4j.opt.MinOneDecorator;
33 import org.sat4j.reader.DimacsReader;
34 import org.sat4j.reader.Reader;
35 import org.sat4j.specs.ISolver;
36
37
38
39
40
41
42
43 public class GenericOptLauncher extends AbstractOptimizationLauncher {
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50 @SuppressWarnings("nls")
51 private Options createCLIOptions() {
52 Options options = new Options();
53 options.addOption("t", "timeout", true,
54 "specifies the timeout (in seconds)");
55 options.addOption("T", "timeoutms", true,
56 "specifies the timeout (in milliseconds)");
57 options.addOption("k", "kind", true,
58 "kind of problem: minone, maxsat, etc.");
59 return options;
60 }
61
62 @Override
63 public void displayLicense() {
64 super.displayLicense();
65 log("This software uses some libraries from the Jakarta Commons project. See jakarta.apache.org for details.");
66 }
67
68 @Override
69 public void usage() {
70 out.println("java -jar sat4j-maxsat.jar instance-name");
71 }
72
73 @Override
74 protected Reader createReader(ISolver aSolver, String problemname) {
75 if (problemname.endsWith(".wcnf")) {
76 return new WDimacsReader(( WeightedMaxSatDecorator)aSolver);
77 }
78 return new DimacsReader(aSolver);
79 }
80
81 @Override
82 protected String getInstanceName(String[] args) {
83 return args[args.length - 1];
84 }
85
86 @Override
87 protected ISolver configureSolver(String[] args) {
88 ISolver asolver = null;
89 Options options = createCLIOptions();
90 if (args.length == 0) {
91 HelpFormatter helpf = new HelpFormatter();
92 helpf.printHelp("java -jar sat4j-maxsat.jar", options, true);
93 } else {
94 try {
95 CommandLine cmd = new PosixParser().parse(options, args);
96 int problemindex = args.length - 1;
97 String kind = cmd.getOptionValue("k");
98 if (kind == null) {
99 kind = "maxsat";
100 }
101 if ("minone".equalsIgnoreCase(kind)) {
102 asolver = new MinOneDecorator(SolverFactory.newDefault());
103 } else if ("mincost".equalsIgnoreCase(kind)||args[problemindex].endsWith(".p2cnf")) {
104 asolver = new MinCostDecorator(SolverFactory.newDefault());
105 } else {
106 assert "maxsat".equalsIgnoreCase(kind);
107
108 if (args[problemindex].endsWith(".wcnf")) {
109 asolver = new WeightedMaxSatDecorator(SolverFactory
110 .newDefault());
111 } else {
112 asolver = new MaxSatDecorator(SolverFactory
113 .newMiniMaxSAT());
114 }
115 }
116 String timeout = cmd.getOptionValue("t");
117 if (timeout == null) {
118 timeout = cmd.getOptionValue("T");
119 if (timeout != null) {
120 asolver.setTimeoutMs(Long.parseLong(timeout));
121 }
122 } else {
123 asolver.setTimeout(Integer.parseInt(timeout));
124 }
125 log(asolver.toString(COMMENT_PREFIX));
126 } catch (ParseException e1) {
127 HelpFormatter helpf = new HelpFormatter();
128 helpf.printHelp("java -jar sat4jopt.jar", options, true);
129 }
130 }
131 return asolver;
132 }
133
134 public static void main(String[] args) {
135 AbstractLauncher lanceur = new GenericOptLauncher();
136 lanceur.run(args);
137 }
138 }