maxitg / SetReplace

C++/Wolfram Language package for exploring set and graph rewriting systems
MIT License
219 stars 46 forks source link

Move TimeConstrained from setSubstitutionSystem$cpp to libSetReplace #647

Closed daneelsan closed 3 years ago

daneelsan commented 3 years ago

Changes

Comments

In[] := WolframModel[
             {{1, 2}} -> {{1, 3}, {1, 3}, {3, 2}}, {{1, 1}},
             Infinity,
             TimeConstraint -> 1] // AbsoluteTiming // First
Out[] = 2.61478

Examples

In[] := WolframModel[{{1, 2}} -> {{1, 3}, {1, 3}, {3, 2}}, {{1, 1}}, Infinity, TimeConstraint -> 1]

image

In[] := %["TerminationReason"]
Out[] = "TimeConstraint"

This change is Reviewable

daneelsan commented 3 years ago

@maxitg Right now I covert "TimeConstraint" -> Infinity to maxdouble before passing in to cpp$setReplace. Inside replace I'm doing this:

    time_t startTime = time(0);
    while (true) {
      if (replaceOnce(shouldAbort)) {
        ++count;
      } else {
        return count;
      }

      // Custom TimeConstraint function
      double secondsElapsed = difftime(time(0), startTime);
      if (secondsElapsed > timeConstraint) {
        terminationReason_ = TerminationReason::TimeConstrained;
        return count;
      }
    }

So even if I pass Infinity, it still computes time(0) and difftime. Do you think it would be better to do something like:

    time_t startTime = time(0);
    auto timeConstrainted = []() { return false; };
    if (timeConstraint > 0) {
       timeConstrained = []() { return difftime(time(0), startTime) > timeConstraint; }
    }
   ...
      if (timeConstrained()) {
        terminationReason_ = TerminationReason::TimeConstrained;
        return count;
      }

(don't know how lambdas work but that's the idea)