Re: [amibroker] Finding all possible combinations of numbers to reach a given sum

 

Ah!  I understand.

Tomasz is using a technique for encoding many individual flags into a single variable.  The entire flag is a value using a positional numbering system to encode several binary values.  Each position in the number corresponds to one of the binary "switches".  This clip from the AmiBroker Reference tells us which variable is in which position.

flags - contains the sum of following values
  • atcFlagDeleteValues = 1 - deletes all previous data from composite symbol at the beginning of scan (recommended)
  • atcFlagCompositeGroup = 2 - put composite ticker into group 253 and EXCLUDE all other tickers from group 253 (avoids adding composite to composite)
  • atcFlagTimeStamp = 4 - put last scan date/time stamp into FullName field
  • atcFlagEnableInBacktest = 8 - allow running AddToComposite in backtest/optimization mode
  • atcFlagEnableInExplore = 16 - allow running AddToComposite in exploration mode
  • atcFlagResetValues = 32 - reset values at the beginning of scan (not required if you use atcFlagDeleteValues)
  • atcFlagDefaults = 7
    (this is a composition of atcFlagResetValues | atcFlagCompositeGroup | atcFlagTimeStamp flags)

  • atcFlagEnableInPortfolio = 64 - allow running AddToComposite in custom portfolio backtester phase
  • atcFlagEnableInIndicator = 128 - allow running AddToComposite in indicator mod
----------

Whatever the number of the flag is, it is a combination of numbers that are each 2 raised to some power. 

The first flag is in the zeroth position.  2 ^ 0 == 1.  The 0th position is the flag for atcFlagDeleteValues.  If it is set to 1, delete all previous values.  If it is set to 0, do not delete.  We can tell whether it is set to 1 or to 0 for a particular use by asking whether the flag value is even or odd.  That is, we can perform modulo 2 operation on the flag and note the remainder.  If modulo(flag,2) == 0, do not delete.

The next flag is in the first position.  2 ^ 1 == 2.  The 1th position is the flag for atcFlagCompositeGroup.  If it is set to 1, put composite ticker into Group 253.  If it is set to zero, don't.  A few arithmetic operations of divide and modulo tell us whether the flag is zero or 1.

Importantly, we can set the Flag to any combination of those variables by noting their position and weighting that position accordingly.

To use an arbitrary example chosen just to be easy to understand, assume we want to "delete all previous values" and "put the last scan date and time into the full name field," but nothing else.  Those two actions are coded using the 1 and the 4

We set the flag components for positions 1 and 4 to 1, everything else to 0.  Flag = 1*1 + 1*4;  Or Flag = 5;
More completely, but less readable (don't do this) Flag = 1*1 + 0*2 + 1*4 + 0*8 + 0*16, + ...

So, to decode a flag, check to see which powers of 2 are present in the number.  Those flag conditions are set to True and all the others are set to False.  No numbers other than powers of 2 matter.  There is no need to find all possibilities that add up to a give flag value.

----------------


You can use this technique yourself if you want to conserve storage and eliminate variables at the cost of reducing the readability of the code and increasing execution time.  In the olden days when computer memory was scarce, we did that.  But seldom any more.

I hope this helps.

Best,
Howard


On Tue, Feb 16, 2016 at 9:29 PM, l3456@excite.com [amibroker] <amibroker@yahoogroups.com> wrote:
 

Hi Howard, thanks for showing interest. I was looking at addtocomposite function, i noticed that the flags have a numerical value (integers). The values are 1,2,4,8,16,32,64,128. If you use number 3 as a flag it will use flags 1 & 2. Now I can write if, then statements or case/switch for every combination. The number of combinations is factorial so as the number of flags increase then writing statements become tedious and impractical. So googled for a solution and I found the Python solution on stackoverflow.com. I thought maybe if logic can be written in afl. I spent the long weekend on it with no success.

The flag numbers are integers, they start with 1 and subsequently would be twice the previous one I don't know what the upper limit would be (some integer in progression I explained). The only restriction is that each number is used once. The sum is used to determine which flags were used.

Best Regards

__._,_.___

Posted by: Howard B <howardbandy@gmail.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (7)
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___

Related Posts


EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv