View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.pb;
31  
32  import java.io.IOException;
33  
34  import org.sat4j.AbstractLauncher;
35  import org.sat4j.ILauncherMode;
36  import org.sat4j.core.ASolverFactory;
37  import org.sat4j.pb.core.ObjectiveReducerPBSolverDecorator;
38  import org.sat4j.pb.reader.OPBReader2006;
39  import org.sat4j.pb.tools.SearchOptimizerListener;
40  import org.sat4j.reader.ParseFormatException;
41  import org.sat4j.reader.Reader;
42  import org.sat4j.specs.ContradictionException;
43  import org.sat4j.specs.ILogAble;
44  import org.sat4j.specs.IProblem;
45  import org.sat4j.specs.ISolver;
46  
47  /**
48   * Launcher especially dedicated to the pseudo boolean 05 evaluation (@link
49   * http://www.cril.univ-artois.fr/PB05/).
50   * 
51   * @author mederic
52   */
53  public class LanceurPseudo2005 extends AbstractLauncher implements ILogAble {
54  
55      ASolverFactory<IPBSolver> factory;
56  
57      public LanceurPseudo2005() {
58          this(SolverFactory.instance());
59      }
60  
61      LanceurPseudo2005(ASolverFactory<IPBSolver> factory) {
62          this.factory = factory;
63          setLauncherMode(ILauncherMode.OPTIMIZATION);
64      }
65  
66      /**
67  	 * 
68  	 */
69      private static final long serialVersionUID = 1L;
70  
71      /**
72       * Lance le prouveur sur un fichier Dimacs
73       * 
74       * @param args
75       *            doit contenir le nom d'un fichier Dimacs, eventuellement
76       *            compress?.
77       */
78      public static void main(final String[] args) {
79          final AbstractLauncher lanceur = new LanceurPseudo2005();
80          lanceur.run(args);
81          System.exit(lanceur.getExitCode().value());
82      }
83  
84      protected ObjectiveFunction obfct;
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see org.sat4j.Lanceur#createReader(org.sat4j.specs.ISolver)
90       */
91      @Override
92      protected Reader createReader(ISolver theSolver, String problemname) {
93          return new OPBReader2006((IPBSolver) theSolver);
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see org.sat4j.Lanceur#configureSolver(java.lang.String[])
100      */
101     @Override
102     protected ISolver configureSolver(String[] args) {
103         IPBSolver theSolver;
104         String solverName = args[0];
105         boolean lower = false;
106         if (solverName.startsWith("Lower")) {
107             lower = true;
108             solverName = solverName.substring("Lower".length());
109         }
110         if (args.length > 1) {
111             theSolver = this.factory.createSolverByName(solverName);
112         } else {
113             theSolver = this.factory.defaultSolver();
114         }
115         if (System.getProperty("OBJREDUCER") != null) {
116             if (lower) {
117                 theSolver = new ConstraintRelaxingPseudoOptDecorator(
118                         new ObjectiveReducerPBSolverDecorator(theSolver));
119             } else {
120                 theSolver = new PseudoOptDecorator(
121                         new ObjectiveReducerPBSolverDecorator(theSolver));
122             }
123         } else if (System.getProperty("INTERNAL") != null) {
124             theSolver.setSearchListener(new SearchOptimizerListener(
125                     ILauncherMode.DECISION));
126             setLauncherMode(ILauncherMode.DECISION);
127         } else {
128             if (lower) {
129                 theSolver = new ConstraintRelaxingPseudoOptDecorator(theSolver);
130             } else {
131                 theSolver = new PseudoOptDecorator(theSolver);
132             }
133         }
134         if (args.length == 3) {
135             theSolver.setTimeout(Integer.valueOf(args[1]));
136         }
137         this.out.println(theSolver.toString(COMMENT_PREFIX));
138         return theSolver;
139     }
140 
141     @Override
142     public void usage() {
143         this.out.println("java -jar sat4j-pb.jar [solvername [timeout]] instancename.opb"); //$NON-NLS-1$
144         showAvailableSolvers(SolverFactory.instance());
145     }
146 
147     @Override
148     protected String getInstanceName(String[] args) {
149         assert args.length == 1 || args.length == 2 || args.length == 3;
150         if (args.length == 0) {
151             return null;
152         }
153         return args[args.length - 1];
154     }
155 
156     @Override
157     protected IProblem readProblem(String problemname)
158             throws ParseFormatException, IOException, ContradictionException {
159         IProblem problem = super.readProblem(problemname);
160         ObjectiveFunction obj = ((IPBSolver) problem).getObjectiveFunction();
161         if (obj != null) {
162             this.out.println(COMMENT_PREFIX + "objective function length is "
163                     + obj.getVars().size() + " literals");
164         }
165         return problem;
166     }
167 }