Skip to content

Commit

Permalink
First version of bandw criterion (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-m committed Jun 18, 2012
1 parent 6d11011 commit 3e93884
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/abstract_criteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class param_range {
string param_name;
int _min, _max;
public:
param_range(string param, int min, int max) : param_name(param), _min(min), _max(max) {}
param_range(string param, int min, int max) : param_name(param), _min(min), _max(max) {
assert(_min <= _max);
}
param_range(string param, int min) : param_name(param), _min(min), _max(numeric_limits<int>::max()) {}
virtual ~param_range() {}

Expand All @@ -121,13 +123,21 @@ class param_range {
}

bool scanf(const char * s) {
return sscanf(s, (param_name + ":,%d-%d").c_str(), &_min, &_max) != 2;
return sscanf(s, (param_name + ":,%d-%d").c_str(), &_min, &_max) == 2;
}

void set_min_limit(int min_limit) {
if(min_limit > _min) {
_min = min_limit;
}
assert(_min <= _max);
}

void update_max(int max_limit) {
void set_max_limit(int max_limit) {
if(max_limit < _max) {
_max = max_limit;
}
assert(_min <= _max);
}

int min() {
Expand Down
68 changes: 68 additions & 0 deletions src/bandw_criteria.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

/*******************************************************/
/* CUDF solver: bandw_criteria.c */
/* Implementation of the bandw criteria */
/* (c) Arnaud Malapert I3S (UNSA-CNRS) 2012 */
/*******************************************************/


#include <bandw_criteria.h>


// Criteria initialization
void bandw_criteria::initialize(PSLProblem *problem, abstract_solver *solver) {
pslp_criteria::initialize(problem, solver);
stage_range.set_min_limit(0);
stage_range.set_max_limit(problem->stageCount() - 1);
length_range.set_min_limit(1); //No bandwidth variables for local connections
//TODO Initialize upper bound
}

// Computing the number of columns required to handle the criteria
int bandw_criteria::set_variable_range(int first_free_var) {
return first_free_var;
}

int bandw_criteria::rank(pair<FacilityNode*,FacilityNode*> const &path, const unsigned int stage)
{
return problem->rankB(path, stage);
}

// Add the criteria to the current objective function
int bandw_criteria::add_criteria_to_objective(CUDFcoefficient lambda) {
for (PathIterator p = problem->getRoot()->pbegin(); p != problem->getRoot()->pend(); ++p) {
if(isRLSelected(*p)) {
for (int s = stage_range.min(); s <= stage_range.max(); ++s) {
set_obj_coeff(rank(*p, s), lambda);
}
}
}
return 0;
}

// Add the criteria to the constraint set
int bandw_criteria::add_criteria_to_constraint(CUDFcoefficient lambda) {

for (PathIterator p = problem->getRoot()->pbegin(); p != problem->getRoot()->pend(); ++p) {
if(isRLSelected(*p)) {
for (int s = stage_range.min(); s <= stage_range.max(); ++s) {
set_constraint_coeff(rank(*p, s), lambda);
}
}
}
return 0;
}

int bandw_criteria::add_constraints()
{
return 0;
}









55 changes: 55 additions & 0 deletions src/bandw_criteria.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/*******************************************************/
/* CUDF solver: bandw_criteria.h */
/* Concrete class for the bandw criteria */
/* (c) Arnaud Malapert I3S (UNSA-CNRS) 2012 */
/*******************************************************/


#ifndef _BANDW_CRITERIA_H_
#define _BANDW_CRITERIA_H_

#include <abstract_criteria.h>

// A concrete class for the bandw criteria
// i.e. the bandwidth allocated to connections.
class bandw_criteria: public pslp_criteria{
public:

param_range stage_range;
param_range length_range;

bandw_criteria(CUDFcoefficient lambda_crit, int reliable, param_range stage_range, param_range length_range) : pslp_criteria(lambda_crit, reliable), stage_range(stage_range), length_range(length_range) {};
virtual ~bandw_criteria() {}


// Criteria initialization
void initialize(PSLProblem *problem, abstract_solver *solver);

// Allocate some columns for the criteria
int set_variable_range(int first_free_var);
// Add the criteria to the objective
int add_criteria_to_objective(CUDFcoefficient lambda);
// Add the criteria to the constraint set
int add_criteria_to_constraint(CUDFcoefficient lambda);
// Add constraints required by the criteria
int add_constraints();

int rank(pair<FacilityNode*, FacilityNode*> const &path, const unsigned int stage);

private :

inline bool isRLSelected(pair<FacilityNode*, FacilityNode*> const &path) {
if(length_range.contains(path.second->getType()->getLevel() - path.first->getType()->getLevel())) {
return reliable == 0 ? ! isReliablePath(path.first, path.second) :
reliable > 0 ? isReliablePath(path.first, path.second) : true;
}
return false;

}

};

#endif


4 changes: 2 additions & 2 deletions src/conn_criteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class conn_criteria: public pslp_criteria{

private :

inline bool isInRel(pair<FacilityNode*, FacilityNode*> const & path) {
inline bool isReliable(pair<FacilityNode*, FacilityNode*> const & path) {
if(reliable < 0) return true;
else {
const bool relp = isReliablePath(path.first, path.second);
Expand All @@ -55,7 +55,7 @@ private :
}

inline bool isInRL(pair<FacilityNode*, FacilityNode*> const &path) {
return isInRel(path) && isInLength(path);
return isReliable(path) && isInLength(path);
}

};
Expand Down
1 change: 1 addition & 0 deletions src/criteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <pserv_criteria.h>
#include <conn_criteria.h>
#include <bandw_criteria.h>

#include <lexagregate_combiner.h>
#include <agregate_combiner.h>
Expand Down
28 changes: 26 additions & 2 deletions src/cudf.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ CUDFcoefficient get_criteria_lambda(char *crit_descr, unsigned int &pos, char si
int n = get_criteria_options(crit_descr, pos, &opts);

if (n == 1) {
return get_criteria_lambda(crit_descr, opts[0]->first, opts[0]->second, sign);
return get_criteria_lambda(crit_descr, opts[0]->first, opts[0]->second, sign);
} else if (n > 1) {
crit_descr[pos] = '\0';
fprintf(stderr, "ERROR: criteria options: a lambda value is espected here: %s\n", crit_descr);
Expand Down Expand Up @@ -311,7 +311,27 @@ void get_criteria_properties(char *crit_descr, unsigned int &pos,
}


void get_criteria_properties(char *crit_descr, unsigned int &pos,
param_range &param1, param_range &param2,
int& reliable, CUDFcoefficient& lambda, char sign) {
int n = 0;
do {
vector< pair<unsigned int, unsigned int> *> opts;
n = get_criteria_options(crit_descr, pos, &opts); //TODO Simplify method ?
if( n > 0 &&
! param1.scanf(crit_descr+opts[0]->first) &&
! param2.scanf(crit_descr+opts[0]->first) &&
sscanf(crit_descr+opts[0]->first, "reliable:,%d", &reliable) != 1 &&
sscanf(crit_descr+opts[0]->first, CUDFflags, &lambda) != 1
) {
crit_descr[pos] = '\0';
fprintf(stderr, "ERROR: criteria options: invalid format [<property>,<value>]: %s\n", crit_descr+opts[0]->first);
exit(-1);
}

} while(n > 0);
if(sign == '+') {lambda *=-1;}
}

// Process a user defined criteria
CriteriaList *process_criteria(char *crit_descr, unsigned int &pos, bool first_level, vector<abstract_criteria *> *criteria_with_property) {
Expand Down Expand Up @@ -363,7 +383,11 @@ CriteriaList *process_criteria(char *crit_descr, unsigned int &pos, bool first_l
);
criteria->push_back(new conn_criteria(lambda, rel, r1, r2));
} else if (strncmp(crit_descr+crit_name, "bandw", crit_name_length) == 0) {
//TODO bandw_criteria
param_range r1("stage",0), r2("length",1);
int rel = -1;
CUDFcoefficient lambda = 1;
get_criteria_properties(crit_descr, pos, r1, r2, rel, lambda, crit_descr[sign]);
criteria->push_back(new bandw_criteria(lambda, rel, r1, r2));
} else if (strncmp(crit_descr+crit_name, "agregate", crit_name_length) == 0) {
criteria->push_back(new agregate_combiner(process_criteria(crit_descr, pos, false, criteria_with_property),
get_criteria_lambda(crit_descr, pos, crit_descr[sign])));
Expand Down

0 comments on commit 3e93884

Please sign in to comment.