use of org.evosuite.symbolic.expr.fp.RealVariable in project evosuite by EvoSuite.
the class EvoSuiteSolver method solve.
@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException {
long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
long startTimeMillis = System.currentTimeMillis();
Set<Variable<?>> variables = getVariables(constraints);
Map<String, Object> initialValues = getConcreteValues(variables);
double distance = DistanceEstimator.getDistance(constraints);
if (distance == 0.0) {
log.info("Initial distance already is 0.0, skipping search");
SolverResult satResult = SolverResult.newSAT(initialValues);
return satResult;
}
for (int attempt = 0; attempt <= Properties.DSE_VARIABLE_RESETS; attempt++) {
for (Variable<?> v : variables) {
long currentTimeMillis = System.currentTimeMillis();
long elapsed_solving_time = currentTimeMillis - startTimeMillis;
if (elapsed_solving_time > timeout) {
throw new SolverTimeoutException();
}
log.debug("Variable: " + v + ", " + variables);
if (v instanceof IntegerVariable) {
IntegerVariable integerVariable = (IntegerVariable) v;
IntegerAVM avm = new IntegerAVM(integerVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof RealVariable) {
RealVariable realVariable = (RealVariable) v;
RealAVM avm = new RealAVM(realVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof StringVariable) {
StringVariable strVariable = (StringVariable) v;
StringAVM avm = new StringAVM(strVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else {
throw new RuntimeException("Unknown variable type " + v.getClass().getName());
}
distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
}
}
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
} else {
log.info("Randomizing variables");
randomizeValues(variables, getConstants(constraints));
}
}
// distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0) {
log.debug("Distance is " + distance + ", found solution");
Map<String, Object> new_model = getConcreteValues(variables);
setConcreteValues(variables, initialValues);
SolverResult satResult = SolverResult.newSAT(new_model);
return satResult;
} else {
setConcreteValues(variables, initialValues);
log.debug("Returning null, search was not successful");
SolverResult unsatResult = SolverResult.newUNSAT();
return unsatResult;
}
}
use of org.evosuite.symbolic.expr.fp.RealVariable in project evosuite by EvoSuite.
the class EvoSuiteSolver method randomizeValues.
private static void randomizeValues(Set<Variable<?>> variables, Set<Object> constants) {
Set<String> stringConstants = new HashSet<String>();
Set<Long> longConstants = new HashSet<Long>();
Set<Double> realConstants = new HashSet<Double>();
for (Object o : constants) {
if (o instanceof String)
stringConstants.add((String) o);
else if (o instanceof Double)
realConstants.add((Double) o);
else if (o instanceof Long)
longConstants.add((Long) o);
else
assert (false) : "Unexpected constant type: " + o;
}
for (Variable<?> v : variables) {
if (v instanceof StringVariable) {
StringVariable sv = (StringVariable) v;
if (!stringConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
sv.setConcreteValue(Randomness.choice(stringConstants));
} else {
sv.setConcreteValue(Randomness.nextString(Properties.STRING_LENGTH));
}
} else if (v instanceof IntegerVariable) {
IntegerVariable iv = (IntegerVariable) v;
if (!longConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
iv.setConcreteValue(Randomness.choice(longConstants));
} else {
iv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
}
} else if (v instanceof RealVariable) {
RealVariable rv = (RealVariable) v;
if (!realConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
rv.setConcreteValue(Randomness.choice(realConstants));
} else {
rv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
}
}
}
}
use of org.evosuite.symbolic.expr.fp.RealVariable in project evosuite by EvoSuite.
the class SymbolicObserver method buildRealVariable.
private RealVariable buildRealVariable(String name, double conV, double minValue, double maxValue) {
RealVariable realVariable;
if (realVariables.containsKey(name)) {
realVariable = realVariables.get(name);
realVariable.setConcreteValue(conV);
assert minValue == realVariable.getMinValue();
assert maxValue == realVariable.getMaxValue();
} else {
realVariable = new RealVariable(name, conV, minValue, maxValue);
realVariables.put(name, realVariable);
}
return realVariable;
}
use of org.evosuite.symbolic.expr.fp.RealVariable in project evosuite by EvoSuite.
the class Solver method setConcreteValues.
/**
* Restore all concrete values of the variables using the concrete_values
* mapping.
*
* @param variables
* @param concrete_values
*/
protected static void setConcreteValues(Set<Variable<?>> variables, Map<String, Object> concrete_values) {
for (Variable<?> v : variables) {
String var_name = v.getName();
if (!concrete_values.containsKey(var_name)) {
continue;
}
Object concreteValue = concrete_values.get(var_name);
if (v instanceof StringVariable) {
StringVariable sv = (StringVariable) v;
String concreteString = (String) concreteValue;
sv.setConcreteValue(concreteString);
} else if (v instanceof IntegerVariable) {
IntegerVariable iv = (IntegerVariable) v;
Long concreteInteger = (Long) concreteValue;
iv.setConcreteValue(concreteInteger);
} else if (v instanceof RealVariable) {
RealVariable ir = (RealVariable) v;
Double concreteReal = (Double) concreteValue;
ir.setConcreteValue(concreteReal);
} else {
logger.warn("unknow variable type " + v.getClass().getName());
}
}
}
use of org.evosuite.symbolic.expr.fp.RealVariable in project evosuite by EvoSuite.
the class TestRealSearch method testLTVariable.
@Test
public void testLTVariable() {
double var1 = 2.6576;
double var2 = 1.434;
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new RealConstraint(new RealVariable("test1", var1, -1000000.0, 1000000.0), Comparator.LT, new RealVariable("test2", var2, -1000000.0, 1000000.0)));
EvoSuiteSolver skr = new EvoSuiteSolver();
Map<String, Object> result;
try {
result = solve(skr, constraints);
assertNotNull(result);
if (result.containsKey("test1"))
var1 = ((Number) result.get("test1")).doubleValue();
if (result.containsKey("test2"))
var2 = ((Number) result.get("test2")).doubleValue();
assertTrue(var1 < var2);
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations