Thread overview  


March 14 Switch function from runtime to compile time  

 
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 (xa)/(ba); else return (cx)/(cb); } 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 compiletime. Look at the term (ba) and (cb) which are then known at compiletime. How can I achieve this without writing duplicate code for runtime and compiletime? 
March 14 Re: Switch function from runtime to compile time  

 
Posted in reply to alex1974  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 (xa)/(ba); > else return (cx)/(cb); > } > > 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 compiletime. Look at the term (ba) and (cb) which are then known at compiletime. > How can I achieve this without writing duplicate code for runtime and compiletime? 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/stringmixins 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 nonlearning run afterward. 
March 15 Re: Switch function from runtime to compile time  

 
Posted in reply to alex1974  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 (xa)/(ba);
> else return (cx)/(cb);
> }
>
> 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 compiletime. Look at the term (ba) and (cb) which are then known at compiletime.
> How can I achieve this without writing duplicate code for runtime and compiletime?
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.

Copyright © 19992018 by the D Language Foundation