fixed serialization problems

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@780976 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-06-02 09:06:26 +00:00
parent 86b19c0e8e
commit 2e4082e52f
3 changed files with 185 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -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 (c<sub>i</sub>). */
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);
}
}

View File

@ -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);
}
}