Permalink
Browse files

* cleanup finished

* +set_pop_size, +validate_conf (+tests)
  • Loading branch information...
mj41 committed May 23, 2007
1 parent d9cedb7 commit 1c05d58df0b107334150f00c8ea7dea0cbf4986b
Showing with 106 additions and 101 deletions.
  1. +15 −80 src/lgp.pmc
  2. +1 −1 src/lgp_conf.h
  3. +71 −1 t/lgp.t
  4. +19 −19 t/pi.pir
View
@@ -159,19 +159,22 @@ pmclass LGP need_ext dynpmc {
}
- METHOD void prepare_lgp (INTVAL t_pop_size) {
- // todo - move a part of code from invoke method here
-
-
- if ( init_done == 2 ) {
- PIO_eprintf(INTERP, "You need call prepare_lgp() only once.");
+ METHOD void set_pop_size (INTVAL t_pop_size) {
+ if ( t_pop_size > MAX_POP_SIZE ) {
+ PIO_eprintf(INTERP, "pop_size <= MAX_POP_SIZE (%i <= %i) failed\n", t_pop_size, MAX_POP_SIZE);
Parrot_exit(INTERP, 1);
}
- if ( init_done != 1 ) {
- PIO_eprintf(INTERP, "You need prepare_sub() first.");
+ if ( t_pop_size < FIGHT_NUM*2 ) {
+ PIO_eprintf(INTERP, "pop_size >= FIGHT_NUM*2 (%i >= %i) failed\n", t_pop_size, FIGHT_NUM*2);
Parrot_exit(INTERP, 1);
}
+ pop_size = t_pop_size;
+ }
+
+
+ METHOD void validate_conf () {
+ // todo - move a part of code from invoke method here
// length of eval space individual core have to be more longer or equal as INDI_MAX_LEN
if ( ipc_end - ipc_start != INDI_MAX_LEN ) {
@@ -183,85 +186,17 @@ pmclass LGP need_ext dynpmc {
}
}
-
- if ( check_op_count ) {
- int j, found = 0;
- for ( j = 0; j < lgp_op_count; j++ ) {
- if ( lgp_ops[j] == 16 ) {
- if ( j != lgp_op_count - 1 ) {
- PIO_eprintf(INTERP, "found 'ret' opcode, but as %i. opcode. seems like lgp_op_count=%i is wrong\n", j+1, lgp_op_count);
- Parrot_exit(INTERP, 1);
- }
- found = 1;
- break;
- }
- }
- if ( found != 1 ) {
- PIO_eprintf(INTERP, "'ret' opcode not found (lgp_op_count=%i)\n", lgp_op_count);
- Parrot_exit(INTERP, 1);
- }
- }
-
- if ( t_pop_size > MAX_POP_SIZE ) {
- PIO_eprintf(INTERP, "pop_size <= MAX_POP_SIZE (%i <= %i) failed\n", t_pop_size, MAX_POP_SIZE);
+ if ( pop_size == NULL ) {
+ PIO_eprintf(INTERP, "pop_size is not set.\n");
Parrot_exit(INTERP, 1);
}
+ }
- if ( t_pop_size < FIGHT_NUM*2 ) {
- PIO_eprintf(INTERP, "pop_size >= FIGHT_NUM*2 (%i >= %i) failed\n", t_pop_size, FIGHT_NUM*2);
- Parrot_exit(INTERP, 1);
- }
- pop_size = t_pop_size;
+ METHOD void initialize (INTVAL inum) {
// randomize
srand((unsigned) time(NULL) );
- // replace "returncc and bsr INDI_CORE" with "noop", "noop", "noop"
- *(epc_start + 0) = 1;
- *(epc_start + 1) = 1;
- *(epc_start + 2) = 1;
-
- init_done = 2;
- }
-
-
- METHOD void initialize_todebug (INTVAL inum) {
- opcode_t *ipc;
-
- if ( INDI_MAX_LEN < 27 ) {
- PIO_printf(INTERP, "ERR: INDI_MAX_LEN < 26");
- exit;
- }
-
- pop[inum].fitness = 0;
- pop[inum].len = INDI_MAX_LEN;
-
- INTVAL j;
- ipc = pop[inum].code;
- for ( j=0; j<INDI_MAX_LEN; j++ ) {
- *(ipc + j) = 1; // noop
- }
-
- // print_s S0
- *(ipc + 5) = 412;
- *(ipc + 6) = 0;
-
- // set_i_ic I4, 666
- *(ipc + 12) = 825;
- *(ipc + 13) = 4;
- *(ipc + 14) = 666;
-
- // print_s S0
- *(ipc + 20) = 412;
- *(ipc + 21) = 0;
-
- // ret
- *(ipc + 27) = 16;
- *(ipc + INDI_MAX_LEN - 1) = 16;
- }
-
-
- METHOD void initialize (INTVAL inum) {
opcode_t *ipc_s, *ipc;
ipc_s = pop[inum].code;
View
@@ -11,7 +11,7 @@ typedef struct lgp_indi {
size_t len;
} t_lgp_indi;
-INTVAL pop_size = MAX_POP_SIZE;
+INTVAL pop_size = NULL;
typedef t_lgp_indi t_pop[MAX_POP_SIZE + FIGHT_NUM];
t_pop pop;
View
72 t/lgp.t
@@ -5,7 +5,7 @@ use warnings;
use lib qw( . lib ../lib ../../lib );
use lib qw( ./parrot/lib );
use Test::More;
-use Parrot::Test tests => 3;
+use Parrot::Test tests => 4;
use Parrot::Config;
@@ -140,3 +140,73 @@ CODE
namespace: 'parrot;LGP'
eval_space sub prepared
OUTPUT
+
+pir_output_is(<< 'CODE', << 'OUTPUT', "all for validate_conf");
+.loadlib "lgp"
+
+.sub main :main
+
+ $I0 = find_type "LGP"
+ if $I0 == 0 goto FIND_TYPE_ERR
+
+ .local pmc engine
+ new engine, $I0
+
+ .local string pir_code_lgp
+
+ pir_code_lgp = <<'EOC_LGP'
+.namespace [ "LGP" ]
+.pcc_sub eval_space:
+ noop
+ returncc
+
+EOC_LGP
+
+ # adding indi core
+ pir_code_lgp = concat "INDI_CORE:\n"
+ .local int add_core_len
+ add_core_len = engine.'indi_max_len'()
+ add_core_len -= 2 # 'bsr' -2, 'ret' -1, + 1 for loop
+ADD_NOOP:
+ pir_code_lgp = concat " noop\n"
+ add_core_len -= 1
+ if add_core_len >= 0 goto ADD_NOOP
+ # mandatory instructions for prepare_eval_space()
+ pir_code_lgp = concat " bsr INDI_CORE\n"
+ pir_code_lgp = concat " ret\n"
+
+ .local pmc pasm_compiler
+ pasm_compiler = compreg "PASM"
+
+ .local pmc eval_code
+ push_eh COMPILE_ERR
+ eval_code = pasm_compiler(pir_code_lgp)
+ clear_eh
+
+ .local pmc eval_space
+ eval_space = get_global ['LGP'], 'eval_space'
+
+ engine.prepare_eval_space( eval_space )
+ print "eval_space sub prepared\n"
+
+ engine.set_pop_size(10)
+ print "population set\n"
+
+ engine.validate_conf()
+
+ print "validation finished sucessful\n"
+
+ end
+FIND_TYPE_ERR:
+ print "find_type for LGP failed\n"
+ end
+
+COMPILE_ERR:
+ print "compilation failed\n"
+ end
+.end
+CODE
+eval_space sub prepared
+population set
+validation finished sucessful
+OUTPUT
View
@@ -7,7 +7,7 @@
.local pmc engine
new engine, $I0
-
+
.local string pir_code_lgp
pir_code_lgp = <<'EOC_LGP'
@@ -42,22 +42,22 @@ CALC_FITNESS:
abs I31
ret
-INDI_CORE:
- set I1, 0
- noop
- noop
- noop
- noop
- noop
- noop
- noop
- # next two instructions are mandatory
- # see prepare_eval_space() in src/dynmpc/lgp.pmc
- bsr INDI_CORE
- ret
-
EOC_LGP
+ # adding indi core
+ pir_code_lgp = concat "INDI_CORE:\n"
+ .local int add_core_len
+ add_core_len = engine.'indi_max_len'()
+ add_core_len -= 2 # 'bsr' -2, 'ret' -1, + 1 for loop
+ADD_NOOP:
+ pir_code_lgp = concat " noop\n"
+ add_core_len -= 1
+ if add_core_len >= 0 goto ADD_NOOP
+ # mandatory instructions for prepare_eval_space()
+ pir_code_lgp = concat " bsr INDI_CORE\n"
+ pir_code_lgp = concat " ret\n"
+
+
.local string pir_code_pi
# PI prepared individuals
pir_code_pi = <<'EOC_PI'
@@ -112,12 +112,12 @@ EOC_PI
eval_space = get_global ['LGP'], 'eval_space'
.local int tn # test number
- tn = 1
+ tn = 0
engine.prepare_eval_space( eval_space )
- $S0 = 'default'
- $I0 = 130
- bsr COMPARE_FITNESS
+
+ engine.set_pop_size(10)
+ engine.validate_conf()
push_eh COMPILE_ERR
eval_code = pasm_compiler(pir_code_pi)

0 comments on commit 1c05d58

Please sign in to comment.