c++ - 在 iOS 中使用 C++ fuzzylite 库和 ObjC(模糊逻辑)

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

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

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

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

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

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

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

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

void Bingo::cleanup(){

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

}

- 包装的 CPP 类的 header

#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();

}

测试.h

#ifndef FL_TEST_H
#define FL_TEST_H

namespace fl {

class Test {
public:
    static void SimpleMamdani();

};
}


#endif  /* FL_TEST_H */

测试.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("--");
    }
}

最佳答案

鉴于所提供的信息,基本上不可能回答您的问题。您的问题是关于 Bingo 类的 cleanup 方法,但是 Bingo 的实例(在堆栈或堆上)在您的代码摘录中没有出现。同样,您声明您正在清理“包装的 CPP 实例”,但在其他任何地方都没有引用它。它确实似乎在您的Test::SimplMamdani 方法中存在泄漏——您新建 那里的一堆对象 [at least in the revealed code] 有相应的delete。同样,在您的 FuzzyLiteIOSViewController::viewDidLoad 方法中,您在堆上创建了一个 Test 实例,但没有相应的 delete。我假设在您的 C++ 代码中没有发生 autoptr 的事情。

已更新以提供更多信息:

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

宾果.h:

namespace fl {
    class Bingo {
    public:

        Bingo();
        virtual ~Bingo();

        void Fuzz();
        void setInput(float input);

        FuzzyEngine* engine;
        OutputLVar* health;
        InputLVar* energy;

    protected:
    private:
    };
}

宾果.cpp:

using namespace fl;

Bingo::Bingo() {
}

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

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

可以说您的成员变量(engine、health 和 energy)在范围内应该是私有(private)的,并通过公共(public)范围的 getter 和 setter 公开。

拿一份 Bjarne Stroustrup 的 C++ 引用并快速阅读它,或者使用像 this one 这样的在线入门指南。 .

关于c++ - 在 iOS 中使用 C++ fuzzylite 库和 ObjC(模糊逻辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8094674/

相关文章:

c++ - 'new' 的字符串分配问题

c++ - 一次写入多个文件

objective-c - 在 UISplitView 中更改 RootViewController 的颜色

ios - 在串行执行其他异步任务之后执行异步任务

ios - 将一个大的 swift 项目拆分为具有核心框架和模块框架的框架

ios - Swift 2 中数组的扩展

c++ - 包括与用户定义的类名冲突的 Windows header

c++ - 我是否应该将使用 push_back 的代码更改为使用 std::move?

objective-c - 用循环顺序显示图像

ios - 滚动到该区域时,Tiled ScrollView 不显示某些图 block