Page 1 of 4

Initializing portions of arrays

Posted: Thu Feb 12, 2015 5:24 am
by Robert Pope
This is related to C programming. I currently initialize a number of arrays using the format, e.g.:
int pcVal[]={0,103,300,9999,325,500,900};
int nPcSq[] = {
-10, -30, -10, -10, -10, -10, -30, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 6, 6, 6, 6, 0, -10,
-10, 3, 6, 12, 12, 6, 3, -10,
-10, 3, 6, 12, 12, 6, 3, -10,
-10, 0, 6, 6, 6, 6, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, -30, -10, -10, -10, -10, -30, -10 };

Now that I am trying to do some optimization, I've put all the piece square tables and bonus values into one big array, with pointers for the different components:

int evalTerm[EVALNUM];
int* nPcSq;
int* bPcSq;
int* kPcSq;

nPcSq=&evalTerm[0];
bPcSq=&evalTerm[64];
kPcSq=&evalTerm[128];

But now is there a good way to set the values of the piece square tables, etc., without resorting to nPcSq[0]=-10; nPcSq[1]=-30; etc. ?

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 5:51 am
by Angrim
memset(nPcSq, sizeof(int)*(nPcSqSIZE), 0);

where nPcSqSIZE is the number of entries in the array that you are replacing.

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 7:10 am
by xmas79
Robert Pope wrote:...I've put all the piece square tables and bonus values into one big array, with pointers for the different components...
Why did you do this? You still end up with different pointers (one for each evaluation term) with different names. But now you have to double your efforts to set values in the array, and you also have to calculate the offsets for each term yourself. In the early implementation the compiler would care about it. One question: the index 1431 of your big array is related to which term exacly??? Better leave these things to the compiler IMHO, leaving the fact that the initialization code looks cleaner, while the usage is the same. You will end up using this big table in the exactly same way, like nPcSq[sq] instead of...... nPcSq[sq]. What???

Just my 2 cents.
Nat.

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 3:44 pm
by hgm
I usually put all core data of the engine in a single array too. To make sure there aren't any L1 cache collisions. Or to allow for index ranges other than the 0...N that C offers. (E.g. to use negative indexes, or to use the 0x88 square number as index without wasting space, by interleaving tables.)

I don't use pointers to the individual tables in the storage area, though, as these would require run-time dereferencing, which is overhead. So I usually define them as preprocessor macros:

Code: Select all

unsigned char rawStorage[...];

#define board (rawStorage + 0x22) /* leave space for rim of guards */
#define PST ((signed char *)rawStorage + 0xC0)
...
You can initialize the sub-arrays by simply initializing rawStorage[].

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 3:52 pm
by Robert Pope
Perhaps this isn't a good approach, but my thinking was that for iterations of the optimization procedure, how can I easily cycle through all of my parameters if they are scattered across 50 arrays and variables?

By putting them in one array, the optimizer will have clear access to every parameter, and by creating pointers to their location in the array, I can still cleanly reference them in the eval function as kPcSq[E1] and not evalTerm[128+E1].

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 3:55 pm
by Robert Pope
Angrim wrote:memset(nPcSq, sizeof(int)*(nPcSqSIZE), 0);

where nPcSqSIZE is the number of entries in the array that you are replacing.
Perhaps initialize is the wrong term, since I am looking to set them to a list of values (known at compile time), and not zero. But I can't figure out how to assign them in the first place because the arVal[]={1,2,3,4,5}; trick doesn't work (as far as I know) when you are dealing with just a subset of the entire array.

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 4:17 pm
by hgm
Yes, it does. All remaining values will be left at 0. Of course you would have to explicitly specify the size of the array, or the compiler would not know that there are remaining elements.

E.g. part of the Shokidoki source:

Code: Select all

// Multi-purpose table of about 9KB, for holding all kernel data

unsigned char tables[(39*13+5)*18+7] = {

#define shadow (tables + 9)

 0, 0, 0, 0, 0, 0, 0, 0, 0,     18,21,22,23,24,23,22,21,18, // 0: 12x18 empty-square PST + shadow board (216 bytes)
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0,19, 0, 0, 0, 0, 0,20, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,     17,17,17,17,17,17,17,17,17,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,     33,33,33,33,33,33,33,33,33,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0,36, 0, 0, 0, 0, 0,35, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,     34,37,38,39,40,39,38,37,34,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0,17,18,19,20,21,22,23,
 0, 0, 0, 0, 0, 0, 0, 0, 0,     33,34,35,36,37,38,39, 0, 0,

#define board (tables + 14*18+1)
#define holdings (tables + 24*18+2)

// board (11x18 mailbox with 1-wide guard band)
48,48,48,48,48,48,48,48,48,48,48,          0,0,0,0,0,0,0, // 216: 14x18 game board + holdings (224 bytes)
48,48,48,48,48,48,48,48,48,48,48,          0,0,0,0,0,0,0, // 253: board corner
48,18,21,22,23,24,23,22,21,18,48,          0,0,0,0,0,0,0,
48, 0,19, 0, 0, 0, 0, 0,20, 0,48,          0,0,0,0,0,0,0,
48,17,17,17,17,17,17,17,17,17,48,          0,0,0,0,0,0,0,
48, 0, 0, 0, 0, 0, 0, 0, 0, 0,48,          0,0,0,0,0,0,0,
48, 0, 0, 0, 0, 0, 0, 0, 0, 0,48,          0,0,0,0,0,0,0,
48, 0, 0, 0, 0, 0, 0, 0, 0, 0,48,          0,0,0,0,0,0,0,
48,33,33,33,33,33,33,33,33,33,48,          0,0,0,0,0,0,0,
48, 0,36, 0, 0, 0, 0, 0,35, 0,48,          0,0,0,0,0,0,0,
48,34,37,38,39,40,39,38,37,34,48,          0,0,0,0,0,0,0,
48,48,48,48,48,48,48,48,48,48,48,          0,0,0,0,0,0,0, // 401: holding[EMPTY]
48,48,48, 0, 0, 0, 0, 0, 0, 0,48,          0,0,0,0,0,0,0,
48, 0, 0, 0, 0, 0, 0, 0,48,48,48,          0,0,0,0,0,0,0,

#define prev (tables + 468)
#define next (tables + 477)

 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, // 450: 12x18 piece-list links (not used yet)
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0,

#define captCode (tables + 684 + 152)

 B, 0, 0, 0, 0, 0, 0, 0,130, 0, 0, 0, 0, 0, 0, 0, B,      0, // 666: 17x18 capture codes (306 bytes)
 0, B, 0, 0, 0, 0, 0, 0,130, 0, 0, 0, 0, 0, 0, B, 0,      0, // bits 0-6 indicate various directions
 0, 0, B, 0, 0, 0, 0, 0,130, 0, 0, 0, 0, 0, B, 0, 0,      0, // bit 7 indicates distant
 0, 0, 0, B, 0, 0, 0, 0,130, 0, 0, 0, 0, B, 0, 0, 0,      0,
 0, 0, 0, 0, B, 0, 0, 0,130, 0, 0, 0, B, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, B, 0, 0,130, 0, 0, B, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, B,32,130,32, B, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, 0, 8, 2,  8, 0, 0, 0, 0, 0, 0, 0,      0,
 R, R, R, R, R, R, R,64, 0, 64, R, R, R, R, R, R, R,      0,
 0, 0, 0, 0, 0, 0, 0, 4, 1,  4, 0, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, F,16,129,16, F, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, F, 0, 0,129, 0, 0, F, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, F, 0, 0, 0,129, 0, 0, 0, F, 0, 0, 0, 0,      0,
 0, 0, 0, F, 0, 0, 0, 0,129, 0, 0, 0, 0, F, 0, 0, 0,      0,
 0, 0, F, 0, 0, 0, 0, 0,129, 0, 0, 0, 0, 0, F, 0, 0,      0,
 0, F, 0, 0, 0, 0, 0, 0,129, 0, 0, 0, 0, 0, 0, F, 0,      0,
 F, 0, 0, 0, 0, 0, 0, 0,129, 0, 0, 0, 0, 0, 0, 0, F,      0,

#define deltaVec ((signed char *)tables + 990 + 152)

-19,0, 0, 0, 0, 0, 0, 0,-18, 0, 0, 0, 0, 0, 0, 0,-17,     0, // 972: capture vectors (306)
 0,-19,0, 0, 0, 0, 0, 0,-18, 0, 0, 0, 0, 0, 0,-17,0,      0,
 0, 0,-19,0, 0, 0, 0, 0,-18, 0, 0, 0, 0, 0,-17,0, 0,      0,
 0, 0, 0,-19,0, 0, 0, 0,-18, 0, 0, 0, 0,-17,0, 0, 0,      0,
 0, 0, 0, 0,-19,0, 0, 0,-18, 0, 0, 0,-17,0, 0, 0, 0,      0,
 0, 0, 0, 0, 0,-19,0, 0,-18, 0, 0,-17,0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0,-19,0,-18, 0,-17,0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0,0,-19,-18,-17,0, 0, 0, 0, 0, 0, 0,      0,
-1,-1,-1,-1,-1,-1,-1,-1,  0, 1, 1, 1, 1, 1, 1, 1, 1,      0,
 0, 0, 0, 0, 0, 0, 0,17, 18,19, 0, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0,17, 0, 18, 0,19, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0,17, 0, 0, 18, 0, 0,19, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0,17, 0, 0, 0, 18, 0, 0, 0,19, 0, 0, 0, 0,      0,
 0, 0, 0,17, 0, 0, 0, 0, 18, 0, 0, 0, 0,19, 0, 0, 0,      0,
 0, 0,17, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0,19, 0, 0,      0,
 0,17, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0,19, 0,      0,
17, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0,19,      0,

#define dist ((signed char *)tables + 1296 + 152)

 8, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 8,      0, // 1278 = 71*18: distance (306)
 0, 7, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 7, 0,      0,
 0, 0, 6, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 6, 0, 0,      0,
 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0,      0,
 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,      0,
 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8,      0,
 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0,      0,
 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0,      0,
 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0,      0,
 0, 0, 6, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 6, 0, 0,      0,
 0, 7, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 7, 0,      0,
 8, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 8,      0,

#define promoFile (tables + 89*18)

0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,0x82,    0x0D,0x1D,0x2D,0x3D,0x4D,0x5D,0x6D,0x7D,0x8D, // 1584 = 88*18: to-square decode table (9x18)
0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x74,0x84,    0x0D,0x1D,0x2D,0x3D,0x4D,0x5D,0x6D,0x7D,0x8D, // 4 MSB contain file nr, 4 LSB promotion flag
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,    0x0D,0x1D,0x2D,0x3D,0x4D,0x5D,0x6D,0x7D,0x8D, // promotions are encoded as off-board squares,
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,    0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x79,0x89, // 9 higher than the real to-square
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,    0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x79,0x89,
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,    0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x79,0x89,
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,    0x0B,0x1B,0x2B,0x3B,0x4B,0x5B,0x6B,0x7B,0x8B,
0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x74,0x84,    0x0B,0x1B,0x2B,0x3B,0x4B,0x5B,0x6B,0x7B,0x8B,
0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,0x82,    0x0B,0x1B,0x2B,0x3B,0x4B,0x5B,0x6B,0x7B,0x8B,

#define steps ((signed char *)tables + 98*18)

17,19,18,1,-1,  -18,0,          // 1764 = 98*18 = 0x6D2: move-generator steps table. 0 = wG, 5 = bP
35,37,0,                        // 7 = wN
17,-17,19,-19,  18,-18,1,-1,0,  // 10 = +R, 14 = R
18,0,                           // 19 = wL
-18,0,                          // 21 = bL
19,17,-19,-17,0,                // 23 = B
0,0,0,0,0,0,0,0,0,0,0,0,0,0,    // 28 (unused slider space, has 32-bit set)
19,17,-19,-17,   1,-1,18,-18,0, // 42 = +B
-35,-37,0,                      // 51 = bN
 1,-1,-18,  18,17,19,-17,-19,0, // 54 = K,  57 = wS
-18,-17,-19,17,19,0,            // 63 = bS
-17,-19,-18,1,-1,  18,0,        // 69 = bG, 74 = wP

#define firstDir (tables + 98*18 + 76 - 16)

0, 74, 19, 23, 14,  7, 57,  0,              54,  0,  0, 42, 10,  0,  0, 0, // indexed by piece type
0,  5, 21, 23, 14, 51, 63, 69,              54, 69, 69, 42, 10, 69, 69, 0,
6,

// capture codes

#define nearCode  (tables + 104*18 - 16 + 1)
#define farCode   (tables + 106*18 - 16 + 1)
#define promoFlag (tables + 108*18 - 16 + 1)

// contact directions
0, 1, 1, BISHOP, ROOK, 16, 1|4|8, WGOLD,    KING, WGOLD, WGOLD, KING, KING, WGOLD, WGOLD, 0, // 8*13*18 = 104*18
0, 2, 2, BISHOP, ROOK, 32, 2|4|8, BGOLD,    KING, BGOLD, BGOLD, KING, KING, BGOLD, BGOLD, 0,
0, 0, 0, 0,
// distant directions
0, 0, 1, BISHOP, ROOK, 0, 0, 0,             0, 0, 0, BISHOP, ROOK, 0, 0, 0,
0, 0, 2, BISHOP, ROOK, 0, 0, 0,             0, 0, 0, BISHOP, ROOK, 0, 0, 0,
0, 0, 0, 0, // edge guard!
// promotion flags (16 = white zone, 32 = black, 2 = last rank forced promotion, 4 = forelast)
0, 16+2, 16+2, 16, 16, 16+6, 16, 0,         0, 0, 0, 0, 0, 0, 0, 0,
0, 32+2, 32+2, 32, 32, 32+6, 32, 0,         0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,

// Piece-Square tables for pieces: white .PNBRLSGK + promoted PNBRLS. , then black

#define PST (tables)
                                                                         /* wP */
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
0,0,0,0,0,0,0,0,0,          /* wL */            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,          0,0,0,0,0,0,0,0,0,          /* wB */
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
0,0,0,0,0,0,0,0,0,          /* wR */            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,            0,  0,  0,  0,  0,  0,  0,  0,  0,
(The PST are initialized at run time, because they are calculated from rank and file data.)

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 4:28 pm
by Robert Pope
Thanks, seeing the example was helpful.

What is the syntax for referencing an element in board in your example code?
board[sq]
*(board+sq)
something else?

Sorry, this is probably basic, but chess is pretty much the only programming I get to do nowadays.

Re: Initializing portions of arrays

Posted: Thu Feb 12, 2015 4:43 pm
by hgm
Just like it is an array. Like board[sq], promoFile[sq], PST[117*piece + sq], deltaVec[to-from].

The #defined macros translate board[sq] to (tables + 14*18+1)[sq], which is equivalent to tables[sq + 14*8+1]. So the executable code is not affected by this trick at all, and is identical as when I would jus have declared the tables as separate arrays. (Except that negative values now cause no problems, e.g. in deltaVec[].)

Re: Initializing portions of arrays

Posted: Fri Feb 13, 2015 7:28 pm
by Angrim
Robert Pope wrote:Perhaps this isn't a good approach, but my thinking was that for iterations of the optimization procedure, how can I easily cycle through all of my parameters if they are scattered across 50 arrays and variables?
Make a new array of pointers to eval terms, then fill it with pointers to the various arrays and variables that are used throughout your program.
Then code might look like this:

Code: Select all

int *tuningarray[5000]={NULL};
int tuningcursor=0;
int pcVal[]={0,103,300,9999,325,500,900}; 
int kingsafety=50;

for&#40;i=0;i<sizeof&#40;pcVal&#41;/sizeof&#40;int&#41;;i++)&#123;
  tuningarray&#91;tuningcursor++&#93;= &pcVal&#91;i&#93;;
&#125;
tuningarray&#91;tuningcursor++&#93;=&kingsafety;

and you could then use that big array of pointers for adjusting all of your parameters, while still having the smaller arrays and variables for regular use.
I'm pretty sure that it's faster for the compiler to access kingsafety than it is to access *tuningarray[8]