Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions models/utilities/constraint_check/include/constraint.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Purpose:
*****************************************************************************/
class Constraint
{
public:
public:
ConstraintEnum::ViolationCondition violation_condition{ConstraintEnum::Undefined}; /* (--)
The default violation-condition to be appllied to all constraint-tests
within this cosntraint, unless they are configured separately.*/
Expand All @@ -52,28 +52,37 @@ class Constraint
Flag indicating this constraint is intended to be evaluated.
Constraints may be disabled, which prevents them from being activated.
An active constraint gets processed.*/

// the following are all outputs of constraint.cc
unsigned int test_violated_index{0}; /* (--)
Record of which test caused the constraint to be violated. */
double test_violated_time_limit{0.0}; /* (s)
The time-limit -- where applicable -- of (one of) the test(s) that
resulted in a violation.
If multiple tests result in a violation, the value recorded here is that
of the lowest indexed test.
If the tests do not include time limits, this value will remain 0.0 */
double test_violated_threshold{0.0}; /* (--)
The threshold -- where applicable -- of (one of) the test(s) that
resulted in a violation, cast to a double for logging purposes.
If multiple tests result in a violation, the value recorded here is that
of the lowest indexed test.
If the tests do not include thresholdss, this value will remain 0.0.
Technically in the units of the threhold / test, which are unknown. */


protected:
bool active{false}; /* (--)
Flag indicating this constraint is going to be checked by the next cycle
of the associated constraint-set.*/
bool initialized{false}; /* (--)
Flag indicating this constraint-checker has passed sanity checks
and is ready to be used.*/
bool prev_violated{false}; /* (--)
Copy of previous value of violated.*/
const size_t num_tests; /* (--)
Record of the number of tests in the constraint.*/

std::vector<ConstraintTest*> test_list; /* (--)
List of pointers to constraint-tests, used for baseline functionality.*/
unsigned int test_violated_index{0}; /* (--)
Record of which test caused the constraint to be violated. */
double test_violated_time_limit{0.0}; /* (--)
The time-limit -- where applicable -- of (one of) the test(s) that
resulted in a violation.
If multiple tests result in a violation, the value recorded here is that
of the lowest indexed test.
If the tests do not include time limits, this value will remain 0.0*/

public:
Constraint( size_t num_tests_ = 1);
Expand All @@ -93,7 +102,6 @@ class Constraint
void activate();
void deactivate() {active = false;}
protected:
void count_violations();
void post_update();
};
#endif
#endif
8 changes: 6 additions & 2 deletions models/utilities/constraint_check/include/constraint_set.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ Notes:
*****************************************************************************/
class ConstraintSet : public SubscriptionBase
{
protected:
protected:
unsigned int num_violations{0}; /* (--)
Counts the number of constraints violated in each cycle.
Do not confuse with ConstraintBase::violate_count, which counts the number
of occurrences of a specific constraint violation. across all cycles.*/
std::string violated_constraints; /* (--)
Comma-separated list of the names of all constraints in the set that are
violated on this timestep.*/

public:
public:
std::list< Constraint *> constraints; /* (--)
Set of pointers to the instances of ConstraintBase being managed by this
class. Constraints should be instantiated independently and added to the
Expand All @@ -56,5 +59,6 @@ class ConstraintSet : public SubscriptionBase
void update();
void activate() override;
static bool constraint_disabled( const Constraint * c);
const std::string & get_violated_constraints() const { return violated_constraints;}
};
#endif
16 changes: 13 additions & 3 deletions models/utilities/constraint_check/include/constraint_test.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ class ConstraintTest
void count_violations();


virtual double get_time_limit() {return 0.0;}
/* Note -- get_time_limit and get_threshold are needed at the top level
because they are called for all constraint-tests that show a violation
within a constraint. They are meaningless for constraint-test types that
have no time-limit or no single threshold and simply return 0.0 for
those classes. They are meaningful for constraint-test types:
- {ConstraintTestTimed} and its derivatives, and
- {ConstraintTest_Threshold, ConstraintTest_ThresholdTimed}
respectively, and are redefined in those specfic classes.*/
virtual double get_time_limit() const { return 0.0;}
virtual double get_threshold() const { return 0.0;}

void activate() {active = (initialized && enabled);}

bool is_initialized() const {return initialized;}
Expand Down Expand Up @@ -96,7 +106,7 @@ class ConstraintTestTimed : public ConstraintTest
bool use_timer{true}; /* (--)
Allows a ConstrainTestTimed to be treated as a ConstraintTest,
circumventing the timing aspect.*/
double time_limit{0.0}; /* (--)
double time_limit{0.0}; /* (s)
The duration for which a domain violation may exist before triggering a
test violation. Not used in all cases.*/
double gap_time_limit{0.0}; /* (s)
Expand Down Expand Up @@ -128,7 +138,7 @@ class ConstraintTestTimed : public ConstraintTest
ConstraintTestTimed() = default;
~ConstraintTestTimed() override = default;

double get_time_limit() override {return time_limit;}
double get_time_limit() const override {return time_limit;}
void initialize_gap_time();
void update_timer(double delta_time);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PROGRAMMERS:
#include "constraint_enum.hh"
#include "constraint_test.hh"
#include "cml/models/utilities/cml_message/include/cml_message.hh"
#include "cml/models/utilities/math_utils/include/math_utils.hh"

/*****************************************************************************
Notes:
Expand Down Expand Up @@ -104,6 +105,16 @@ class ConstraintTest_Threshold : public ConstraintTest
}
return violation;
}

/****************************************************************************
* get_threshold
* Purpose: Returns the threshold, cast to double, for logging purposes.
****************************************************************************/
double get_threshold() const override
{
return static_cast<double>(threshold);
}

/****************************************************************************
* query_equals
* Purpose: evaluates (variable == threshold)
Expand Down Expand Up @@ -158,12 +169,12 @@ class ConstraintTest_Interval : public ConstraintTest
lower_bound = temporary;
}

if (upper_bound == lower_bound) {
if (MathUtils::is_within_abs_tolerance(upper_bound, lower_bound, equality_threshold)) {
CMLMessage::error( __FILE__,__LINE__,
"Cannot initialize an interval constraint-test with a \n"
"zero-width interval. Bounds are set to:\n",
lower_bound," and ",upper_bound,".\n"
"Initialization failed.\n");
lower_bound," and ",upper_bound," with equality threshold\n",
equality_threshold,". Initialization failed.\n");
return false;
}
initialize_interval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ class ConstraintTest_ThresholdTimed : public ConstraintTestTimed
}
return violation;
}

/****************************************************************************
* get_threshold
* Purpose: Returns the threshold, cast to double, for logging purposes.
****************************************************************************/
double get_threshold() const override
{
return static_cast<double>(threshold);
}

/****************************************************************************
* query_equals
* Purpose: evaluates (variable == threshold)
Expand Down
39 changes: 15 additions & 24 deletions models/utilities/constraint_check/src/constraint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,47 +68,38 @@ Name: post_update
void
Constraint::post_update()
{
bool new_violation_status = false; // local temporary measure of constraint status
// If configured with "Any", trip on the first test that is violated;
// record its index.
if (violate_on_any_test) {
bool violated_ = false;
// Step through by index because we want to record which test violated.
for (size_t ii = 0; ii < num_tests; ii++) {
if (test_list[ii]->get_violation()) {
test_violated_index = ii;
test_violated_time_limit = test_list[ii]->get_time_limit();
violated_ = true;
violated = true;
count_violations();
test_violated_threshold = test_list[ii]->get_threshold();
new_violation_status = true;
/* increment the count if this violation is not a holdover from the
previous cycle (violated is the copy held over from the previous
cycle). */
violation_count += static_cast<int>(!violated);
break;
}
}
violated = violated_; // in case violated_ is still false.
}

// If configured with "All", require all test violations to trip the
// constraint violation:
// constraint violation. Start with an assumption that there is a
// violation, and correct that assumption as necessary:
else {
violated = std::all_of(test_list.begin(), test_list.end(),
new_violation_status = std::all_of(test_list.begin(), test_list.end(),
[](const auto& test) {return test->get_violation();});
if (violated) {
count_violations();
}
}
prev_violated = violated;
}

/*****************************************************************************
Name: count_violations
Purpose: Counts the number of unique violations
*****************************************************************************/
void
Constraint::count_violations()
{
if (violated != prev_violated) {
violation_count += static_cast<unsigned int>(violated);
prev_violated = violated;
/* increment the count if this violation is not a holdover from the
previous cycle (violated is the copy held over from the previous
cycle). */
violation_count += static_cast<int>(new_violation_status && !violated);
}
violated = new_violation_status;
}

/*****************************************************************************
Expand Down
11 changes: 9 additions & 2 deletions models/utilities/constraint_check/src/constraint_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ ConstraintSet::update()
if (!active) {return;}

num_violations = 0;
violated_constraints.clear();
for (Constraint * constraint : constraints) {
constraint->update();
num_violations += static_cast<unsigned int>(constraint->violated);
if (constraint->violated) {
num_violations++;
if (!violated_constraints.empty()) {
violated_constraints += ", ";
}
violated_constraints += constraint->name;
}
}
}

Expand All @@ -67,4 +74,4 @@ bool
ConstraintSet::constraint_disabled( const Constraint * c)
{
return !(c->enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
)
*******************************************************************************/

#include "cml/models/utilities/math_utils/include/math_utils.hh"
#include "../include/constraint_test_templates.hh"
#include "cml/models/utilities/math_utils/include/math_utils.hh"

/*****************************************************************************
Template specializations
Expand All @@ -24,15 +24,15 @@ Template specializations
template<>
bool ConstraintTest_Threshold<double>::query_equals( double variable) const
{
return (MathUtils::is_within_range( variable,
threshold,
equality_threshold));
return (MathUtils::is_within_abs_tolerance( variable,
threshold,
equality_threshold));
}

template<>
bool ConstraintTest_Threshold<float>::query_equals( float variable) const
{
return (MathUtils::is_within_range( static_cast<double>(variable),
static_cast<double>(threshold),
equality_threshold));
return (MathUtils::is_within_abs_tolerance( static_cast<double>(variable),
static_cast<double>(threshold),
equality_threshold));
}
6 changes: 6 additions & 0 deletions models/utilities/constraint_check/verif/SIM_verif/S_define
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ class VerifUnitSimObject: public Trick::SimObject {

("initialization") constraints.set.initialize();
(0.1, "environment") constraints.set.update();
(0.1, "environment") print_violations();
}

void update_vals()
{
var_x = var_i * 1.0;
var_x_neg = -var_x;
}

void print_violations()
{
std::cout << constraints.set.get_violated_constraints() << "\n**\n";
}
private:
VerifUnitSimObject (const VerifUnitSimObject&);
VerifUnitSimObject & operator = (const VerifUnitSimObject&);
Expand Down
Loading
Loading