www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Switch function from runtime to compile time

reply alex1974 <alex.leisser gmail.com> writes:
I have several geometric shapes (triangle, trapezoid, gauss, ...) 
forming the membership functions of a fuzzy set.
For example the shape of the triangle is defined by the variables 
a, b and c. The function calculating membership looks like:

real triangle (real a, real b, real c, real value) {
   if (value <= a || value >= c) return 0.0;
   else if (value <= b) return (x-a)/(b-a);
   else return (c-x)/(c-b);
}

Intuitiv I packed this in a class:

class Triangle {
   real a,b,c;
   real getValue (real value) {
     ... // math as above
   }
}

My question is if this is the best practice. During the learning 
process of the fuzzy logic the shape of the triangle will change.
But once I found the optimal shape the triangle will be fixed and 
the program could be recompiled with the optimal shapes. The 
compiler could then perform optimization of the code at 
compile-time. Look at the term (b-a) and (c-b) which are then 
known at compile-time.
  How can I achieve this without writing duplicate code for 
runtime and compile-time?
Mar 14 2019
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Thursday, 14 March 2019 at 11:38:44 UTC, alex1974 wrote:
 I have several geometric shapes (triangle, trapezoid, gauss, 
 ...) forming the membership functions of a fuzzy set.
 For example the shape of the triangle is defined by the 
 variables a, b and c. The function calculating membership looks 
 like:

 real triangle (real a, real b, real c, real value) {
   if (value <= a || value >= c) return 0.0;
   else if (value <= b) return (x-a)/(b-a);
   else return (c-x)/(c-b);
 }

 Intuitiv I packed this in a class:

 class Triangle {
   real a,b,c;
   real getValue (real value) {
     ... // math as above
   }
 }

 My question is if this is the best practice. During the 
 learning process of the fuzzy logic the shape of the triangle 
 will change.
 But once I found the optimal shape the triangle will be fixed 
 and the program could be recompiled with the optimal shapes. 
 The compiler could then perform optimization of the code at 
 compile-time. Look at the term (b-a) and (c-b) which are then 
 known at compile-time.
  How can I achieve this without writing duplicate code for 
 runtime and compile-time?
Basically, the question is, how to store values, if known and optimize them, if not. Right? In this case, I think, the optimizations done by the compiler are less important, as the learning procedure lasts (by far?) more, compared to a single run without compiler optimizations. However, if you insist, you can use a mixin https://tour.dlang.org/tour/en/gems/string-mixins https://dlang.org/articles/mixin.html 0. Then, you define some default state, which corresponds to the "absence of knowledge". 1. If this state is encountered, you start some learn procedure. As mixins are interpreted at compile time, the state can be checked at compile time. 2. After needed values are learned, overwrite the file with the mixin in such a way, that the learned values are written and exit. 3. After that, you recompile. And as the values differ from the "absence of knowledge" state, you can execute some non-learning run afterward.
Mar 14 2019
prev sibling parent Michelle Long <HappyDance321 gmail.com> writes:
On Thursday, 14 March 2019 at 11:38:44 UTC, alex1974 wrote:
 I have several geometric shapes (triangle, trapezoid, gauss, 
 ...) forming the membership functions of a fuzzy set.
 For example the shape of the triangle is defined by the 
 variables a, b and c. The function calculating membership looks 
 like:

 real triangle (real a, real b, real c, real value) {
   if (value <= a || value >= c) return 0.0;
   else if (value <= b) return (x-a)/(b-a);
   else return (c-x)/(c-b);
 }

 Intuitiv I packed this in a class:

 class Triangle {
   real a,b,c;
   real getValue (real value) {
     ... // math as above
   }
 }

 My question is if this is the best practice. During the 
 learning process of the fuzzy logic the shape of the triangle 
 will change.
 But once I found the optimal shape the triangle will be fixed 
 and the program could be recompiled with the optimal shapes. 
 The compiler could then perform optimization of the code at 
 compile-time. Look at the term (b-a) and (c-b) which are then 
 known at compile-time.
  How can I achieve this without writing duplicate code for 
 runtime and compile-time?
Just store them to a file and read the file at compile time if it exists... quite simple. if (exists(file)) import(file); elseif (optimal) write(file); If you need to retrain just remove the file or use something like version.
Mar 14 2019