myloc2012
06-04-2013, 15:42
سلام عليكم
ارجو المساعدة في كود Mutation بلغة c
لدي الكود مكتب بلغة java ارجو المساعدة في تحويل الى c
// BitFlipMutation.java
//
//
package jmetal.operators.mutation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import jmetal.operators.mutation.Mutation;
import jmetal.util.Configuration;
import jmetal.util.JMException;
import jmetal.util.PseudoRandom;
import jmetal.core.Solution;
import jmetal.encodings.solutionType.ArrayRealSolutionTyp e;
import jmetal.encodings.solutionType.BinaryRealSolutionTy pe;
import jmetal.encodings.solutionType.BinarySolutionType;
import jmetal.encodings.solutionType.IntSolutionType;
import jmetal.encodings.solutionType.RealSolutionType;
import jmetal.encodings.variable.*;
/**
* This class implements a bit flip mutation operator.
* NOTE: the operator is applied to binary or integer solutions, considering the
* whole solution as a single variable.
*/
public class BitFlipMutation extends Mutation {
/**
* Valid solution types to apply this operator
*/
private static List VALID_TYPES = Arrays.asList(BinarySolutionType.class,
BinaryRealSolutionType.class,
IntSolutionType.class) ;
private Double mutationProbability_ = null ;
/**
* Constructor
* Creates a new instance of the Bit Flip mutation operator
*/
public BitFlipMutation(HashMap<String, Object> parameters) {
super(parameters) ;
if (parameters.get("probability") != null)
mutationProbability_ = (Double) parameters.get("probability") ;
} // BitFlipMutation
/**
* Perform the mutation operation
* @param probability Mutation probability
* @param solution The solution to mutate
* @throws JMException
*/
public void doMutation(double probability, Solution solution) throws JMException {
try {
if ((solution.getType().getClass() == BinarySolutionType.class) ||
(solution.getType().getClass() == BinaryRealSolutionType.class)) {
for (int i = 0; i < solution.getDecisionVariables().length; i++) {
for (int j = 0; j < ((Binary) solution.getDecisionVariables()[i]).getNumberOfBits(); j++) {
if (PseudoRandom.randDouble() < probability) {
((Binary) solution.getDecisionVariables()[i]).bits_.flip(j);
}
}
}
for (int i = 0; i < solution.getDecisionVariables().length; i++) {
((Binary) solution.getDecisionVariables()[i]).decode();
}
} // if
else { // Integer representation
for (int i = 0; i < solution.getDecisionVariables().length; i++)
if (PseudoRandom.randDouble() < probability) {
int value = (int) (PseudoRandom.randInt(
(int)solution.getDecisionVariables()[i].getLowerBound(),
(int)solution.getDecisionVariables()[i].getUpperBound()));
solution.getDecisionVariables()[i].setValue(value);
} // if
} // else
} catch (ClassCastException e1) {
Configuration.logger_.severe("BitFlipMutation.doMutation: " +
"ClassCastException error" + e1.getMessage());
Class cls = java.lang.String.class;
String name = cls.getName();
throw new JMException("Exception in " + name + ".doMutation()");
}
} // doMutation
/**
* Executes the operation
* @param object An object containing a solution to mutate
* @return An object containing the mutated solution
* @throws JMException
*/
public Object execute(Object object) throws JMException {
Solution solution = (Solution) object;
if (!VALID_TYPES.contains(solution.getType().getClass ())) {
Configuration.logger_.severe("BitFlipMutation.execute: the solution " +
"is not of the right type. The type should be 'Binary', " +
"'BinaryReal' or 'Int', but " + solution.getType() + " is obtained");
Class cls = java.lang.String.class;
String name = cls.getName();
throw new JMException("Exception in " + name + ".execute()");
} // if
doMutation(mutationProbability_, solution);
return solution;
} // execute
} // BitFlipMutation
ارجو المساعدة في كود Mutation بلغة c
لدي الكود مكتب بلغة java ارجو المساعدة في تحويل الى c
// BitFlipMutation.java
//
//
package jmetal.operators.mutation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import jmetal.operators.mutation.Mutation;
import jmetal.util.Configuration;
import jmetal.util.JMException;
import jmetal.util.PseudoRandom;
import jmetal.core.Solution;
import jmetal.encodings.solutionType.ArrayRealSolutionTyp e;
import jmetal.encodings.solutionType.BinaryRealSolutionTy pe;
import jmetal.encodings.solutionType.BinarySolutionType;
import jmetal.encodings.solutionType.IntSolutionType;
import jmetal.encodings.solutionType.RealSolutionType;
import jmetal.encodings.variable.*;
/**
* This class implements a bit flip mutation operator.
* NOTE: the operator is applied to binary or integer solutions, considering the
* whole solution as a single variable.
*/
public class BitFlipMutation extends Mutation {
/**
* Valid solution types to apply this operator
*/
private static List VALID_TYPES = Arrays.asList(BinarySolutionType.class,
BinaryRealSolutionType.class,
IntSolutionType.class) ;
private Double mutationProbability_ = null ;
/**
* Constructor
* Creates a new instance of the Bit Flip mutation operator
*/
public BitFlipMutation(HashMap<String, Object> parameters) {
super(parameters) ;
if (parameters.get("probability") != null)
mutationProbability_ = (Double) parameters.get("probability") ;
} // BitFlipMutation
/**
* Perform the mutation operation
* @param probability Mutation probability
* @param solution The solution to mutate
* @throws JMException
*/
public void doMutation(double probability, Solution solution) throws JMException {
try {
if ((solution.getType().getClass() == BinarySolutionType.class) ||
(solution.getType().getClass() == BinaryRealSolutionType.class)) {
for (int i = 0; i < solution.getDecisionVariables().length; i++) {
for (int j = 0; j < ((Binary) solution.getDecisionVariables()[i]).getNumberOfBits(); j++) {
if (PseudoRandom.randDouble() < probability) {
((Binary) solution.getDecisionVariables()[i]).bits_.flip(j);
}
}
}
for (int i = 0; i < solution.getDecisionVariables().length; i++) {
((Binary) solution.getDecisionVariables()[i]).decode();
}
} // if
else { // Integer representation
for (int i = 0; i < solution.getDecisionVariables().length; i++)
if (PseudoRandom.randDouble() < probability) {
int value = (int) (PseudoRandom.randInt(
(int)solution.getDecisionVariables()[i].getLowerBound(),
(int)solution.getDecisionVariables()[i].getUpperBound()));
solution.getDecisionVariables()[i].setValue(value);
} // if
} // else
} catch (ClassCastException e1) {
Configuration.logger_.severe("BitFlipMutation.doMutation: " +
"ClassCastException error" + e1.getMessage());
Class cls = java.lang.String.class;
String name = cls.getName();
throw new JMException("Exception in " + name + ".doMutation()");
}
} // doMutation
/**
* Executes the operation
* @param object An object containing a solution to mutate
* @return An object containing the mutated solution
* @throws JMException
*/
public Object execute(Object object) throws JMException {
Solution solution = (Solution) object;
if (!VALID_TYPES.contains(solution.getType().getClass ())) {
Configuration.logger_.severe("BitFlipMutation.execute: the solution " +
"is not of the right type. The type should be 'Binary', " +
"'BinaryReal' or 'Int', but " + solution.getType() + " is obtained");
Class cls = java.lang.String.class;
String name = cls.getName();
throw new JMException("Exception in " + name + ".execute()");
} // if
doMutation(mutationProbability_, solution);
return solution;
} // execute
} // BitFlipMutation