From 2e4082e52f45bd423df6c47fa19b6c5da8b9c009 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Tue, 2 Jun 2009 09:06:26 +0000 Subject: [PATCH] fixed serialization problems git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@780976 13f79535-47bb-0310-9956-ffa450edef68 --- .../optimization/linear/LinearConstraint.java | 61 +++++++++++++++- .../linear/LinearObjectiveFunction.java | 57 ++++++++++++++- .../optimization/linear/SimplexTableau.java | 70 ++++++++++++++++++- 3 files changed, 185 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/commons/math/optimization/linear/LinearConstraint.java b/src/java/org/apache/commons/math/optimization/linear/LinearConstraint.java index f3c2956e8..118208724 100644 --- a/src/java/org/apache/commons/math/optimization/linear/LinearConstraint.java +++ b/src/java/org/apache/commons/math/optimization/linear/LinearConstraint.java @@ -17,8 +17,12 @@ package org.apache.commons.math.optimization.linear; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; +import org.apache.commons.math.linear.MatrixUtils; import org.apache.commons.math.linear.RealVector; import org.apache.commons.math.linear.RealVectorImpl; @@ -50,7 +54,7 @@ public class LinearConstraint implements Serializable { private static final long serialVersionUID = -764632794033034092L; /** Coefficients of the constraint (left hand side). */ - private final RealVector coefficients; + private final transient RealVector coefficients; /** Relationship between left and right hand sides (=, <=, >=). */ private final Relationship relationship; @@ -180,4 +184,59 @@ public class LinearConstraint implements Serializable { return value; } + /** {@inheritDoc} */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + + if (other == null) { + return false; + } + + try { + + LinearConstraint rhs = (LinearConstraint) other; + return (relationship == rhs.relationship) && + (value == rhs.value) && + coefficients.equals(rhs.coefficients); + + } catch (ClassCastException ex) { + // ignore exception + return false; + } + + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return relationship.hashCode() ^ + Double.valueOf(value).hashCode() ^ + coefficients.hashCode(); + } + + /** Serialize the instance. + * @param oos stream where object should be written + * @throws IOException if object cannot be written to stream + */ + private void writeObject(ObjectOutputStream oos) + throws IOException { + oos.defaultWriteObject(); + MatrixUtils.serializeRealVector(coefficients, oos); + } + + /** Deserialize the instance. + * @param ois stream from which the object should be read + * @throws ClassNotFoundException if a class in the stream cannot be found + * @throws IOException if object cannot be read from the stream + */ + private void readObject(ObjectInputStream ois) + throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + MatrixUtils.deserializeRealVector(this, "coefficients", ois); + } + } diff --git a/src/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java b/src/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java index 0f4dab91e..0a236ae03 100644 --- a/src/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java +++ b/src/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java @@ -17,8 +17,12 @@ package org.apache.commons.math.optimization.linear; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; +import org.apache.commons.math.linear.MatrixUtils; import org.apache.commons.math.linear.RealVector; import org.apache.commons.math.linear.RealVectorImpl; @@ -41,7 +45,7 @@ public class LinearObjectiveFunction implements Serializable { private static final long serialVersionUID = -4531815507568396090L; /** Coefficients of the constraint (ci). */ - private final RealVector coefficients; + private final transient RealVector coefficients; /** Constant term of the linear equation. */ private final double constantTerm; @@ -97,4 +101,55 @@ public class LinearObjectiveFunction implements Serializable { return coefficients.dotProduct(point) + constantTerm; } + /** {@inheritDoc} */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + + if (other == null) { + return false; + } + + try { + + LinearObjectiveFunction rhs = (LinearObjectiveFunction) other; + return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients); + + } catch (ClassCastException ex) { + // ignore exception + return false; + } + + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode(); + } + + /** Serialize the instance. + * @param oos stream where object should be written + * @throws IOException if object cannot be written to stream + */ + private void writeObject(ObjectOutputStream oos) + throws IOException { + oos.defaultWriteObject(); + MatrixUtils.serializeRealVector(coefficients, oos); + } + + /** Deserialize the instance. + * @param ois stream from which the object should be read + * @throws ClassNotFoundException if a class in the stream cannot be found + * @throws IOException if object cannot be read from the stream + */ + private void readObject(ObjectInputStream ois) + throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + MatrixUtils.deserializeRealVector(this, "coefficients", ois); + } + } diff --git a/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java b/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java index 64e10c188..ba577223d 100644 --- a/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java +++ b/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java @@ -17,11 +17,15 @@ package org.apache.commons.math.optimization.linear; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.apache.commons.math.linear.MatrixUtils; import org.apache.commons.math.linear.RealMatrix; import org.apache.commons.math.linear.RealMatrixImpl; import org.apache.commons.math.linear.RealVector; @@ -69,7 +73,7 @@ class SimplexTableau implements Serializable { private final boolean restrictToNonNegative; /** Simple tableau. */ - protected RealMatrix tableau; + protected transient RealMatrix tableau; /** Number of decision variables. */ protected final int numDecisionVariables; @@ -488,4 +492,68 @@ class SimplexTableau implements Serializable { return tableau.getData(); } + /** {@inheritDoc} */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + + if (other == null) { + return false; + } + + try { + + SimplexTableau rhs = (SimplexTableau) other; + return (restrictToNonNegative == rhs.restrictToNonNegative) && + (numDecisionVariables == rhs.numDecisionVariables) && + (numSlackVariables == rhs.numSlackVariables) && + (numArtificialVariables == rhs.numArtificialVariables) && + (epsilon == rhs.epsilon) && + f.equals(rhs.f) && + constraints.equals(rhs.constraints) && + tableau.equals(rhs.tableau); + + } catch (ClassCastException ex) { + // ignore exception + return false; + } + + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return Boolean.valueOf(restrictToNonNegative).hashCode() ^ + numDecisionVariables ^ + numSlackVariables ^ + numArtificialVariables ^ + Double.valueOf(epsilon).hashCode() ^ + f.hashCode() ^ + constraints.hashCode() ^ + tableau.hashCode(); + } + + /** Serialize the instance. + * @param oos stream where object should be written + * @throws IOException if object cannot be written to stream + */ + private void writeObject(ObjectOutputStream oos) + throws IOException { + oos.defaultWriteObject(); + MatrixUtils.serializeRealMatrix(tableau, oos); + } + + /** Deserialize the instance. + * @param ois stream from which the object should be read + * @throws ClassNotFoundException if a class in the stream cannot be found + * @throws IOException if object cannot be read from the stream + */ + private void readObject(ObjectInputStream ois) + throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + MatrixUtils.deserializeRealMatrix(this, "tableau", ois); + } }