protected void ExecuteOnFitnessCase(LGPFitnessCase fitness_case)
        {
            mPop.InitializeProgramRegisters(this, fitness_case);

            LGPOperator.OperatorExecutionStatus command  = LGPOperator.OperatorExecutionStatus.LGP_EXECUTE_NEXT_INSTRUCTION;
            LGPInstruction current_effective_instruction = null;
            LGPInstruction prev_effective_instruction    = null;

            foreach (LGPInstruction instruction in mInstructions)
            {
                if (instruction.IsStructuralIntron)
                {
                    continue;
                }
                prev_effective_instruction    = current_effective_instruction;
                current_effective_instruction = instruction;
                if (command == LGPOperator.OperatorExecutionStatus.LGP_EXECUTE_NEXT_INSTRUCTION)
                {
                    command = current_effective_instruction.Execute();
                    fitness_case.ReportProgress(instruction.Operator, instruction.Operand1, instruction.Operand2, instruction.DestinationRegister, RegisterSet);
                }
                else
                {
                    // CSChen says:
                    // as suggested in Linear Genetic Programming
                    // the condictional construct is restricted to single condictional construct
                    // an example of single conditional construct would be
                    // line 1: if(register[a])
                    // line 2: <action1>
                    // line 3: <action2>
                    // if register[a]==true, then <action1> and <action2> are executed
                    // if register[a]==false, then <action1> is skipped and <action2> is executed
                    // <action1> and <action2> are restricted to effective instruction
                    if (prev_effective_instruction.IsConditionalConstruct)
                    {
                        command = LGPOperator.OperatorExecutionStatus.LGP_EXECUTE_NEXT_INSTRUCTION;
                    }
                }
            }

            double[] outputs = new double[mRegisterSet.RegisterCount];
            for (int i = 0; i < outputs.Length; ++i)
            {
                outputs[i] = mRegisterSet.FindRegisterByIndex(i).Value;
            }
            fitness_case.RunLGPProgramCompleted(outputs);
        }
        public virtual void EvaluateFitness()
        {
            //if (InstructionCount == 0)
            //{
            //    throw new ArgumentNullException();
            //}

            MarkStructuralIntrons();

            if (mPop.ObjectiveEvaluator == null)
            {
                LGPEnvironment        env = mPop.Environment;
                int                   fitness_case_count = env.GetFitnessCaseCount();
                List <LGPFitnessCase> cases = new List <LGPFitnessCase>();
                for (int i = 0; i < fitness_case_count; ++i)
                {
                    LGPFitnessCase fitness_case = env.CreateFitnessCase(i);
                    ExecuteOnFitnessCase(fitness_case);
                    cases.Add(fitness_case);
                }
                mObjectiveValue = EvaluateFitnessFromAllCases(cases);
            }
            else
            {
                mObjectiveValue = mPop.ObjectiveEvaluator(this);
            }

            if (mPop.IsMaximization)
            {
                mFitness = mObjectiveValue;
            }
            else
            {
                mFitness = -mObjectiveValue;
            }
            mIsFitnessValid = true;
        }
        public override void InitializeRegisters(LGPRegisterSet reg_set, LGPConstantSet constant_set, LGPFitnessCase fitness_case)
        {
            int iRegisterCount = reg_set.RegisterCount;
            int iInputCount    = fitness_case.GetInputCount();



            int iRegisterIndex = 0;

            for (int i = 0; i < mInputCopyCount; ++i)
            {
                for (int j = 0; j < iInputCount; ++j, ++iRegisterIndex)
                {
                    if (iRegisterIndex >= iRegisterCount)
                    {
                        break;
                    }

                    double value;
                    fitness_case.QueryInput(j, out value);
                    reg_set.FindRegisterByIndex(iRegisterIndex).Value = value;
                }

                if (iRegisterIndex >= iRegisterCount)
                {
                    break;
                }
            }

            while (iRegisterIndex < iRegisterCount)
            {
                reg_set.FindRegisterByIndex(iRegisterIndex).Value = mDefaultRegisterValue;
                iRegisterIndex++;
            }
        }
Example #4
0
 public virtual void InitializeRegisters(LGPRegisterSet reg_set, LGPConstantSet constant_set, LGPFitnessCase fitness_case)
 {
     if (mCurrentInstruction != null)
     {
         mCurrentInstruction.InitializeRegisters(reg_set, constant_set, fitness_case);
     }
     else
     {
         throw new ArgumentNullException();
     }
 }
 public abstract void InitializeRegisters(LGPRegisterSet reg_set, LGPConstantSet constant_set, LGPFitnessCase fitness_case);
 public virtual void InitializeProgramRegisters(LGPProgram lgp, LGPFitnessCase fitness_case)
 {
     mRegInitInstructionFactory.InitializeRegisters(lgp.RegisterSet, lgp.ConstantSet, fitness_case);
 }