Search in sources :

Example 11 with RealVariable

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;
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Variable(org.evosuite.symbolic.expr.Variable) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) SolverResult(org.evosuite.symbolic.solver.SolverResult) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable)

Example 12 with RealVariable

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);
            }
        }
    }
}
Also used : IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) HashSet(java.util.HashSet)

Example 13 with RealVariable

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;
}
Also used : RealVariable(org.evosuite.symbolic.expr.fp.RealVariable)

Example 14 with 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());
        }
    }
}
Also used : IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable)

Example 15 with RealVariable

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();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Constraint(org.evosuite.symbolic.expr.Constraint) RealConstraint(org.evosuite.symbolic.expr.RealConstraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) RealConstraint(org.evosuite.symbolic.expr.RealConstraint) Test(org.junit.Test)

Aggregations

RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)29 Constraint (org.evosuite.symbolic.expr.Constraint)23 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)22 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)22 ArrayList (java.util.ArrayList)21 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)21 Test (org.junit.Test)20 RealConstant (org.evosuite.symbolic.expr.fp.RealConstant)16 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)4 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)4 RealBinaryExpression (org.evosuite.symbolic.expr.fp.RealBinaryExpression)2 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)2 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)2 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)2 VariableReference (org.evosuite.testcase.variable.VariableReference)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Variable (org.evosuite.symbolic.expr.Variable)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1 SmtAssertion (org.evosuite.symbolic.solver.smt.SmtAssertion)1