我已经漫步到这里的游泳池的深处。我已经取得了一些不错的进展,但现在只是在挣扎。我正在尝试在 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 实例中分配的内存呢?好像我需要在删除实例之前调用一个方法来释放它。
例如:被包装的类有一些子类的实例变量——我的清理功能是否足以正确管理内存?
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();
}
测试.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("--");
}
}