All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chen0040.gp.lgp.program.operators.Divide Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.github.chen0040.gp.lgp.program.operators;

import com.github.chen0040.gp.commons.Observation;
import com.github.chen0040.gp.lgp.enums.OperatorExecutionStatus;
import com.github.chen0040.gp.lgp.program.Register;
import com.github.chen0040.gp.lgp.program.Operator;
import com.github.chen0040.gp.lgp.LGP;


/**
 * Created by xschen on 30/4/2017.
 */
public class Divide extends Operator {

   private static final long serialVersionUID = 5146577862092367213L;
   private double undefined;

   public Divide(double undefined){
      super("/");
      this.undefined = undefined;
   }

   public Divide(){
      super("/");
      this.undefined = LGP.DEFAULT_UNDEFINED_LOW;
   }

   @Override public Operator makeCopy() {
      Divide clone = new Divide(undefined);
      clone.copy(this);
      return clone;
   }


   @Override public OperatorExecutionStatus execute(Register operand1, Register operand2, Register destination_register, Observation observation) {
      double x1 = operand1.getValue();
      double x2 = operand2.getValue();
      if (x2 == 0)
      {
         destination_register.setValue(x1 + undefined);
      }
      else
      {
         destination_register.setValue(x1 / x2);
      }

      return OperatorExecutionStatus.LGP_EXECUTE_NEXT_INSTRUCTION;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy