/// <summary> /// Reset the internal state of the evaluation scheme if any exists. /// Note. The XOR problem domain has no internal state. This method does nothing. /// </summary> public void Reset() { IDMotorUtils.SetCSVData(false); _Data = IDMotorUtils.GetData; _rows = new int[_Data.GetLength(0)]; for (int i = 0; i < _Data.GetLength(0); i++) { _rows[i] = _Data[i].GetLength(0); } _MaxFitness = 2 * IDMotorUtils.GetMaxFitness; StopFitness = _MaxFitness * (1 - tol); }
/// <summary> /// Evaluate the provided IBlackBox against the IDMotor problem domain and return its fitness score. /// </summary> public FitnessInfo Evaluate(IBlackBox box) { if (_evalCount == 0) { Reset(); } double ePAgg = 0, eVAgg = 0; double fitness = 0; double afitness = 0; double[] error; double output; double[] pos; double[] posD; double[] h; ISignalArray inputArr = box.InputSignalArray; ISignalArray outputArr = box.OutputSignalArray; // _evalCount++; /* #region Null test * box.ResetState(); * rt = new Random(); * output = 0; * for (int i =0; i <100;i++) * { * inputArr[0] = rt.NextDouble(); * inputArr[1] = 0; * if (i==0) * inputArr[2] = 0; * else * inputArr[2] = output; * box.Activate(); * output = outputArr[0]; * ePAgg += Math.Abs(output); * } * #endregion */ for (int k = 0; k < _Data.GetLength(0); k++) { error = new double[_rows[k]]; pos = new double[_rows[k]]; posD = new double[_rows[k]]; h = new double[_rows[k]]; initialCondition = _Data[k][0, 2]; //----- First Test box.ResetState(); // Set the input values inputArr[0] = _Data[k][0, 0]; h[0] = inputArr[0]; inputArr[1] = _Data[k][0, 1]; inputArr[2] = initialCondition; posD[0] = _Data[k][0, 2]; //box.OutputSignalArray[0] = initialCondition; // Activate the black box. box.Activate(); if (!box.IsStateValid) { // Any black box that gets itself into an invalid state is unlikely to be // any good, so lets just bail out here. return(FitnessInfo.Zero); } // Read output signal. output = outputArr[0]; pos[0] = output; // Calculate this test case's contribution to the overall fitness score. error[0] = _Data[k][0, 2] - output; if (System.Math.Abs(error[0]) > tol) { ePAgg += System.Math.Abs(error[0]); } //----- Rest of the Tests // Set the input values for (int i = 1; i < _rows[k]; i++) { inputArr[0] = _Data[k][i, 0] - _Data[k][i - 1, 0]; h[i] = inputArr[0]; inputArr[1] = _Data[k][i, 1]; inputArr[2] = output; posD[i] = _Data[k][i, 2]; // Activate the black box. box.Activate(); if (!box.IsStateValid) { // Any black box that gets itself into an invalid state is unlikely to be // any good, so lets just bail out here. return(FitnessInfo.Zero); } // Read output signal. output = outputArr[0]; pos[i] = output; // Calculate this test case's contribution to the overall fitness score. error[i] = _Data[k][i, 2] - output; if (System.Math.Abs(error[i]) > tol) { ePAgg += System.Math.Abs(error[i]); } /* * if (error[i] * error[i] > tol) * ePAgg += error[i] * error[i];*/ } double[] vel = IDMotorUtils.Derivate(pos, h); double[] velD = IDMotorUtils.Derivate(posD, h); for (int i = 0; i < _rows[k]; i++) { eVAgg += System.Math.Abs(velD[i] - vel[i]); } } afitness = _MaxFitness - eVAgg; fitness = _MaxFitness - kp * ePAgg - kv * eVAgg; /*if (fitness >= StopFitness) { * _stopConditionSatisfied = true; * } */ _stopConditionSatisfied = fitness >= StopFitness; if (fitness >= 0 && afitness >= 0) { return(new FitnessInfo(fitness, afitness)); } return(FitnessInfo.Zero); }