/*
* File name: PolynomeGenome.java (package eas.users.lukas.primeProperties.primeEvolution)
* Author(s): lko
* Java version: 7.0
* Generation date: 24.11.2012 (12:11:06)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.lukas.primeProperties.primeEvolution;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
/**
* @author lko
*/
public class PolynomeGenome implements Serializable {
private static final long serialVersionUID = 4292538400786058385L;
private int[] coefficients;
private Random rand;
public PolynomeGenome(int length) {
this.coefficients = new int[length + 1];
this.rand = new Random();
}
@Override
public String toString() {
String s = "";
s += "$$p_{" + (this.coefficients.length - 1) + "}(x) =";
for (int i = 0; i < coefficients.length; i++) {
if (coefficients[i] != 0) {
if (coefficients[i] > 0) {
s += " + ";
} else {
s += " - ";
}
if (Math.abs(coefficients[i]) > 1) {
s += Math.abs(coefficients[i]);
}
s += "x^{" + i + "}";
}
}
s += "$$";
return s;
}
public void mutateRandomPositionPlusMinusOne() {
if (this.coefficients.length == 0) {
return;
}
if (rand.nextBoolean()) {
this.coefficients[rand.nextInt(this.coefficients.length)] += 1;
} else {
this.coefficients[rand.nextInt(this.coefficients.length)] -= 1;
}
}
public BigInteger calculateYValue(int xValue) {
return calculateYValue(new BigInteger("" + xValue));
}
private BigInteger lastCalc = null;
private BigInteger lastResult = null;
public BigInteger calculateYValue(BigInteger xValue) {
if (xValue.equals(lastCalc)) {
return lastResult;
}
BigInteger value = new BigInteger("0");
BigInteger coeff;
BigInteger base;
for (int i = 0; i < coefficients.length; i++) {
coeff = new BigInteger("" + coefficients[i]);
base = new BigInteger(xValue.toString());
base = base.pow(i);
coeff = coeff.multiply(base);
value = value.add(coeff);
}
lastCalc = new BigInteger(xValue.toString());
lastResult = new BigInteger(value.toString());
return value;
}
public boolean isPrimeForValue (int xValue) {
return this.calculateYValue(xValue).isProbablePrime(10000);
}
public int firstNonPrime(int maxTestNum) {
for (int i = 0; i <= maxTestNum; i++) {
if (!isPrimeForValue(i)) {
return i;
}
}
return 0;
}
public class doppelInt{
public int wert;
public int doppelte;
public doppelInt(int size, int doppelte) {
super();
this.wert = size;
this.doppelte = doppelte;
}
}
public doppelInt numDifferentPrimesInRange(int maxTestNum, boolean quiet) {
HashSet<BigInteger> set = new HashSet<BigInteger>();
int doppelte = 0;
for (int i = 0; i <= maxTestNum; i++) {
if (isPrimeForValue(i)) {
if (!quiet) {
System.out.println("p(" + i + ") = " + calculateYValue(i));
}
int size = set.size();
set.add(calculateYValue(i));
if (size == set.size()) {
doppelte++;
}
} else {
if (!quiet) {
System.out.println("p(" + i + ") = " + calculateYValue(i) + " XXX");
}
}
}
return new doppelInt(set.size(), doppelte);
}
public void setAvailableCoefficientsFromOther(PolynomeGenome other) {
int i = 0;
while (i < this.coefficients.length && i < other.coefficients.length) {
this.coefficients[i] = other.coefficients[i];
}
}
public void setCoefficient(int num, int value) {
this.coefficients[num] = value;
}
public int getCoefficient(int num) {
return this.coefficients[num];
}
/**
* Recombines two genomes (this and other). The existing objects remain
* unaltered. A one-point crossover is used.
*
* @param other The other genome.
*
* @return The recombination of this and other.
*/
public List<PolynomeGenome> recombineGenomes(PolynomeGenome other) {
if (this.coefficients.length != other.coefficients.length) {
throw new IllegalArgumentException();
}
int r = rand.nextInt(this.coefficients.length);
List<PolynomeGenome> genomes = new ArrayList<PolynomeGenome>(2);
genomes.add(new PolynomeGenome(this.coefficients.length));
genomes.add(new PolynomeGenome(this.coefficients.length));
PolynomeGenome pol0 = genomes.get(0);
PolynomeGenome pol1 = genomes.get(1);
for (int i = 0; i < r; i++) {
pol0.setCoefficient(i, this.coefficients[i]);
pol1.setCoefficient(i, other.coefficients[i]);
}
for (int i = r; i < this.coefficients.length; i++) {
pol0.setCoefficient(i, other.coefficients[i]);
pol1.setCoefficient(i, this.coefficients[i]);
}
return genomes;
}
public void randomize(int maxNumPlusMinus) {
for (int i = 0; i < this.coefficients.length; i++) {
boolean positive = rand.nextBoolean();
int num = (int) (rand.nextGaussian() * maxNumPlusMinus);
while ((i == 0 || i == 1) & num != 1 && rand.nextInt(10) != 1 && !new BigInteger("" + num).isProbablePrime(10000)) {
num = (int) (rand.nextGaussian() * maxNumPlusMinus);
}
if (positive) {
this.coefficients[i] = num;
} else {
this.coefficients[i] = -num;
}
}
}
public doppelInt fitness() {
return this.numDifferentPrimesInRange(100, true);
// return this.firstNonPrime(10000);
}
public static PolynomeGenome parsePolynome(String string) {
int grad = Integer.parseInt(string.split("\\{")[1].split("\\}")[0]);
PolynomeGenome p = new PolynomeGenome(grad);
String[] split = string.replace(" x", " 1x").split("= ")[1].replace("+", "+p").replace("-", "+m").split("\\+");
for (String s : split) {
if (!s.isEmpty()) {
String plain = s.replace("{", "").replace("}", "").replace("$", "").replace("x", "");
boolean positive = plain.charAt(0) == 'p';
int exponent = Integer.parseInt(plain.split("\\^")[1].replace(" ", ""));
int base = Integer.parseInt(plain.split("\\^")[0].replace("m", "").replace("p", "").replace(" ", ""));
if (positive) {
p.setCoefficient(exponent, base);
} else {
p.setCoefficient(exponent, -base);
}
}
}
return p;
}
public static void main(String[] args) {
PolynomeGenome p = PolynomeGenome.parsePolynome("$$p_{3}(x) = - 37x^{0} + 23x^{1} + x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{4}(x) = + 13x^{0} + 49x^{1} - x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 197x^{0} - 25x^{1} - x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 41x^{0} - x^{1} + x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 151x^{0} - 23x^{1} - x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 47x^{0} - 5x^{1} - x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 53x^{0} + 7x^{1} + x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 19x^{0} - 40x^{1} + 2x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 29x^{0} - 2x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + x^{0} + 30x^{1} - 2x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 1601x^{0} - 79x^{1} + x^{2}$$"); // Internet: 80 - 56+40
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - x^{0} + 21x^{1} - x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{3}(x) = - 37x^{0} + 23x^{1} + x^{2}$$");
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 47x^{0} - 5x^{1} + x^{2}$$"); // 43 - 84
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 43x^{0} + 3x^{1} - x^{2}$$"); // 42 - 85
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 199x^{0} + 2x^{2}$$"); // 10 - 88
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 131x^{0} - 19x^{1} + x^{2}$$"); // 50 - 79+10
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = + 173x^{0} - 23x^{1} + x^{2}$$"); // 52 - 78+12
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 251x^{0} + 29x^{1} - x^{2}$$"); // 55 - 76+15
p = PolynomeGenome.parsePolynome("$$p_{2}(x) = - 251x^{0} + 79x^{1} - x^{2}$$"); // 55 - 76+15
System.out.println(p);
// System.out.println(p.numDifferentPrimesInRange(100, false).wert + " + " + p.numDifferentPrimesInRange(100, false).doppelte);
BigInteger prime = new BigInteger("10");
prime = prime.pow(2000);
prime = prime.multiply(new BigInteger("6"));
prime = prime.add(new BigInteger("1"));
BigPrimeFactor pf = new BigPrimeFactor();
System.out.println(pf.getPrimeFactors(prime.toString()));
}
public Random getRand() {
return this.rand;
}
}