我已經在這裏徘徊到游泳池的盡頭。我取得了一些很好的進展,但現在我只是在肆虐。我試圖使用iOS的這一模糊邏輯的lib:http://code.google.com/p/fuzzy-lite/在iOS中使用C++ fuzzylite lib和ObjC(模糊邏輯)
我得到了它的編譯 - 我所做的是雙方在.cpp添加&的.h文件到我的項目,改變了後綴我主視圖控制器爲「.mm」。我能夠從viewDidload(show below)中運行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;
}
-header用於從ObjC包裝紙包裝的CPP類
#include "fuzzylite/FuzzyLite.h"
namespace fl {
class Bingo {
public:
FuzzyEngine* engine;
OutputLVar* health;
InputLVar* energy;
Bingo();
void Fuzz();
void setInput(float input);
};
}
:
- (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("--");
}
}
感謝回覆+對不完整的代碼抱歉。我想問的是,如果我新建一個類賓果實例,然後創建其他類的一些實例作爲屬性,那麼在刪除賓果實例之前,我需要定義一些「清理」方法來刪除這些屬性。 (即我需要清理我**新** ** - 內部賓果屬性不會被釋放否則)。 –
我在我的答案中增加了一些指導信息。 – jstevenco
再次感謝!我知道了。 –