- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在这里发帖询问是否有一种方法可以交替使用不同的分支策略。让我解释一下,我有一个有效的分支策略,我们称之为策略A。最大的问题是策略 A 不能经常使用。因此,当我无法使用策略 A 时,我会使用另一种策略,我将其称为策略 B,但效率较低。
文档说:
Brancher order. Creating a brancher registers it with its home space. A space maintains a queue of its branchers in that the brancher that is registered first is also used first for branching. The first brancher in the queue of branchers is referred to as the current brancher.
所以,我假设如果我先发布分支器 A,然后发布分支器 B,则分支器 A 将具有优先权,并且每次 status
A 的 > 表示没有要执行的分支,将使用分支器 B。似乎我错了,因为当分支器的 status
返回 false
时,它再也不会被调用。这是一个“最小示例”:
#include <gecode/minimodel.hh>
#include <iostream>
using namespace Gecode;
using namespace std;
class MyChoice : public Choice {
public:
int pos; // Position of the variable
int val; // Value of to assign
MyChoice(const Brancher& b, int pos0, int val0)
: Choice(b,2), pos(pos0), val(val0) {}
// Report size occupied
virtual size_t size(void) const {
return sizeof(*this);
}
// Archive into e
virtual void archive(Archive& e) const {
Choice::archive(e);
e << pos << val;
}
};
class BranchA : public Brancher {
protected:
ViewArray<Int::IntView> x;
public:
BranchA(Home home, ViewArray<Int::IntView>& x0)
: Brancher(home), x(x0) {}
static void post(Home home, ViewArray<Int::IntView>& x) {
(void) new (home) BranchA(home,x);
}
virtual size_t dispose(Space& home) {
(void) Brancher::dispose(home);
return sizeof(*this);
}
BranchA(Space& home, bool share, BranchA& b)
: Brancher(home,share,b) {
x.update(home,share,b.x);
}
virtual Brancher* copy(Space& home, bool share) {
return new (home) BranchA(home,share,*this);
}
// status
virtual bool status(const Space& home) const {
for (int i=0; i<x.size(); i++)
if (!x[i].assigned())
return !i%2 && x[i].in(1);
return false;
}
// choice
virtual Choice* choice(Space& home) {
for (int i=0; true; i++)
if (!x[i].assigned())
return new MyChoice(*this,i,1);
GECODE_NEVER;
return NULL;
}
virtual Choice* choice(const Space&, Archive& e) {
int pos, val;
e >> pos >> val;
return new MyChoice(*this, pos, val);
}
// commit
virtual ExecStatus commit(Space& home,
const Choice& c,
unsigned int a) {
const MyChoice& pv = static_cast<const MyChoice&>(c);
int pos=pv.pos, val=pv.val;
if (a == 0)
return me_failed(x[pos].eq(home,val)) ? ES_FAILED : ES_OK;
else
return me_failed(x[pos].nq(home,val)) ? ES_FAILED : ES_OK;
}
};
void branchA(Home home, const IntVarArgs& x) {
if (home.failed()) return;
ViewArray<Int::IntView> y(home,x);
BranchA::post(home,y);
}
// BranchB //////////////////////////////////////////////////////
class BranchB : public Brancher {
protected:
ViewArray<Int::IntView> x;
public:
BranchB(Home home, ViewArray<Int::IntView>& x0)
: Brancher(home), x(x0) {}
static void post(Home home, ViewArray<Int::IntView>& x) {
(void) new (home) BranchB(home,x);
}
virtual size_t dispose(Space& home) {
(void) Brancher::dispose(home);
return sizeof(*this);
}
BranchB(Space& home, bool share, BranchB& b)
: Brancher(home,share,b) {
x.update(home,share,b.x);
}
virtual Brancher* copy(Space& home, bool share) {
return new (home) BranchB(home,share,*this);
}
// status
virtual bool status(const Space& home) const {
for (int i=0; i<x.size(); i++)
if (!x[i].assigned())
return i%2 && x[i].in(2);
return false;
}
// choice
virtual Choice* choice(Space& home) {
for (int i=0; true; i++)
if (!x[i].assigned())
return new MyChoice(*this,i,2);
GECODE_NEVER;
return NULL;
}
virtual Choice* choice(const Space&, Archive& e) {
int pos, val;
e >> pos >> val;
return new MyChoice(*this, pos, val);
}
// commit
virtual ExecStatus commit(Space& home,
const Choice& c,
unsigned int a) {
const MyChoice& pv = static_cast<const MyChoice&>(c);
int pos=pv.pos, val=pv.val;
if (a == 0)
return me_failed(x[pos].eq(home,val)) ? ES_FAILED : ES_OK;
else
return me_failed(x[pos].nq(home,val)) ? ES_FAILED : ES_OK;
}
};
void branchB(Home home, const IntVarArgs& x) {
if (home.failed()) return;
ViewArray<Int::IntView> y(home,x);
BranchB::post(home,y);
}
// Minimal Space ///////////////////////////////////////
class TestSpace : public Space {
protected:
IntVarArray x;
public:
TestSpace(int size)
: x(*this, size, 0, 10) {
branchA(*this, x);
branchB(*this, x);
}
TestSpace (bool share, TestSpace& s)
: Space(share, s) {
x.update(*this, share, s.x);
}
virtual Space* copy (bool share) {
return new TestSpace(share, *this);
}
void print(std::ostream& os) {
os << "x= " << x << endl;
}
};
// Minimal Main //////////////////////:
int main (int, char**) {
// create model and search engine
TestSpace* m = new TestSpace(10);
DFS<TestSpace> e(m);
delete m;
// search and print all solutions
while (TestSpace* s = e.next()) {
s->print(cout); delete s;
}
return 0;
}
在此示例中,如果要分配的下一个变量在偶数 索引,如果变量可以取 1
的值(false
else)。如果要分配的下一个变量在奇数索引上并且如果变量可以取 2
的值(false
else)。使用该代码,我希望得到解决方案 [1, 2, 1, 2, ...]
和 [!1, !2, !1, !2, ...]
(以及其他组合,例如 [!1, 2, 1, !2, ...]
)但是由于当分支器的 status
返回 false
,只有前两个变量被赋值。
是否有一种好方法可以使分支器在其 status
返回 false
后不被处理(或交替使用两种不同的分支策略),或者我应该合并这两个分支器成一个?
最佳答案
如果它可以帮助某人,这是我使用的解决方案。正如 Patrick Trentin 所建议的那样,我通过制作第三个分支器来统一控制,该分支器是分支器的 vector 。这是我使用的实现:
header branchAllInOne.h:
#include <gecode/minimodel.hh>
using namespace Gecode;
using namespace std;
class BranchAllInOne : public Brancher {
protected:
// Queue of brancher (may be better with ActorLink)
vector<Actor *> queue;
// Every brancher are in the brancher
BrancherGroup group;
mutable int toChoose;
class ChoiceAndID : public Choice {
public:
// Choice of the brancher used
Choice* c;
/// ID of brancher used
unsigned int id;
ChoiceAndID(const Brancher& b, Choice * c, unsigned int id);
virtual ~ChoiceAndID();
virtual size_t size(void) const ;
virtual void archive(Archive& e) const ;
};
public:
BranchAllInOne(Home home);
virtual size_t dispose(Space& home);
BranchAllInOne(Home home, bool share, BranchAllInOne& b);
virtual ~BranchAllInOne();
/**
* Check status of brancher, set toChoose value to the ID of the first
* brancher with alternative left
**/
virtual bool status(const Space&) const ;
/**
* Let the brancher of ID toChoose make the choice
*/
virtual Choice* choice(Space&);
virtual Choice* choice(const Space&, Archive& e);
/**
* Let the brancher of ID toChoose commit his choice
*/
virtual ExecStatus commit(Space& home, const Choice& _c, unsigned int a);
/// Copy brancher
virtual Actor* copy(Space& home, bool share);
/// Post brancher
static BranchAllInOne * post(Home home);
virtual void print(const Space& home,
const Choice& c,
unsigned int a,
ostream& o) const ;
void pushBrancher(Space& home, Brancher *b);
};
BranchAllInOne * branchAllInOne(Home home);
实现branchAllInOne.cpp:
#include "branchAllInOne.h"
static Brancher * ActorToBrancher(Actor *a);
// Choice implementation
BranchAllInOne::ChoiceAndID::ChoiceAndID(const Brancher& b, Choice * c0, unsigned int id0)
: Choice(b, c0->alternatives()),
c(c0),
id(id0){}
BranchAllInOne::ChoiceAndID::~ChoiceAndID() {
delete c;
}
size_t BranchAllInOne::ChoiceAndID::size(void) const {
return sizeof(*this) + c->size();
}
void BranchAllInOne::ChoiceAndID::archive(Archive& e) const {
Choice::archive(e);
c->archive(e);
}
BranchAllInOne::BranchAllInOne(Home home)
: Brancher(home),
toChoose(-1) {
home.notice(*this,AP_DISPOSE);
}
// brancher
BranchAllInOne * BranchAllInOne::post(Home home) {
return new (home) BranchAllInOne(home);
}
size_t BranchAllInOne::dispose(Space& home) {
home.ignore(*this, AP_DISPOSE);
size_t size = queue.size() * sizeof(Actor*);
for (unsigned int i = queue.size() ; i--;) {
size += ActorToBrancher(queue[i])->dispose(home);
}
queue.~vector();
// Making sure to kill each brancher inserted in the queue (may be useless)
group.kill(home);
(void) Brancher::dispose(home);
return sizeof(*this) + size;
}
BranchAllInOne::BranchAllInOne(Home home, bool share, BranchAllInOne& b)
: Brancher(home, share, b),
queue(b.queue.size()),
toChoose(b.toChoose){
for (unsigned int i = 0 ; i < queue.size() ; i++)
queue[i] = b.queue[i]->copy(home, share);
}
BranchAllInOne::~BranchAllInOne() {
for (unsigned int i = 0 ; i < queue.size() ; i++) {
delete queue[i];
}
queue.~vector();
}
Actor* BranchAllInOne::copy(Space& home, bool share){
return new (home) BranchAllInOne(home, share, *this);
}
// status
bool BranchAllInOne::status(const Space& s) const {
for (unsigned int i = 0 ; i < queue.size() ; i++) {
if (ActorToBrancher(queue[i])->status(s)) {
toChoose = i;
return true;
}
}
std::cout << std::endl;
return false;
}
// choice
Choice* BranchAllInOne::choice(Space& s) {
ChoiceAndID* res = new ChoiceAndID(*this,
const_cast<Choice *>(ActorToBrancher(queue[toChoose])->choice(s)),
toChoose);
toChoose = -1;
return res;
}
Choice* BranchAllInOne::choice(const Space& s, Archive& e) {
return new ChoiceAndID(*this,
const_cast<Choice *>(ActorToBrancher(queue[toChoose])->choice(s, e)),
toChoose);
}
// Perform commit for choice \a _c and alternative \a a
ExecStatus BranchAllInOne::commit(Space& home, const Choice& c, unsigned int a) {
const BranchAllInOne::ChoiceAndID& ch = static_cast<const BranchAllInOne::ChoiceAndID&>(c);
return ActorToBrancher(queue[ch.id])->commit(home, const_cast<Choice&>(*ch.c), a);
}
void BranchAllInOne::print(const Space& home,
const Choice& c,
unsigned int a,
ostream& o) const {
const BranchAllInOne::ChoiceAndID& ch = static_cast<const BranchAllInOne::ChoiceAndID&>(c);
o << ch.id << ": ";
ActorToBrancher(queue[ch.id])->print(home, *(ch.c), a, o);
}
void BranchAllInOne::pushBrancher(Space &home, Brancher *b) {
queue.push_back(b);
group.move(home, *b);
}
static Brancher * ActorToBrancher(Actor *a) {
return dynamic_cast<Brancher *>(a);
}
// end of BranchAllInOne implementation
BranchAllInOne* branchAllInOne(Home home) {
if (home.failed()) return NULL;
return BranchAllInOne::post(home);
}
我做了一些修改以获得指向我想放入 vector 中的分支的指针(包括每个分支的 post 函数):brancher一个例子:
BranchA * BranchA::post(Home home, ViewArray<Int::IntView>& x) {
return new (home) BranchA(home,x);
}
BranchA * branchA(Home home, const IntVarArgs& x) {
if (home.failed()) return NULL;
ViewArray<Int::IntView> y(home,x);
return BranchA::post(home,y);
}
空间也做了修改:
TestSpace::TestSpace(int size)
: x(*this, size, 0, 10) {
BranchAllInOne * b = branchAllInOne(*this);
b->pushBrancher(*this, branchA(*this, x));
b->pushBrancher(*this, branchB(*this, x));
}
我在使用和不使用 Gist 的情况下对其进行了测试,并且对于放置在 vector 中的每个分支(这里只有两个)只有一个指针的内存泄漏。一个小问题是,在第三个分支停止后, vector 中的分支也会被调度(但它们的状态返回 false)。
关于c++ - Gecode 中的备用分支策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48785925/
我为 VS 2017 安装了 Gecode 6.0。将目录添加到/lib 和/include。然后将 gecode/int.hh 包含到 Hello World 程序中。编译过程中出现了 3 个奇怪的
我正在使用 minizinc 和 gecode 以分布式方式解决最小化问题。我有多个分布式服务器,它们使用相同的输入来解决相同的模型,并且我希望所有服务器都能获得相同的解决方案。 问题是模型有多个解决
我正在使用 minizinc 和 gecode 以分布式方式解决最小化问题。我有多个分布式服务器,它们使用相同的输入来解决相同的模型,并且我希望所有服务器都能获得相同的解决方案。 问题是模型有多个解决
我通过它的 C++ API 使用 Gecode,这是一种带有正面和负面示例的学习环境。在这种情况下,我有两个 BoolVarArray:positive_bags_ 和 negative_bags_。
我在这里发帖询问是否有一种方法可以交替使用不同的分支策略。让我解释一下,我有一个有效的分支策略,我们称之为策略A。最大的问题是策略 A 不能经常使用。因此,当我无法使用策略 A 时,我会使用另一种策略
在Gecode中,我使用评价函数在分支时选择变量。 为了计算变量 v 的值(value),我需要访问一些其他变量值,但看起来在调用值(value)函数时,空间变量尚未分配任何值: Exception:
在Gecode中,我使用评价函数在分支时选择变量。 为了计算变量 v 的值(value),我需要访问一些其他变量值,但看起来在调用值(value)函数时,空间变量尚未分配任何值: Exception:
我正在做一个 Gecode 项目,代码应该输出一个如下所示的文件: n: 17 x: {0, 0, 16, 18, 17, 31, 32, 0, 34, 10, 30, 37, 38, 30, 30,
我正在尝试为我在 Gecode 中的分支使用评价函数。在 MPG Gecode 手册中,陈述了一个评价函数示例,以及如何将其传递给分支。但我无法弄清楚将功能放在哪里。它应该在脚本内部还是外部?现在我把
关注这篇文章 - http://zverovich.net/2013/06/27/visualizing-geographical-ampl-data-using-ipython-and-google
我正在寻找一种方法来复制 Space Gecode 中的实例,然后分析空间之间的差异。 但是在第一个拷贝之后就已经出错了。当复制 Gecode 中的建模和编程 一书中的代码时,如下所示,并简单地修改它
我正在尝试在 CentOS 6.3 x64 上安装 chef-solo,当我运行时: sudo chef-solo -c /etc/chef/solo.rb -j ~/chef.json -r htt
一般来说,我是地理编码和约束编程的新手。 到目前为止,我在获取 gecode 时没有遇到太多问题,这太棒了。但我想知道执行“嵌套”成本函数的最佳方法是什么。具体来说,我希望最小化 X,但在 X 相等的
考虑以下 google map 地理编码响应的 JSON 响应。它由一组 address_components 组成有五个元素。通常开发人员使用它的第一个项目(索引为 0 的项目)来查找地址元素,例如
我是 CMake 新手,需要一些关于一个简单问题的指导:我按照 this link 在我的 Ubuntu 18.04 机器中从源代码构建了gecode 。该库安装在 /opt/gecode-relea
我使用 Ubuntu 18.04 并尝试编译 Gecode 带来的示例“money.cpp”文件。我下载了 (gecode-release-5.1.0.tar.gz) 并解压。然后安装 Gecode
在Win8.1上使用VisualStudio2015 Express 我已经设置了我的外部包含文件的位置进入项目配置/VC++包括目录: $(VC_IncludePath);"C:\Program F
gem install berkshelf失败 Failed to build gecode library在 Centos 6 中。它失败并显示以下输出。 # /opt/chef/embedded/
我是一名优秀的程序员,十分优秀!