本文整理汇总了C++中Energy::AddNode方法的典型用法代码示例。如果您正苦于以下问题:C++ Energy::AddNode方法的具体用法?C++ Energy::AddNode怎么用?C++ Energy::AddNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Energy
的用法示例。
在下文中一共展示了Energy::AddNode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadUAI
Energy* ReadUAI(const char* filename)
{
ReadLinesFromFile F(filename);
if (!F.exists()) { printf("Can't open %s\n", filename); exit(1); }
Energy* g = NULL;
int phase = 0; // 0: reading # nodes
// 1: reading # states
// 2: reading # terms
// 3: reading term descriptions
// 4: reading term costs
// 5: finished
int node_num = 0, t = 0, term_num = 0;
struct Term
{
int arity;
int* nodes;
};
Term* terms = NULL;
Buffer buf(1024);
ReadNumbers<int> read_ints;
ReadNumbers<double> read_doubles;
char* line;
while ( (line=F.NextLine()) != NULL )
{
int i, num = read_ints.ReadLine(line);
if (num == 0) continue;
if (phase == 0)
{
if (num != 1) { printf("Error in line %d: # nodes expected\n", F.CurrentLine()); exit(1); }
node_num = read_ints.GetNumber(0);
if (node_num < 1) { printf("Error in line %d: # of nodes should be positive\n", F.CurrentLine()); exit(1); }
g = new Energy(node_num);
phase ++;
}
else if (phase == 1)
{
if (num != node_num) { printf("Error in line %d: # of nodes do not match\n", F.CurrentLine()); exit(1); }
for (i=0; i<node_num; i++) g->AddNode(read_ints.GetNumber(i));
phase ++;
}
else if (phase == 2)
{
if (num != 1) { printf("Error in line %d: # terms expected\n", F.CurrentLine()); exit(1); }
term_num = read_ints.GetNumber(0);
if (term_num < 0) { printf("Error in line %d: # of terms should be non-negative\n", F.CurrentLine()); exit(1); }
t = 0;
terms = new Term[term_num];
phase ++;
}
else if (phase == 3)
{
num --;
if (num != read_ints.GetNumber(0)) { printf("Error in line %d: incorrect term entry\n", F.CurrentLine()); exit(1); }
if (num < 1) { printf("Error in line %d: arity must be positive\n", F.CurrentLine()); exit(1); }
terms[t].arity = num;
terms[t].nodes = (int*)buf.Alloc(num*sizeof(int));
for (i=0; i<num; i++)
{
int q = read_ints.GetNumber(i+1);
if (q<0 || q>=node_num) { printf("Error in line %d: incorrect index\n", F.CurrentLine()); exit(1); }
terms[t].nodes[i] = q;
}
t ++;
if (t == term_num) { phase ++; t = 0; }
}
else if (phase == 4)
{
if (num != 1) { printf("Error in line %d: # costs expected\n", F.CurrentLine()); exit(1); }
num = read_ints.GetNumber(0);
line = F.NextLine();
if (!line) { printf("Error in line %d: missing cost table\n", F.CurrentLine()); exit(1); }
int double_num = read_doubles.ReadLine(line);
double* arr = read_doubles.GetNumbersArray();
if (TAKE_LOG)
{
for (i=0; i<double_num; i++) arr[i] = log(arr[i]);
}
for (i=0; i<double_num; i++) arr[i] = -arr[i]; // we are minimizing, not maximizing!
if (num == -1)
{
if (terms[t].arity != 2 || g->GetK(terms[t].nodes[0]) != g->GetK(terms[t].nodes[1]))
{ printf("Error in line %d: not a potts term\n", F.CurrentLine()); exit(1); }
if ( 1 )
{
if (!potts) potts = new PottsFactorType;
g->AddFactor(2, terms[t].nodes, arr, potts);
}
else
{
int x, y, i=0, K = g->GetK(terms[t].nodes[0]);
double lambda = arr[0];
double* V = new double[K*K];
for (x=0; x<K; x++)
for (y=0; y<K; y++)
{
V[i ++] = (x == y) ? 0 : lambda;
//.........这里部分代码省略.........