#include "PBQP.h"
#include "llvm/Config/alloca.h"
#include <limits>
#include <cassert>
#include <cstring>
namespace llvm {
typedef struct adjnode {
struct adjnode *prev,
*succ,
*reverse;
int adj;
PBQPMatrix *costs;
bool tc_valid;
int *tc_safe_regs;
int tc_impact;
} adjnode;
typedef struct bucketnode {
struct bucketnode *prev;
struct bucketnode *succ;
int u;
} bucketnode;
struct pbqp {
int num_nodes;
int max_deg;
bool solved;
bool optimal;
PBQPNum min;
bool changed;
PBQPVector **node_costs;
int *node_deg;
int *solution;
adjnode **adj_list;
bucketnode **bucket_ptr;
int *stack;
int stack_ptr;
bucketnode **bucket_list;
int num_r0;
int num_ri;
int num_rii;
int num_rn;
int num_rn_special;
};
bool isInf(PBQPNum n) { return n == std::numeric_limits<PBQPNum>::infinity(); }
pbqp *alloc_pbqp(int num_nodes)
{
pbqp *this_;
int u;
assert(num_nodes > 0);
this_ = (pbqp *)malloc(sizeof(pbqp));
this_->num_nodes = num_nodes;
this_->solved = false;
this_->optimal = true;
this_->min = 0.0;
this_->max_deg = 0;
this_->changed = false;
this_->num_r0 = 0;
this_->num_ri = 0;
this_->num_rii = 0;
this_->num_rn = 0;
this_->num_rn_special = 0;
this_->stack = (int *) malloc(sizeof(int)*num_nodes);
this_->stack_ptr = 0;
this_->adj_list = (adjnode **) malloc(sizeof(adjnode *)*num_nodes);
this_->node_deg = (int *) malloc(sizeof(int)*num_nodes);
this_->solution = (int *) malloc(sizeof(int)*num_nodes);
this_->bucket_ptr = (bucketnode **) malloc(sizeof(bucketnode **)*num_nodes);
this_->node_costs = (PBQPVector**) malloc(sizeof(PBQPVector*) * num_nodes);
for(u=0;u<num_nodes;u++) {
this_->solution[u]=-1;
this_->adj_list[u]=NULL;
this_->node_deg[u]=0;
this_->bucket_ptr[u]=NULL;
this_->node_costs[u]=NULL;
}
this_->bucket_list = NULL;
return this_;
}
void free_pbqp(pbqp *this_)
{
int u;
int deg;
adjnode *adj_ptr,*adj_next;
bucketnode *bucket,*bucket_next;
assert(this_ != NULL);
for(u=0;u < this_->num_nodes;u++) {
delete this_->node_costs[u];
}
free(this_->node_costs);
for(deg=0;deg<=this_->max_deg;deg++) {
for(bucket=this_->bucket_list[deg];bucket!=NULL;bucket=bucket_next) {
this_->bucket_ptr[bucket->u] = NULL;
bucket_next = bucket-> succ;
free(bucket);
}
}
free(this_->bucket_list);
assert(this_->adj_list != NULL);
for(u=0;u < this_->num_nodes; u++) {
for(adj_ptr = this_->adj_list[u]; adj_ptr != NULL; adj_ptr = adj_next) {
adj_next = adj_ptr -> succ;
if (u < adj_ptr->adj) {
assert(adj_ptr != NULL);
delete adj_ptr->costs;
}
if (adj_ptr -> tc_safe_regs != NULL) {
free(adj_ptr -> tc_safe_regs);
}
free(adj_ptr);
}
}
free(this_->adj_list);
free(this_->node_deg);
free(this_->solution);
free(this_->bucket_ptr);
free(this_->stack);
free(this_);
}
static
adjnode *find_adjnode(pbqp *this_,int u,int v)
{
adjnode *adj_ptr;
assert (this_ != NULL);
assert (u >= 0 && u < this_->num_nodes);
assert (v >= 0 && v < this_->num_nodes);
assert(this_->adj_list != NULL);
for(adj_ptr = this_ -> adj_list[u];adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
if (adj_ptr->adj == v) {
return adj_ptr;
}
}
return NULL;
}
static
adjnode *alloc_adjnode(pbqp *this_,int u, PBQPMatrix *costs)
{
adjnode *p;
assert(this_ != NULL);
assert(costs != NULL);
assert(u >= 0 && u < this_->num_nodes);
p = (adjnode *)malloc(sizeof(adjnode));
assert(p != NULL);
p->adj = u;
p->costs = costs;
p->tc_valid= false;
p->tc_safe_regs = NULL;
p->tc_impact = 0;
return p;
}
static
void insert_adjnode(pbqp *this_, int u, adjnode *adj_ptr)
{
assert(this_ != NULL);
assert(adj_ptr != NULL);
assert(u >= 0 && u < this_->num_nodes);
if (this_ -> adj_list[u] != NULL) {
assert(this_->adj_list[u]->prev == NULL);
this_->adj_list[u] -> prev = adj_ptr;
}
adj_ptr -> succ = this_->adj_list[u];
adj_ptr -> prev = NULL;
this_->adj_list[u] = adj_ptr;
}
static
void remove_adjnode(pbqp *this_, int u, adjnode *adj_ptr)
{
assert(this_!= NULL);
assert(u >= 0 && u <= this_->num_nodes);
assert(this_->adj_list != NULL);
assert(adj_ptr != NULL);
if (adj_ptr -> prev == NULL) {
this_->adj_list[u] = adj_ptr -> succ;
} else {
adj_ptr -> prev -> succ = adj_ptr -> succ;
}
if (adj_ptr -> succ != NULL) {
adj_ptr -> succ -> prev = adj_ptr -> prev;
}
if(adj_ptr->reverse != NULL) {
adjnode *rev = adj_ptr->reverse;
rev->reverse = NULL;
}
if (adj_ptr -> tc_safe_regs != NULL) {
free(adj_ptr -> tc_safe_regs);
}
free(adj_ptr);
}
static
int get_deg(pbqp *this_,int u)
{
adjnode *adj_ptr;
int deg = 0;
assert(this_ != NULL);
assert(u >= 0 && u < this_->num_nodes);
assert(this_->adj_list != NULL);
for(adj_ptr = this_ -> adj_list[u];adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
deg ++;
}
return deg;
}
static
void reinsert_node(pbqp *this_,int u)
{
adjnode *adj_u,
*adj_v;
assert(this_!= NULL);
assert(u >= 0 && u <= this_->num_nodes);
assert(this_->adj_list != NULL);
for(adj_u = this_ -> adj_list[u]; adj_u != NULL; adj_u = adj_u -> succ) {
int v = adj_u -> adj;
adj_v = alloc_adjnode(this_,u,adj_u->costs);
insert_adjnode(this_,v,adj_v);
}
}
static
void remove_node(pbqp *this_,int u)
{
adjnode *adj_ptr;
assert(this_!= NULL);
assert(u >= 0 && u <= this_->num_nodes);
assert(this_->adj_list != NULL);
for(adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
remove_adjnode(this_,adj_ptr->adj,adj_ptr -> reverse);
}
}
static
void insert_edge(pbqp *this_, int u, int v, PBQPMatrix *costs)
{
adjnode *adj_u,
*adj_v;
adj_u = alloc_adjnode(this_,v,costs);
insert_adjnode(this_,u,adj_u);
adj_v = alloc_adjnode(this_,u,costs);
insert_adjnode(this_,v,adj_v);
adj_u -> reverse = adj_v;
adj_v -> reverse = adj_u;
}
static
void delete_edge(pbqp *this_,int u,int v)
{
adjnode *adj_ptr;
adjnode *rev;
assert(this_ != NULL);
assert( u >= 0 && u < this_->num_nodes);
assert( v >= 0 && v < this_->num_nodes);
adj_ptr=find_adjnode(this_,u,v);
assert(adj_ptr != NULL);
assert(adj_ptr->reverse != NULL);
delete adj_ptr -> costs;
rev = adj_ptr->reverse;
remove_adjnode(this_,u,adj_ptr);
remove_adjnode(this_,v,rev);
}
void add_pbqp_nodecosts(pbqp *this_,int u, PBQPVector *costs)
{
assert(this_ != NULL);
assert(costs != NULL);
assert(u >= 0 && u <= this_->num_nodes);
if (!this_->node_costs[u]) {
this_->node_costs[u] = new PBQPVector(*costs);
} else {
*this_->node_costs[u] += *costs;
}
}
static
PBQPMatrix *get_costmatrix_ptr(pbqp *this_, int u, int v)
{
adjnode *adj_ptr;
PBQPMatrix *m = NULL;
assert (this_ != NULL);
assert (u >= 0 && u < this_->num_nodes);
assert (v >= 0 && v < this_->num_nodes);
adj_ptr = find_adjnode(this_,u,v);
if (adj_ptr != NULL) {
m = adj_ptr -> costs;
}
return m;
}
static
PBQPMatrix *pbqp_get_costmatrix(pbqp *this_, int u, int v)
{
adjnode *adj_ptr = find_adjnode(this_,u,v);
if (adj_ptr != NULL) {
if ( u < v) {
return new PBQPMatrix(*adj_ptr->costs);
} else {
return new PBQPMatrix(adj_ptr->costs->transpose());
}
} else {
return NULL;
}
}
void add_pbqp_edgecosts(pbqp *this_,int u,int v, PBQPMatrix *costs)
{
PBQPMatrix *adj_costs;
assert(this_!= NULL);
assert(costs != NULL);
assert(u >= 0 && u <= this_->num_nodes);
assert(v >= 0 && v <= this_->num_nodes);
if (u == v) {
PBQPVector *diag = new PBQPVector(costs->diagonalize());
add_pbqp_nodecosts(this_,v,diag);
delete diag;
} else if ((adj_costs = get_costmatrix_ptr(this_,u,v))!=NULL) {
if ( u < v) {
*adj_costs += *costs;
} else {
*adj_costs += costs->transpose();
}
} else {
adj_costs = new PBQPMatrix((u < v) ? *costs : costs->transpose());
insert_edge(this_,u,v,adj_costs);
}
}
static
void pbqp_remove_bucket(pbqp *this_, bucketnode *bucket)
{
int u = bucket->u;
assert(this_ != NULL);
assert(u >= 0 && u < this_->num_nodes);
assert(this_->bucket_list != NULL);
assert(this_->bucket_ptr[u] != NULL);
if (bucket->prev != NULL) {
bucket->prev-> succ = bucket->succ;
} else {
this_->bucket_list[this_->node_deg[u]] = bucket -> succ;
}
if (bucket->succ != NULL) {
bucket->succ-> prev = bucket->prev;
}
}
static
int pop_node(pbqp *this_,int deg)
{
bucketnode *bucket;
int u;
assert(this_ != NULL);
assert(deg >= 0 && deg <= this_->max_deg);
assert(this_->bucket_list != NULL);
bucket = this_->bucket_list[deg];
assert(bucket != NULL);
pbqp_remove_bucket(this_,bucket);
u = bucket->u;
free(bucket);
return u;
}
static
void add_to_bucketlist(pbqp *this_,bucketnode *bucket, int deg)
{
bucketnode *old_head;
assert(bucket != NULL);
assert(this_ != NULL);
assert(deg >= 0 && deg <= this_->max_deg);
assert(this_->bucket_list != NULL);
this_->node_deg[bucket->u] = deg;
old_head = this_->bucket_list[deg];
bucket -> prev = NULL;
bucket -> succ = old_head;
this_ -> bucket_list[deg] = bucket;
if (bucket -> succ != NULL ) {
assert ( old_head -> prev == NULL);
old_head -> prev = bucket;
}
}
static
void reorder_node(pbqp *this_, int u)
{
int deg;
assert(this_ != NULL);
assert(u>= 0 && u < this_->num_nodes);
assert(this_->bucket_list != NULL);
assert(this_->bucket_ptr[u] != NULL);
deg = get_deg(this_,u);
if (deg != this_->node_deg[u]) {
pbqp_remove_bucket(this_,this_->bucket_ptr[u]);
add_to_bucketlist(this_,this_->bucket_ptr[u],deg);
}
}
static
void reorder_adjnodes(pbqp *this_,int u)
{
adjnode *adj_ptr;
assert(this_!= NULL);
assert(u >= 0 && u <= this_->num_nodes);
assert(this_->adj_list != NULL);
for(adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
reorder_node(this_,adj_ptr->adj);
}
}
static
void create_bucket(pbqp *this_,int u,int deg)
{
bucketnode *bucket;
assert(this_ != NULL);
assert(u >= 0 && u < this_->num_nodes);
assert(this_->bucket_list != NULL);
bucket = (bucketnode *)malloc(sizeof(bucketnode));
assert(bucket != NULL);
bucket -> u = u;
this_->bucket_ptr[u] = bucket;
add_to_bucketlist(this_,bucket,deg);
}
static
void create_bucketlist(pbqp *this_)
{
int u;
int max_deg;
int deg;
assert(this_ != NULL);
assert(this_->bucket_list == NULL);
max_deg = 2;
for(u=0;u<this_->num_nodes;u++) {
deg = this_->node_deg[u] = get_deg(this_,u);
if (deg > max_deg) {
max_deg = deg;
}
}
this_->max_deg = max_deg;
this_ -> bucket_list = (bucketnode **)malloc(sizeof(bucketnode *)*(max_deg + 1));
memset(this_->bucket_list,0,sizeof(bucketnode *)*(max_deg + 1));
assert(this_->bucket_list != NULL);
for(u=0;u<this_->num_nodes;u++) {
create_bucket(this_,u,this_->node_deg[u]);
}
}
static
void disconnect_trivialnode(pbqp *this_,int u)
{
int v;
adjnode *adj_ptr,
*next;
PBQPMatrix *c_uv;
PBQPVector *c_v;
assert(this_ != NULL);
assert(this_->node_costs != NULL);
assert(u >= 0 && u < this_ -> num_nodes);
assert(this_->node_costs[u]->getLength() == 1);
for(adj_ptr = this_->adj_list[u]; adj_ptr != NULL; adj_ptr = next){
next = adj_ptr -> succ;
v = adj_ptr -> adj;
assert(v >= 0 && v < this_ -> num_nodes);
c_uv = pbqp_get_costmatrix(this_,u,v);
c_v = new PBQPVector(c_uv->getRowAsVector(0));
*this_->node_costs[v] += *c_v;
delete c_v;
delete c_uv;
delete_edge(this_,u,v);
}
}
static
void eliminate_trivial_nodes(pbqp *this_)
{
int u;
assert(this_ != NULL);
assert(this_ -> node_costs != NULL);
for(u=0;u < this_ -> num_nodes; u++) {
if (this_->node_costs[u]->getLength() == 1) {
disconnect_trivialnode(this_,u);
}
}
}
static
bool normalize_matrix(PBQPMatrix *m, PBQPVector *u, PBQPVector *v)
{
assert( m != NULL);
assert( u != NULL);
assert( v != NULL);
assert( u->getLength() > 0);
assert( v->getLength() > 0);
assert(m->getRows() == u->getLength());
assert(m->getCols() == v->getLength());
for(unsigned r = 0; r < m->getRows(); ++r) {
PBQPNum min = m->getRowMin(r);
(*u)[r] += min;
if (!isInf(min)) {
m->subFromRow(r, min);
} else {
m->setRow(r, 0);
}
}
for(unsigned c = 0; c < m->getCols(); ++c) {
PBQPNum min = m->getColMin(c);
(*v)[c] += min;
if (!isInf(min)) {
m->subFromCol(c, min);
} else {
m->setCol(c, 0);
}
}
return m->isZero();
}
static
void simplify_edge(pbqp *this_,int u,int v)
{
PBQPMatrix *costs;
bool is_zero;
assert (this_ != NULL);
assert (u >= 0 && u <this_->num_nodes);
assert (v >= 0 && v <this_->num_nodes);
assert (u != v);
if (u > v) {
int swap = u;
u = v;
v = swap;
}
costs = get_costmatrix_ptr(this_,u,v);
is_zero=normalize_matrix(costs,this_->node_costs[u],this_->node_costs[v]);
if(is_zero){
delete_edge(this_,u,v);
this_->changed = true;
}
}
static
void eliminate_independent_edges(pbqp *this_)
{
int u,v;
adjnode *adj_ptr,*next;
assert(this_ != NULL);
assert(this_ -> adj_list != NULL);
this_->changed = false;
for(u=0;u < this_->num_nodes;u++) {
for (adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = next) {
next = adj_ptr -> succ;
v = adj_ptr -> adj;
assert(v >= 0 && v < this_->num_nodes);
if (u < v) {
simplify_edge(this_,u,v);
}
}
}
}
static
void apply_RI(pbqp *this_,int x)
{
int y;
unsigned xlen,
ylen;
PBQPMatrix *c_yx;
PBQPVector *c_x, *delta;
assert(this_ != NULL);
assert(x >= 0 && x < this_->num_nodes);
assert(this_ -> adj_list[x] != NULL);
assert(this_ -> adj_list[x] -> succ == NULL);
y = this_ -> adj_list[x] -> adj;
assert(y >= 0 && y < this_->num_nodes);
xlen = this_ -> node_costs[x]->getLength();
ylen = this_ -> node_costs[y]->getLength();
c_x = this_ -> node_costs[x];
c_yx = pbqp_get_costmatrix(this_,y,x);
assert (c_yx != NULL);
delta = new PBQPVector(ylen);
for(unsigned i = 0; i < ylen; ++i) {
PBQPNum min = (*c_yx)[i][0] + (*c_x)[0];
for(unsigned j = 1; j < xlen; ++j) {
PBQPNum c = (*c_yx)[i][j] + (*c_x)[j];
if ( c < min )
min = c;
}
(*delta)[i] = min;
}
*this_ -> node_costs[y] += *delta;
remove_node(this_,x);
reorder_adjnodes(this_,x);
assert(this_ -> stack_ptr < this_ -> num_nodes);
this_->stack[this_ -> stack_ptr++] = x;
delete c_yx;
delete delta;
this_->num_ri++;
}
static
void apply_RII(pbqp *this_,int x)
{
int y,z;
unsigned xlen,ylen,zlen;
adjnode *adj_yz;
PBQPMatrix *c_yx, *c_zx;
PBQPVector *cx;
PBQPMatrix *delta;
assert(this_ != NULL);
assert(x >= 0 && x < this_->num_nodes);
assert(this_ -> adj_list[x] != NULL);
assert(this_ -> adj_list[x] -> succ != NULL);
assert(this_ -> adj_list[x] -> succ -> succ == NULL);
y = this_ -> adj_list[x] -> adj;
z = this_ -> adj_list[x] -> succ -> adj;
assert(y >= 0 && y < this_->num_nodes);
assert(z >= 0 && z < this_->num_nodes);
xlen = this_ -> node_costs[x]->getLength();
ylen = this_ -> node_costs[y]->getLength();
zlen = this_ -> node_costs[z]->getLength();
cx = this_ -> node_costs[x];
c_yx = pbqp_get_costmatrix(this_,y,x);
c_zx = pbqp_get_costmatrix(this_,z,x);
assert(c_yx != NULL);
assert(c_zx != NULL);
if ( (adj_yz = find_adjnode(this_,y,z)) != NULL) {
adj_yz->tc_valid = false;
adj_yz->reverse->tc_valid = false;
}
delta = new PBQPMatrix(ylen, zlen);
for(unsigned i=0;i<ylen;i++) {
for(unsigned j=0;j<zlen;j++) {
PBQPNum min = (*c_yx)[i][0] + (*c_zx)[j][0] + (*cx)[0];
for(unsigned k=1;k<xlen;k++) {
PBQPNum c = (*c_yx)[i][k] + (*c_zx)[j][k] + (*cx)[k];
if ( c < min ) {
min = c;
}
}
(*delta)[i][j] = min;
}
}
add_pbqp_edgecosts(this_,y,z,delta);
remove_node(this_,x);
simplify_edge(this_,y,z);
reorder_adjnodes(this_,x);
assert(this_ -> stack_ptr < this_ -> num_nodes);
this_->stack[this_ -> stack_ptr++] = x;
delete c_yx;
delete c_zx;
delete delta;
this_->num_rii++;
}
static
void apply_RN(pbqp *this_,int x)
{
unsigned xlen;
assert(this_ != NULL);
assert(x >= 0 && x < this_->num_nodes);
assert(this_ -> node_costs[x] != NULL);
xlen = this_ -> node_costs[x] -> getLength();
this_ -> optimal = false;
assert(this_ -> stack_ptr < this_ -> num_nodes);
this_->stack[this_ -> stack_ptr++] = x;
remove_node(this_,x);
reorder_adjnodes(this_,x);
this_->num_rn++;
}
static
void compute_tc_info(pbqp *this_, adjnode *p)
{
adjnode *r;
PBQPMatrix *m;
int x,y;
PBQPVector *c_x, *c_y;
int *row_inf_counts;
assert(p->reverse != NULL);
r = p->reverse;
p->tc_valid = true;
r->tc_valid = true;
x = r->adj;
y = p->adj;
c_x = this_ -> node_costs[x];
c_y = this_ -> node_costs[y];
m = pbqp_get_costmatrix(this_, x, y);
if (p->tc_safe_regs == NULL) {
p->tc_safe_regs = (int *) malloc(sizeof(int) * c_x->getLength());
}
if (r->tc_safe_regs == NULL ) {
r->tc_safe_regs = (int *) malloc(sizeof(int) * c_y->getLength());
}
p->tc_impact = r->tc_impact = 0;
row_inf_counts = (int *) alloca(sizeof(int) * c_x->getLength());
p->tc_safe_regs[0] = 0;
row_inf_counts[0] = 0;
for(unsigned i = 1; i < c_x->getLength(); ++i){
p->tc_safe_regs[i] = 1;
row_inf_counts[i] = 0;
}
r->tc_safe_regs[0] = 0;
for(unsigned j = 1; j < c_y->getLength(); ++j){
r->tc_safe_regs[j] = 1;
}
for(unsigned j = 0; j < c_y->getLength(); ++j) {
int col_inf_counts = 0;
for (unsigned i = 0; i < c_x->getLength(); ++i) {
if (isInf((*m)[i][j])) {
++col_inf_counts;
++row_inf_counts[i];
p->tc_safe_regs[i] = 0;
r->tc_safe_regs[j] = 0;
}
}
if (col_inf_counts > p->tc_impact) {
p->tc_impact = col_inf_counts;
}
}
for(unsigned i = 0; i < c_x->getLength(); ++i){
if (row_inf_counts[i] > r->tc_impact)
{
r->tc_impact = row_inf_counts[i];
}
}
delete m;
}
static
int is_colorable(pbqp *this_,int x)
{
adjnode *adj_ptr;
PBQPVector *c_x;
int result = 1;
int *allowed;
int num_allowed = 0;
unsigned total_impact = 0;
assert(this_ != NULL);
assert(x >= 0 && x < this_->num_nodes);
assert(this_ -> node_costs[x] != NULL);
c_x = this_ -> node_costs[x];
allowed = (int *)malloc(sizeof(int) * c_x->getLength());
for(unsigned i = 0; i < c_x->getLength(); ++i){
if (!isInf((*c_x)[i]) && i > 0) {
allowed[i] = 1;
++num_allowed;
} else {
allowed[i] = 0;
}
}
for(adj_ptr=this_->adj_list[x] ;adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
if (!adj_ptr -> tc_valid) {
compute_tc_info(this_, adj_ptr);
}
total_impact += adj_ptr->tc_impact;
if (num_allowed > 0) {
for (unsigned i = 1; i < c_x->getLength(); ++i){
if (allowed[i]){
if (!adj_ptr->tc_safe_regs[i]){
allowed[i] = 0;
--num_allowed;
if (num_allowed == 0)
break;
}
}
}
}
if ( total_impact >= c_x->getLength() - 1 && num_allowed == 0 ) {
result = 0;
break;
}
}
free(allowed);
return result;
}
int pop_colorablenode(pbqp *this_)
{
int deg;
bucketnode *min_bucket=NULL;
PBQPNum min = std::numeric_limits<PBQPNum>::infinity();
for(deg=this_->max_deg;deg > 2;deg--) {
bucketnode *bucket;
for(bucket=this_->bucket_list[deg];bucket!= NULL;bucket = bucket -> succ) {
int u = bucket->u;
if (is_colorable(this_,u)) {
pbqp_remove_bucket(this_,bucket);
this_->num_rn_special++;
free(bucket);
return u;
}
}
}
for(deg=this_->max_deg;deg >2; deg--) {
bucketnode *bucket;
for(bucket=this_->bucket_list[deg];bucket!= NULL;bucket = bucket -> succ) {
PBQPNum h;
int u;
u = bucket->u;
assert(u>=0 && u < this_->num_nodes);
h = (*this_->node_costs[u])[0] / (PBQPNum) deg;
if (h < min) {
min_bucket = bucket;
min = h;
}
}
}
if (min_bucket != NULL) {
int u;
pbqp_remove_bucket(this_,min_bucket);
u = min_bucket->u;
free(min_bucket);
return u;
} else {
return -1;
}
}
static
void reduce_pbqp(pbqp *this_)
{
int u;
assert(this_ != NULL);
assert(this_->bucket_list != NULL);
for(;;){
if (this_->bucket_list[1] != NULL) {
u = pop_node(this_,1);
apply_RI(this_,u);
} else if (this_->bucket_list[2] != NULL) {
u = pop_node(this_,2);
apply_RII(this_,u);
} else if ((u = pop_colorablenode(this_)) != -1) {
apply_RN(this_,u);
} else {
break;
}
}
}
static
void determine_solution(pbqp *this_,int x)
{
PBQPVector *v = new PBQPVector(*this_ -> node_costs[x]);
adjnode *adj_ptr;
assert(this_ != NULL);
assert(x >= 0 && x < this_->num_nodes);
assert(this_ -> adj_list != NULL);
assert(this_ -> solution != NULL);
for(adj_ptr=this_->adj_list[x] ;adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
int y = adj_ptr -> adj;
int y_sol = this_ -> solution[y];
PBQPMatrix *c_yx = pbqp_get_costmatrix(this_,y,x);
assert(y_sol >= 0 && y_sol < (int)this_->node_costs[y]->getLength());
(*v) += c_yx->getRowAsVector(y_sol);
delete c_yx;
}
this_ -> solution[x] = v->minIndex();
delete v;
}
static
void back_propagate(pbqp *this_)
{
int i;
assert(this_ != NULL);
assert(this_->stack != NULL);
assert(this_->stack_ptr < this_->num_nodes);
for(i=this_ -> stack_ptr-1;i>=0;i--) {
int x = this_ -> stack[i];
assert( x >= 0 && x < this_ -> num_nodes);
reinsert_node(this_,x);
determine_solution(this_,x);
}
}
static
void determine_trivialsolution(pbqp *this_)
{
int u;
PBQPNum delta;
assert( this_ != NULL);
assert( this_ -> bucket_list != NULL);
while (this_->bucket_list[0] != NULL) {
u = pop_node(this_,0);
assert( u >= 0 && u < this_ -> num_nodes);
this_->solution[u] = this_->node_costs[u]->minIndex();
delta = (*this_->node_costs[u])[this_->solution[u]];
this_->min = this_->min + delta;
this_->num_r0++;
}
}
static
void check_pbqp(pbqp *this_)
{
int u,v;
PBQPMatrix *costs;
adjnode *adj_ptr;
assert( this_ != NULL);
for(u=0;u< this_->num_nodes; u++) {
assert (this_ -> node_costs[u] != NULL);
for(adj_ptr = this_ -> adj_list[u];adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
v = adj_ptr -> adj;
assert( v>= 0 && v < this_->num_nodes);
if (u < v ) {
costs = adj_ptr -> costs;
assert( costs->getRows() == this_->node_costs[u]->getLength() &&
costs->getCols() == this_->node_costs[v]->getLength());
}
}
}
}
void solve_pbqp(pbqp *this_)
{
assert(this_ != NULL);
assert(!this_->solved);
check_pbqp(this_);
eliminate_trivial_nodes(this_);
eliminate_independent_edges(this_);
create_bucketlist(this_);
reduce_pbqp(this_);
determine_trivialsolution(this_);
back_propagate(this_);
this_->solved = true;
}
int get_pbqp_solution(pbqp *this_,int x)
{
assert(this_ != NULL);
assert(this_->solution != NULL);
assert(this_ -> solved);
return this_->solution[x];
}
bool is_pbqp_optimal(pbqp *this_)
{
assert(this_ -> solved);
return this_->optimal;
}
}