001package org.clafer.math;
002
003import java.util.Set;
004import org.clafer.common.Check;
005import org.clafer.common.Util;
006import org.clafer.domain.BoolDomain;
007
008/**
009 * @author jimmy
010 */
011public class LinearEquation {
012
013    private final LinearFunction left;
014    private final Op op;
015    private final Rational right;
016
017    public LinearEquation(LinearFunction left, Op op, long right) {
018        this(left, op, new Rational(right));
019    }
020
021    public LinearEquation(LinearFunction left, Op op, Rational right) {
022        this(left, op, right, true);
023    }
024
025    public LinearEquation(LinearFunction left, Op op, long right, boolean scale) {
026        this(left, op, new Rational(right), scale);
027    }
028
029    public LinearEquation(LinearFunction left, Op op, Rational right, boolean scale) {
030        Check.notNull(left);
031        Check.notNull(op);
032        Check.notNull(right);
033        if (left.hasConstant()) {
034            throw new IllegalArgumentException();
035        }
036        if (scale && (!right.isZero() || left.arity() > 0)) {
037            Rational divisor = right.isZero() ? left.getCoefficients()[0].abs() : right.abs();
038            this.left = left.div(divisor);
039            this.right = right.div(divisor);
040        } else {
041            this.left = left;
042            this.right = right;
043        }
044        this.op = op;
045    }
046
047    public static LinearEquation equal(LinearFunctionable left, LinearFunctionable right) {
048        LinearFunction combine = left.toFunction().sub(right.toFunction());
049        return new LinearEquation(
050                combine.sub(combine.getConstant()),
051                Op.Equal,
052                combine.getConstant().minus());
053    }
054
055    public static LinearEquation lessThan(LinearFunctionable left, LinearFunctionable right) {
056        LinearFunction combine = left.toFunction().sub(right.toFunction());
057        return new LinearEquation(
058                combine.sub(combine.getConstant()),
059                Op.LessThanEqual,
060                combine.getConstant().minus().sub(Rational.One));
061    }
062
063    public static LinearEquation lessThanEqual(LinearFunctionable left, LinearFunctionable right) {
064        LinearFunction combine = left.toFunction().sub(right.toFunction());
065        return new LinearEquation(
066                combine.sub(combine.getConstant()),
067                Op.LessThanEqual,
068                combine.getConstant().minus());
069    }
070
071    public static LinearEquation greaterThan(LinearFunctionable left, LinearFunctionable right) {
072        return lessThan(right, left);
073    }
074
075    public static LinearEquation greaterThanEqual(LinearFunctionable left, LinearFunctionable right) {
076        return lessThanEqual(right, left);
077    }
078
079    public LinearFunction getLeft() {
080        return left;
081    }
082
083    public Op getOp() {
084        return op;
085    }
086
087    public Rational getRight() {
088        return right;
089    }
090
091    public Variable[] getVariables() {
092        return left.getVariables();
093    }
094
095    public Set<Variable> getVariableSet() {
096        return left.getVariableSet();
097    }
098
099    public LinearEquation replace(Variable variable, LinearFunction value) {
100        LinearFunction newLeft = left.replace(variable, value);
101        return new LinearEquation(
102                newLeft.sub(newLeft.getConstant()),
103                op,
104                right.sub(newLeft.getConstant()));
105    }
106
107    public LinearEquation normalize() {
108        long denominator = right.getDenominator();
109        for (Rational coefficient : left.getCoefficients()) {
110            denominator = Util.lcm(denominator, coefficient.getDenominator());
111        }
112        assert denominator > 0;
113        return new LinearEquation(left.mul(denominator), op, right.mul(denominator));
114    }
115
116    public BoolDomain isEntailed() {
117        switch (op) {
118            case Equal:
119                if (left.getLowBound().compareTo(right) > 0 || left.getHighBound().compareTo(right) < 0) {
120                    return BoolDomain.FalseDomain;
121                }
122                if (left.getLowBound().equals(right) && left.getHighBound().equals(right)) {
123                    return BoolDomain.TrueDomain;
124                }
125                return BoolDomain.TrueFalseDomain;
126            case LessThanEqual:
127                if (left.getLowBound().compareTo(right) > 0) {
128                    return BoolDomain.FalseDomain;
129                }
130                if (left.getHighBound().compareTo(right) <= 0) {
131                    return BoolDomain.TrueDomain;
132                }
133                return BoolDomain.TrueFalseDomain;
134            default:
135                throw new IllegalStateException();
136        }
137    }
138
139    public boolean subsumes(LinearEquation other) {
140        if (left.getVariableSet().equals(other.left.getVariableSet())
141                && Op.LessThanEqual.equals(other.getOp())) {
142            Rational scale = left.getCoefficients()[0].div(other.left.getCoefficients()[0]);
143            return left.equals(other.left.mul(scale))
144                    && right.isLessThan(other.right.mul(scale));
145        }
146        return false;
147    }
148
149    @Override
150    public boolean equals(Object obj) {
151        if (obj == this) {
152            return true;
153        }
154        if (obj instanceof LinearEquation) {
155            LinearEquation other = (LinearEquation) obj;
156            return op.equals(other.op) && left.equals(other.left) && right.equals(other.right);
157        }
158        return false;
159    }
160
161    @Override
162    public int hashCode() {
163        // op.hashCode() can change between runs which makes the output change
164        // every time.
165        return left.hashCode() ^ op.ordinal() ^ right.hashCode();
166    }
167
168    @Override
169    public String toString() {
170        return left + " " + op + " " + right;
171    }
172
173    public static enum Op {
174
175        Equal("="),
176        LessThanEqual("<=");
177
178        private final String syntax;
179
180        Op(String syntax) {
181            this.syntax = syntax;
182        }
183
184        public String getSyntax() {
185            return syntax;
186        }
187
188        @Override
189        public String toString() {
190            return syntax;
191        }
192    }
193}