From 3403eac15e84b616803ca4ef1880c05e3e7fa491 Mon Sep 17 00:00:00 2001 From: Arnaud Malapert Date: Thu, 14 Jun 2012 16:23:46 +0200 Subject: [PATCH] can compile a generic class pslp_criteria (#16) --- src/abstract_criteria.h | 106 ++++++++++++++++++++++++++++------------ src/criteria.h | 7 --- src/cudf.c | 6 +-- src/pserv_criteria.h | 9 +--- 4 files changed, 81 insertions(+), 47 deletions(-) diff --git a/src/abstract_criteria.h b/src/abstract_criteria.h index 161801f..7519007 100644 --- a/src/abstract_criteria.h +++ b/src/abstract_criteria.h @@ -14,38 +14,39 @@ // Abstract criteria class class abstract_criteria { - public: - // Method called to allocate some variables (columns) to the criteria - virtual int set_variable_range(int first_free_var) { return 0; } - // Method called to add the criteria to the current objective - virtual int add_criteria_to_objective(CUDFcoefficient lambda) { return 0; }; - // Method called to add the criteria to the constraints - virtual int add_criteria_to_constraint(CUDFcoefficient lambda) { return 0; }; - // Method called to add criteria related constraints - virtual int add_constraints() { return 0; }; - - // Gives the range of the criteria objective - virtual CUDFcoefficient bound_range() { return 0; }; - // Gives the upper bound of the criteria objective - virtual CUDFcoefficient upper_bound() { return 0; }; - // Gives the lower bound of the criteria objective - virtual CUDFcoefficient lower_bound() { return 0; }; - - // Does this criteria allows problem reduction ? - virtual bool can_reduce(CUDFcoefficient lambda) { return true; } - - // Method called to let the criteria initializes itself - virtual void initialize(PSLProblem *problem, abstract_solver *solver) { }; - // Method called to initialize criteria variables - virtual void initialize_intvars() { }; - - //Method called to let the criteria checks some properties availability - virtual void check_property(PSLProblem *problem) {}; - - // Criteria destructor - virtual ~abstract_criteria() { }; +public: + // Method called to allocate some variables (columns) to the criteria + virtual int set_variable_range(int first_free_var) { return 0; } + // Method called to add the criteria to the current objective + virtual int add_criteria_to_objective(CUDFcoefficient lambda) { return 0; }; + // Method called to add the criteria to the constraints + virtual int add_criteria_to_constraint(CUDFcoefficient lambda) { return 0; }; + // Method called to add criteria related constraints + virtual int add_constraints() { return 0; }; + + // Gives the range of the criteria objective + virtual CUDFcoefficient bound_range() { return 0; }; + // Gives the upper bound of the criteria objective + virtual CUDFcoefficient upper_bound() { return 0; }; + // Gives the lower bound of the criteria objective + virtual CUDFcoefficient lower_bound() { return 0; }; + + // Does this criteria allows problem reduction ? + virtual bool can_reduce(CUDFcoefficient lambda) { return true; } + + // Method called to let the criteria initializes itself + virtual void initialize(PSLProblem *problem, abstract_solver *solver) { }; + // Method called to initialize criteria variables + virtual void initialize_intvars() { }; + + //Method called to let the criteria checks some properties availability + virtual void check_property(PSLProblem *problem) {}; + + // Criteria destructor + virtual ~abstract_criteria() { }; }; + // Type for a list of criteria typedef vector CriteriaList; typedef CriteriaList::iterator CriteriaListIterator; @@ -53,5 +54,50 @@ typedef CriteriaList::iterator CriteriaListIterator; // Shall we optimize variable usage or not extern bool criteria_opt_var; +inline bool isInRange(unsigned int val, pair &range) { + return val >= range.first && val <= range.second; +} + +// A generic class for defining PSLP criteria. +class pslp_criteria : public abstract_criteria { +public: + PSLProblem *problem; // a pointer to the problem + abstract_solver *solver; // a pointer to the solver + + // select elements according to their reliability (nodes, links, paths ...) + int reliable; + // lambda multiplier for the criteria + CUDFcoefficient lambda_crit ; + + // upper bound of the criteria + int _upper_bound; + + // Compute the criteria range, upper and lower bounds + virtual CUDFcoefficient bound_range() { + return CUDFabs(lambda_crit) * _upper_bound; + } + virtual CUDFcoefficient upper_bound() { + return _upper_bound; + } + virtual CUDFcoefficient lower_bound() { + return 0; + } + + pslp_criteria(CUDFcoefficient lambda_crit, int reliable) : lambda_crit(lambda_crit), reliable(reliable) {}; + + // Criteria destructor + virtual ~pslp_criteria() {}; + +protected : + + virtual void set_constraint_coeff(int rank, CUDFcoefficient value) { + solver->set_constraint_coeff(rank, lambda_crit * value); + } + + virtual void set_obj_coeff(int rank, CUDFcoefficient value) { + solver->set_obj_coeff(rank, lambda_crit * value + solver->get_obj_coeff(rank)); + } +}; + #endif diff --git a/src/criteria.h b/src/criteria.h index 7814aed..b4bf6bf 100644 --- a/src/criteria.h +++ b/src/criteria.h @@ -14,14 +14,7 @@ #include #include -//#include -//#include -//#include #include -//#include - -//#include -//#include #include #include diff --git a/src/cudf.c b/src/cudf.c index 028ca30..9033f59 100644 --- a/src/cudf.c +++ b/src/cudf.c @@ -501,15 +501,15 @@ CriteriaList *process_criteria(char *crit_descr, unsigned int &pos, bool first_l // handle criteria if (strncmp(crit_descr+crit_name, "pserv", crit_name_length) == 0) { pair r1(0, numeric_limits::max()), r2(0,numeric_limits::max() ); - int reliable = -1; + int rel = -1; CUDFcoefficient lambda = 1; get_criteria_properties(crit_descr, pos, C_TEXT("type"), r1, C_TEXT("layer"), r2, - reliable, lambda + rel, lambda ); if(crit_descr[sign] == '+') {lambda *=-1;} - criteria->push_back(new pserv_criteria(r1, r2, reliable, lambda)); + criteria->push_back(new pserv_criteria(lambda, rel, r1, r2)); } else if (strncmp(crit_descr+crit_name, "conn", crit_name_length) == 0) { //TODO conn_criteria } else if (strncmp(crit_descr+crit_name, "bandw", crit_name_length) == 0) { diff --git a/src/pserv_criteria.h b/src/pserv_criteria.h index 6507249..e56f31c 100644 --- a/src/pserv_criteria.h +++ b/src/pserv_criteria.h @@ -16,7 +16,7 @@ // not installed in the initial configuration and // that are installed in the final one. // The criteria can be restricted to a type of pservers. -class pserv_criteria: public abstract_criteria { +class pserv_criteria: public pslp_criteria{ public: PSLProblem *problem; // a pointer to the problem abstract_solver *solver; // a pointer to the solver @@ -26,7 +26,6 @@ class pserv_criteria: public abstract_criteria { // 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 @@ -52,17 +51,13 @@ class pserv_criteria: public abstract_criteria { // lambda multiplier for the criteria CUDFcoefficient lambda_crit ; - pserv_criteria(pair pserv_range, pair layer_range, int reliable, CUDFcoefficient lambda_crit) : pserv_range(pserv_range), layer_range(layer_range), reliable(reliable), lambda_crit(lambda_crit) {}; + pserv_criteria(CUDFcoefficient lambda_crit, int reliable, pair pserv_range, pair layer_range) : pslp_criteria(lambda_crit, reliable), pserv_range(pserv_range), layer_range(layer_range) {}; private : inline bool all_pserv() { return pserv_range.first <= 0 && problem->serverTypeCount() -1 <= pserv_range.second; } - bool isInRange(unsigned int val, pair &range) { - return val >= range.first && val <= range.second; - } - inline bool isInRel(FacilityNode* node) { if(reliable < 0) return true; else {