home Forums # Technical Support Unhandled exception Reply To: Unhandled exception

#6360
Unknown
Member

Hi, thanks for the quick reply.

When using “engine->toString()” it does print out my two inputs, output, and all its proportions as well as my Rule Block. But no matter what I do, I can’t seem to get my output based on mt data input or defuzzify it.

Any help is appreciated.

Here is my code if that helps at all

int main(int argc, char* argv[]) {
    using namespace fl;

    Engine* engine = new Engine;
    engine->setName("CAR_AI");
    engine->setDescription("");

    InputVariable* DistanceVariableInput = new InputVariable;
    DistanceVariableInput->setName("DistanceVariableInput");
    DistanceVariableInput->setDescription("");
    DistanceVariableInput->setEnabled(true);
    DistanceVariableInput->setRange(-1.000, 1.000);
    DistanceVariableInput->setLockValueInRange(false);
    DistanceVariableInput->addTerm(new Trapezoid("FarLeftOfLine", -1.000, -1.000, -0.900, -0.600));
    DistanceVariableInput->addTerm(new Trapezoid("LeftOfLine", -0.900, -0.600, -0.400, -0.100));
    DistanceVariableInput->addTerm(new Triangle("OnLine", -0.100, 0.000, 0.100));
    DistanceVariableInput->addTerm(new Trapezoid("RightOfLine", 0.100, 0.400, 0.600, 0.900));
    DistanceVariableInput->addTerm(new Trapezoid("FarRightOfLine", 0.700, 0.900, 1.000, 1.000));
    engine->addInputVariable(DistanceVariableInput);

    InputVariable* LinearVelocityInput = new InputVariable;
    LinearVelocityInput->setName("LinearVelocityInput");
    LinearVelocityInput->setDescription("");
    LinearVelocityInput->setEnabled(true);
    LinearVelocityInput->setRange(-1.000, 1.000);
    LinearVelocityInput->setLockValueInRange(false);
    LinearVelocityInput->addTerm(new Trapezoid("MovingLeftFast", -1.000, -1.000, -0.800, -0.600));
    LinearVelocityInput->addTerm(new Trapezoid("MoveingLeft", -0.800, -0.600, -0.300, -0.100));
    LinearVelocityInput->addTerm(new Triangle("GoingStraight", -0.800, 0.000, 0.800));
    LinearVelocityInput->addTerm(new Trapezoid("MovingRight", 0.100, 0.300, 0.600, 0.800));
    LinearVelocityInput->addTerm(new Trapezoid("MovingRightFast", 0.600, 0.800, 1.000, 1.000));
    engine->addInputVariable(LinearVelocityInput);

    OutputVariable* Steering = new OutputVariable;
    Steering->setName("Steering");
    Steering->setDescription("");
    Steering->setEnabled(true);
    Steering->setRange(-1.000, 1.000);
    Steering->setLockValueInRange(false);
    Steering->setAggregation(new Maximum);
    Steering->setDefuzzifier(new Centroid(100));
    Steering->setDefaultValue(fl::nan);
    Steering->setLockPreviousValue(false);
    Steering->addTerm(new Triangle("MakeAHardLeft", -1.000, -1.000, -0.900));
    Steering->addTerm(new Triangle("MakeALeft", -1.400, -0.900, -0.400));
    Steering->addTerm(new Triangle("GoStraight", -1.500, 0.000, 1.500));
    Steering->addTerm(new Triangle("MakeARight", 0.400, 0.900, 1.400));
    Steering->addTerm(new Triangle("MakeAHardRight", 0.900, 1.000, 1.000));
    engine->addOutputVariable(Steering);

    RuleBlock* OutputRule = new RuleBlock;
    OutputRule->setName("OutputRule");
    OutputRule->setDescription("");
    OutputRule->setEnabled(true);
    OutputRule->setConjunction(fl::null);
    OutputRule->setDisjunction(fl::null);
    OutputRule->setImplication(new AlgebraicProduct);
    OutputRule->setActivation(new General);

    OutputRule->addRule(Rule::parse("if DistanceVariableInput is OnLine and LinearVelocityInput is GoingStraight then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is OnLine and LinearVelocityInput is MoveingLeft then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is OnLine and LinearVelocityInput is MovingLeftFast then Steering is MakeARight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is OnLine and LinearVelocityInput is MovingRight then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is OnLine and LinearVelocityInput is MovingRightFast then Steering is MakeALeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is LeftOfLine and LinearVelocityInput is GoingStraight then Steering is MakeARight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is LeftOfLine and LinearVelocityInput is MoveingLeft then Steering is MakeARight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is LeftOfLine and LinearVelocityInput is MovingLeftFast then Steering is MakeAHardRight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is LeftOfLine and LinearVelocityInput is MovingRight then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is LeftOfLine and LinearVelocityInput is MovingRightFast then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarLeftOfLine and LinearVelocityInput is GoingStraight then Steering is MakeARight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarLeftOfLine and LinearVelocityInput is MoveingLeft then Steering is MakeAHardRight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarLeftOfLine and LinearVelocityInput is MovingLeftFast then Steering is MakeAHardRight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarLeftOfLine and LinearVelocityInput is MovingRight then Steering is MakeARight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarLeftOfLine and LinearVelocityInput is MovingRightFast then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is RightOfLine and LinearVelocityInput is GoingStraight then Steering is MakeALeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is RightOfLine and LinearVelocityInput is MoveingLeft then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is RightOfLine and LinearVelocityInput is MovingLeftFast then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is RightOfLine and LinearVelocityInput is MovingRight then Steering is MakeALeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is RightOfLine and LinearVelocityInput is MovingRightFast then Steering is MakeAHardLeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarRightOfLine and LinearVelocityInput is GoingStraight then Steering is MakeALeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarRightOfLine and LinearVelocityInput is MoveingLeft then Steering is MakeALeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarRightOfLine and LinearVelocityInput is MovingLeftFast then Steering is GoStraight", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarRightOfLine and LinearVelocityInput is MovingRight then Steering is MakeAHardLeft", engine));
    OutputRule->addRule(Rule::parse("if DistanceVariableInput is FarRightOfLine and LinearVelocityInput is MovingRightFast then Steering is MakeAHardLeft", engine));

    engine->addRuleBlock(OutputRule);

    engine->getInputVariable(0)->setValue(-1.000);
    engine->getInputVariable(1)->setValue(-1.000);

    engine->process();

    printf("%f\n", engine->toString());

    OutputVariable* outputVariable = engine->getOutputVariable(0);
    printf("%f\n", outputVariable->fuzzyOutputValue());

    //OutputVariable* output = engine->getOutputVariable(0)->defuzzify();
    //printf("%f\n", output);
}