Re: Logical Bit x "Physical" Bit
- From: "Nero1604" <normanemailbox-usenet@xxxxxxxxx>
- Date: 11 Mar 2006 10:43:48 -0800
snow wrote:
If you dont hit memory upper limit i would go for byte(char), since
byte is normally easier to manipulate than single bits in high level
languages.
You have things like bitset and vector<bool> in C++ if you want to save
some memory, but performance might decrease a little.
In Java, the performance difference between high level routines and bit
manipulation is, according to my experience, tremendous. See my
comments and the approach I adopted from the original Santa Fe
Artificial Stock Market Model. It's difficult to implement, but the
increase in performance is worth the trouble.
/**
* Here I use the fast bit packing and condition checking algorithms
* from the original Santa Fe Stock Market Model. I first tried an
implementation with BitSet,
* which was much easier to implement, but it really slowed down the
application. Having a
* fast algorithm is at the core of the simulation, since condition
checking is the
* routine which is most often called in the whole simulation run.
*
* First, construct tables for fast bit packing and condition
checking for
* classifier systems. Sets up the global tables SHIFT[], MASK[],
and
* NMASK[] to cover "nbits" condition bits.
*
* Assumes 64 bit words (long). Since the last bit is used for sign,
only bits 0 - 61
* can be used. Thus, one condition word can store nbits = 3 ternary
values (0, 1, or *)
* per word, with one of the following codings:
* Value World coding Rule coding
* 0 2 1
* 1 1 2
* * - 0
* Thus rule satisfaction can be checked with a simple AND between
* the two types of codings. If the result is >0, then the rule is
not satisfied.
*
* Since we need more than 31 world bits to code, the asset has an
array long[] aState
* in which the single words are stored. It is straightforward to
group the words, thus
* fundamental traders just have access to aState[0] which is big
enough to hold all
* fundamental bits. Technical traders have also access to aState[1]
and aState[2] which
* code also technical condotions.
*
* After calling this routine, given an array declared as
* int aState[maxStateWords];
* you can do the following:
*
* a. Store "value" (0, 1, 2, using one of the codings above) for bit
n with
* aState[WORD(n)] |= value << SHIFT[n];
* if the stored value was previously 0; or
*
* b. Store "value" (0, 1, 2, using one of the codings above) for bit
n with
* aState[WORD(n)] = (array[WORD(n)] & NMASK[n]) | (value <<
SHIFT[n]);
* if the initial state is unknown.
*
* c. Store value 0 for bit n with
* aState[WORD(n)] &= NMASK[n];
*
* d. Extract the value of bit n (0, 1, 2, or possibly 3) with
* value = (aState[WORD(n)] >> SHIFT[n]) & 3;
*
* e. Test for value 0 for bit n with
* if ((aState[WORD(n)] & MASK[n]) == 0) ...
*
* f. Check whether a condition is fulfilled (using the two codings)
with
* for (i=0; i<maxwords; i++)
* if (condition[i] & aState[i]) break;
* if (i != maxwords) ...
*
*/
protected void stateInit() {
for (int i = 0 ; i < maxStateWords ; i++ ) { // initialize array
with state of the asset
aState[i] = 0l;
}
SHIFT = new int[nbits];
MASK = new long[nbits];
NMASK = new long[nbits];
for (int bit = 0 ; bit < nbits ; bit++) {
SHIFT[bit] = (bit % 32)*2;
MASK[bit] = 3l << SHIFT[bit];
NMASK[bit] = ~MASK[bit];
}
int a =1;
} // stateInit
Declarations:
protected final int maxStateWords = 2 ;
protected long[] aState = new long[maxStateWords];
protected static int nbits = 32;
protected static int[] SHIFT;
protected static long[] MASK;
protected static long[] NMASK;
Norman
.
- References:
- Logical Bit x "Physical" Bit
- From: victorufba@xxxxxxxxx
- Re: Logical Bit x "Physical" Bit
- From: snow
- Logical Bit x "Physical" Bit
- Prev by Date: Re: Logical Bit x "Physical" Bit
- Next by Date: Data structure for matching in XCS
- Previous by thread: Re: Logical Bit x "Physical" Bit
- Next by thread: Data structure for matching in XCS
- Index(es):