Scenario:
I have two Java applications - Master and Slave. Master needs to send to the Slave instance of class DEParamSet via socket.
DEParamSet:
public class DEParamSet implements Serializable {
private static final long serialVersionUID = 8151790178483218232L;
public final float CR, F;
public final int NP, D;
public final Specimen specimen;
public final IOptimizationFunction optFunction;
// Constructor and some methods....
... some code ...
}
IOptimizationFunction:
public interface IOptimizationFunction extends Serializable {
public double evaluate(Object[] values);
}
Master creates instance of this class, serializes it and sends it via socket to the Slave.
Fragment of code:
// Create objective function
IOptimizationFunction opt = new IOptimizationFunction() {
private static final long serialVersionUID = -5167105656008707046L;
@Override
public double evaluate(Object[] params) {
return (double) params[0] * (double) params[1];
}
};
// Create instance of DEParamSet with objective function defined above.
...some code to create it...
// Start thread, which serializes the instance and sends it via socket.
...some code...
Question:
On the Master, everything works fine. No exception. Slave receives data and tries to deserialize it. The following exception is thrown
java.lang.ClassNotFoundException
There is no error in DEParamSet class, because if I create it with optimization function = null, then Slave deserializes it with no problem. I tried to serialize instance into the file in the Master and then deserialize it as well. And it works. But Slave unable to deserialize instance, if it constains implementation of optimization function. Of course, slave contains definition of IOptimizationFunction interface.
Please help.
Edit
I have the class TestingFunction. The class looks like this:
public abstract class TestingFunctions {
public static double schwefel(Object[] params) {
double res = 0d;
for (int i = 0; i < params.length; i++) {
res += (-1) * (double) params[i] * Math.sin(Math.sqrt(Math.abs((double) params[i])));
}
return res;
}
public static double rastrigin(Object[] params) {
double res = 0d;
for (int i = 0; i < params.length; i++) {
res += Math.pow((double) params[i], 2) - 10 * Math.cos(2 * Math.PI * (Double) params[i]);
}
return res * 2 * params.length;
}
}
Each of these functions has returned type double and parameter is array of object. This is equal to the method double evaluate(Object[] params) in IOptimizationFunction interface. So, my vision is, that master create object of DEParamSet, which will contains some parameters and concrete implementation of interface IOptimizationFunction. For example, one of possible implementation is this:
IOptimizationFunction opt = new IOptimizationFunction() {
private static final long serialVersionUID = -5167105656008707046L;
@Override
public double evaluate(Object[] params) {
return TestingFunctions.rastrigin(params);
}
};
It this case, Rastrigin's function is called. But which function will be called, this defines user at runtime. He can selects from function, which are in the TestingFunction class.