package ec.gp.semantic.initialization;

import ec.EvolutionState;
import ec.app.semanticGP.func.numeric.Constant;
import ec.gp.*;
import ec.util.Parameter;
import ec.util.ParameterDatabase;

/**
 * @author Tomasz Pawlak
 */
public class DoubleRandomConstantNodeBuilder extends GPNodeBuilder {

    private final static Parameter DEFAULT_BASE = new Parameter("");
    private final static String FROM = "from";
    private final static String TO = "to";

    private double from = 0.0;
    private double to = 1.0;

    @Override
    public Parameter defaultBase() {
        return DEFAULT_BASE;
    }

    @Override
    public void setup(EvolutionState state, Parameter base) {
        super.setup(state, base);

        ParameterDatabase db = state.parameters;

        this.from = db.getDoubleWithDefault(base.push(FROM), defaultBase().push(FROM), this.from);
        this.to = db.getDoubleWithDefault(base.push(TO), defaultBase().push(TO), this.to);

        assert this.from <= this.to;
    }

    @Override
    public GPNode newRootedTree(EvolutionState state, GPType type, int thread, GPNodeParent parent, GPFunctionSet set, int argposition, int requestedSize) {
        double rand = state.random[thread].nextDouble();
        rand = rand * (this.to - this.from) + this.from;

        assert this.from <= rand && rand <= this.to;

        return new Constant(rand);
    }


}
