com.github.chen0040.gp.lgp.program.Register Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-genetic-programming Show documentation
Show all versions of java-genetic-programming Show documentation
Genetic Programming in Java, including packages on Linear Genetic Programming
package com.github.chen0040.gp.lgp.program;
import com.github.chen0040.gp.commons.Indexable;
import com.github.chen0040.gp.services.RandEngine;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* Created by xschen on 27/4/2017.
* This class represents a register in a linear program
*
*
* - a register can be read-only, in which case it is a constant
* - a register is continuous numeric variable in a linear program
*
*/
@Getter
@Setter
public class Register implements Serializable, Indexable {
private static final long serialVersionUID = 8423690373685553734L;
// denote whether the register is read-only
private boolean constant;
// value stored in the register
private double value;
// the index of the register within the linear program
private int index;
public Register makeCopy(){
Register clone = new Register();
clone.setConstant(constant);
clone.setValue(value);
clone.setIndex(index);
return clone;
}
@Override public String getName() {
return (constant ? "c" : "r") + "[" + index + "]";
}
public void mutate(RandEngine randomEngine, double sd){
value += randomEngine.normal(0, 1.0) * sd;
}
@Override
public String toString(){
return getName();
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Register register = (Register) o;
if (constant != register.constant)
return false;
if (Double.compare(register.value, value) != 0)
return false;
return index == register.index;
}
@Override public int hashCode() {
int result;
long temp;
result = (constant ? 1 : 0);
temp = Double.doubleToLongBits(value);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + index;
return result;
}
}