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.Serializable;
33  import java.math.BigInteger;
34  
35  import org.sat4j.core.ReadOnlyVec;
36  import org.sat4j.core.ReadOnlyVecInt;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVec;
39  import org.sat4j.specs.IVecInt;
40  
41  /**
42   * Abstraction for an Objective Function for Pseudo Boolean Optimization.
43   * 
44   * May be generalized in the future to deal with other optimization functions.
45   * 
46   * @author leberre
47   * 
48   */
49  public class ObjectiveFunction implements Serializable {
50  
51      /**
52       * 
53       */
54      private static final long serialVersionUID = 1L;
55  
56      // contains the coeffs of the objective function for each variable
57      private final IVec<BigInteger> coeffs;
58  
59      private final IVecInt vars;
60  
61      private BigInteger correction = BigInteger.ZERO;
62  
63      public ObjectiveFunction(IVecInt vars, IVec<BigInteger> coeffs) {
64          this.vars = new ReadOnlyVecInt(vars);
65          this.coeffs = new ReadOnlyVec<BigInteger>(coeffs);
66      }
67  
68      // calculate the degree of the objective function
69      public BigInteger calculateDegree(ISolver solver) {
70          BigInteger tempDegree = BigInteger.ZERO;
71          for (int i = 0; i < this.vars.size(); i++) {
72              BigInteger coeff = this.coeffs.get(i);
73              if (varInModel(this.vars.get(i), solver)) {
74                  tempDegree = tempDegree.add(coeff);
75              } else if (coeff.signum() < 0
76                      && !varInModel(-this.vars.get(i), solver)) {
77                  // the variable does not appear in the model: it can be assigned
78                  // either way
79                  tempDegree = tempDegree.add(coeff);
80              }
81          }
82          return tempDegree;
83      }
84  
85      // calculate the degree of the objective function
86      public BigInteger calculateDegreeImplicant(ISolver solver) {
87          BigInteger tempDegree = BigInteger.ZERO;
88          solver.primeImplicant();
89          for (int i = 0; i < this.vars.size(); i++) {
90              BigInteger coeff = this.coeffs.get(i);
91              if (solver.primeImplicant(this.vars.get(i))) {
92                  tempDegree = tempDegree.add(coeff);
93              } else if (coeff.signum() < 0
94                      && !solver.primeImplicant(-this.vars.get(i))) {
95                  // the variable does not appear in the model: it can be assigned
96                  // either way
97                  tempDegree = tempDegree.add(coeff);
98              }
99          }
100         return tempDegree;
101     }
102 
103     private boolean varInModel(int var, ISolver solver) {
104         if (var > 0) {
105             return solver.model(var);
106         }
107         return !solver.model(-var);
108     }
109 
110     public IVec<BigInteger> getCoeffs() {
111         return this.coeffs;
112     }
113 
114     public IVecInt getVars() {
115         return this.vars;
116     }
117 
118     public void setCorrection(BigInteger correction) {
119         this.correction = correction;
120     }
121 
122     public BigInteger getCorrection() {
123         return this.correction;
124     }
125 
126     @Override
127     public String toString() {
128         StringBuffer stb = new StringBuffer();
129         IVecInt lits = getVars();
130         IVec<BigInteger> coefs = getCoeffs();
131         BigInteger coef;
132         int lit;
133         for (int i = 0; i < lits.size(); i++) {
134             coef = coefs.get(i);
135             lit = lits.get(i);
136             if (lit < 0) {
137                 lit = -lit;
138                 coef = coef.negate();
139             }
140             stb.append((coef.signum() < 0 ? "" : "+") + coef + " x" + lit + " ");
141         }
142         return stb.toString();
143     }
144 
145     public BigInteger minValue() {
146         BigInteger tempDegree = BigInteger.ZERO;
147         for (int i = 0; i < this.vars.size(); i++) {
148             BigInteger coeff = this.coeffs.get(i);
149             if (coeff.signum() < 0) {
150                 tempDegree = tempDegree.add(coeff);
151             }
152         }
153         return tempDegree;
154     }
155 
156     public BigInteger calculateDegree(int[] model) {
157         BigInteger tempDegree = BigInteger.ZERO;
158 
159         for (int i = 0; i < this.vars.size(); i++) {
160             BigInteger coeff = this.coeffs.get(i);
161             if (varInModel(this.vars.get(i), model)) {
162                 tempDegree = tempDegree.add(coeff);
163             } else if (coeff.signum() < 0
164                     && !varInModel(-this.vars.get(i), model)) {
165                 tempDegree = tempDegree.add(coeff);
166             }
167         }
168         return tempDegree;
169     }
170 
171     private boolean varInModel(int var, int[] model) {
172         for (int element : model) {
173             if (var == element) {
174                 return true;
175             }
176         }
177         return false;
178     }
179 
180 }