【问题标题】:working with C++ fuzzylite lib and ObjC in iOS (fuzzy logic)在 iOS 中使用 C++ 模糊库和 ObjC(模糊逻辑)
【发布时间】:2011-11-11 13:31:07
【问题描述】:

我已经漫步到这里的池子深处。我已经取得了一些不错的进展,但现在只是在挣扎。我正在尝试在 iOS 中使用这个模糊逻辑库:http://code.google.com/p/fuzzy-lite/

我已经编译好了 - 我所做的是将 .cpp 和 .h 文件添加到我的项目中,并将我的主 viewController 上的后缀更改为“.mm”。我能够从 viewDidload 中运行fuzzyLite test.h 文件(如下所示)。它运行并显示测试数据。

我需要做的是创建一个fuzzyLite 的持久实例,以便我可以在我的应用程序中使用它(例如,能够解决它,然后在应用程序卸载时进行清理)。

我四处搜索,但不了解在 ObjC 项目中包含 C++ 代码的讨论/示例。有人可以告诉我一种可以继续前进的方法-包装fuzzyLite代码,以便我可以调用函数并取回结果吗?谢谢!

编辑:我使用此处详述的方法在这方面取得了进展: http://robnapier.net/blog/wrapping-c-take-2-1-486

我不清楚的一件事是内存清理。 dealloc 函数会清理被包装的 CPP 实例的实例——但是在 CCP 实例中分配的内存呢?好像我需要在删除实例之前调用一个方法来释放它。

例如:被包装的类有一些子类的实例变量——我的 cleanup 函数是否足以正确管理内存?

void Bingo::cleanup(){

delete  engine;
engine = NULL;
delete health;
health = NULL;
delete energy;
energy = NULL;

}

-封装 CPP 类的标头

#include "fuzzylite/FuzzyLite.h"

namespace fl {

class Bingo {
public:
    FuzzyEngine* engine;
    OutputLVar* health;
    InputLVar* energy;
    Bingo();
    void Fuzz();

    void setInput(float input);

};
}

来自 ObjC 包装器:

- (void)dealloc
{
delete _cpp;
_cpp = NULL;

[super dealloc];
}

FuzzyLiteIOSViewController.mm

#include "FuzzyLiteIOSViewController.h"
#include "FuzzyLite.h"
#include "test.h"
#include <limits>
#include "fuzzylite/FunctionTerm.h"

//stuff not shown

- (void)viewDidLoad
{
   [super viewDidLoad];

   fl::Test* test = new fl::Test();
   test->SimpleMamdani();

}

test.h

#ifndef FL_TEST_H
#define FL_TEST_H

namespace fl {

class Test {
public:
    static void SimpleMamdani();

};
}


#endif  /* FL_TEST_H */

test.cpp

#include "fuzzylite/test.h"
#include "fuzzylite/FuzzyLite.h"
#include <limits>

#include "fuzzylite/FunctionTerm.h"
namespace fl {

void Test::SimpleMamdani() {
    FuzzyOperator& op = FuzzyOperator::DefaultFuzzyOperator();
    FuzzyEngine engine("simple-mamdani", op);
    engine.hedgeSet().add(new fl::HedgeNot);
    engine.hedgeSet().add(new fl::HedgeSomewhat);
    engine.hedgeSet().add(new fl::HedgeVery);
    fl::InputLVar* energy = new fl::InputLVar("Energy");
    energy->addTerm(new fl::ShoulderTerm("LOW", 0.25, 0.5, true));
    energy->addTerm(new fl::TriangularTerm("MEDIUM", 0.25, 0.75));
    energy->addTerm(new fl::ShoulderTerm("HIGH", 0.50, 0.75, false));
    engine.addInputLVar(energy);

    fl::OutputLVar* health = new fl::OutputLVar("Health");
    health->addTerm(new fl::TriangularTerm("BAD", 0.0, 0.50));
    health->addTerm(new fl::TriangularTerm("REGULAR", 0.25, 0.75));
    health->addTerm(new fl::TriangularTerm("GOOD", 0.50, 1.00));
    engine.addOutputLVar(health);
    fl::RuleBlock* block = new fl::RuleBlock();
    block->addRule(new fl::MamdaniRule("if Energy is LOW then Health is BAD", engine));
    block->addRule(new fl::MamdaniRule("if Energy is MEDIUM then Health is REGULAR", engine));
    block->addRule(new fl::MamdaniRule("if Energy is HIGH then Health is GOOD", engine));
    engine.addRuleBlock(block);

    for (fl::flScalar in = 0.0; in < 1.1; in += 0.1) {
        energy->setInput(in);
        engine.process();
        fl::flScalar out = health->output().defuzzify();
        (void)out; //Just to avoid warning when building
        FL_LOG("Energy=" << in);
        FL_LOG("Energy is " << energy->fuzzify(in));
        FL_LOG("Health=" << out);
        FL_LOG("Health is " << health->fuzzify(out));
        FL_LOG("--");
    }
}

【问题讨论】:

    标签: c++ objective-c ios fuzzy-logic


    【解决方案1】:

    鉴于提供的信息,基本上无法回答您的问题。您的问题是关于 Bingo 类的 cleanup 方法,但是 Bingo 的实例(在堆栈或堆上)在您的代码摘录中没有出现。同样,您声明您正在清理“包装的 CPP 实例”,但在其他任何地方都没有引用它。 确实似乎您的Test::SimplMamdani 方法中有泄漏——您new 的一堆对象[至少在显示的代码中] 没有任何对应的deletes .同样,在您的FuzzyLiteIOSViewController::viewDidLoad 方法中,您在堆上创建一个Test 实例,而没有相应的delete。我假设你的 C++ 代码中没有 autoptr 的东西。

    已更新以提供更多信息:

    根据您的评论,您需要复习 C++ 的基本语言结构。基本规则是你需要delete 任何你new 的东西。 Bingo 类的清理应在析构函数 中执行(Objective-C 的dealloc 的C++ 构造)。你的 Bingo 类应该看起来更像:

    Bingo.h:

    namespace fl {
        class Bingo {
        public:
    
            Bingo();
            virtual ~Bingo();
    
            void Fuzz();
            void setInput(float input);
    
            FuzzyEngine* engine;
            OutputLVar* health;
            InputLVar* energy;
    
        protected:
        private:
        };
    }
    

    Bingo.cpp:

    using namespace fl;
    
    Bingo::Bingo() {
    }
    
    Bingo::~Bingo() {
        if (engine) {
            delete engine;
        }
        if (health) {
            delete health;
        }
        if (energy) {
            delete energy;
        }
    }
    

    当您delete 一个 Bingo 实例时,将调用析构函数并释放 Bingo 的成员变量。

    可以说,您的成员变量(引擎、健康和能源)在范围内应该是私有的,并通过公共范围的 getter 和 setter 公开。

    获取一份 Bjarne Stroustrup 的 C++ 参考并快速阅读,或使用在线入门指南,如 this one

    【讨论】:

    • 感谢您的回复 + 抱歉代码不完整。我想问的是,如果我新建了一个 Bingo 类的实例,然后它创建了其他类的一些实例作为属性,那么我需要定义一些“清理”方法来删除这些属性,然后再删除 Bingo 实例。 (即我需要清理我 new 的所有内容 - 否则不会释放内部 Bingo 属性)。
    猜你喜欢
    • 2011-05-17
    • 2016-03-17
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    相关资源
    最近更新 更多