Projects/Open/Add project from disk
で追加遺伝的
はこの木構造を作るときに突然変異や交叉といった進化論の仕組みを取り入れていることに由来します。遺伝的に組み合わせを変えていくので、勾配降下法のように「評価関数で定量的に評価して、微分して、...」といったことをせずに学習を進められます。due to closure, all nodes are the same type - float, in this case
// save state data in an object of this type before evaluating the expression tree // terminal functions can then look at it, and they or stateful functions can modify it, if desired
//GP全体を司るクラスのインスタンスを定義
Brain=new Engine<Vector3, ProblemState>();
//GPの変数名を定義
Brain.AddVariable("XY0");
Brain.AddVariable("X0Y");
Brain.AddVariable("YX0");
Brain.AddVariable("Y0X");
Brain.AddVariable("0XY");
Brain.AddVariable("0YX");
//GPで使う演算子を定義
//unary
Brain.AddFunction((a) => new Vector3 (Mathf.Abs(a.x),Mathf.Abs(a.y),Mathf.Abs(a.z)), "abs");
Brain.AddFunction((a) => new Vector3 (CustomLog(a.x),CustomLog(a.y),CustomLog(a.z)), "Log");
Brain.AddFunction((a) => new Vector3 (Mathf.Exp(a.x),Mathf.Exp(a.y),Mathf.Exp(a.z)), "exp");
Brain.AddFunction((a) => new Vector3 (Mathf.Sin(a.x),Mathf.Sin(a.y),Mathf.Sin(a.z)), "sin");
Brain.AddFunction((a) => new Vector3 (Mathf.Cos(a.x),Mathf.Cos(a.y),Mathf.Cos(a.z)), "cos");
Brain.AddFunction((a) => new Vector3 (Sqrt(a.x),Sqrt(a.y),Sqrt(a.z)), "sqrt");
//Brain.AddFunction((a) => new Vector3 (Mathf.Sign(a.x),Mathf.Sign(a.y),Mathf.Sign(a.z)), "sign");
Brain.AddFunction((a) => -1.0f*a, "Reverse");
//binary
Brain.AddFunction((a, b) => a + b, "Plus");
Brain.AddFunction((a, b) => a - b, "Minus");
Brain.AddFunction((a, b) => new Vector3(a.x*b.x,a.y*b.y,a.z*b.z), "Mul");
Brain.AddFunction((a, b) => new Vector3(CustomDiv(a.x,b.x),CustomDiv(a.y,b.y),CustomDiv(a.z,b.z)), "Div");
Brain.AddFunction((a, b) => Vector3.Max(a,b), "Max");
Brain.AddFunction((a, b) => Vector3.Min(a,b), "Min");
//Brain.AddFunction((a, b) => new Vector3(CustomPow(a.x,b.x),CustomPow(a.y,b.y),CustomPow(a.z,b.z)), "Pow");
Brain.AddFunction((a, b) => new Vector3(Hypot(a.x,b.x),Hypot(a.y,b.y),Hypot(a.z,b.z)), "Hypot");
Brain.AddFunction((a, b) => Vector3.Lerp(a,b,0.5f), "Lerp");
Brain.AddFunction((a, b) => new Vector3(Mix(a.x,b.x),Mix(a.y,b.y),Mix(a.z,b.z)), "Mix");
public static Vector3 ScanTree(CandidateSolution<Vector3, ProblemState> candidate, Vector2 pos){
//GPの変数ノードに実際に値を代入。x座標、y座標、0を並び替えた6通りのベクトル。
candidate.SetVariableValue("XY0",new Vector3(pos.x,pos.y,0));
candidate.SetVariableValue("X0Y",new Vector3(pos.x,0,pos.y));
candidate.SetVariableValue("YX0",new Vector3(pos.y,pos.x,0));
candidate.SetVariableValue("Y0X",new Vector3(pos.y,0,pos.x));
candidate.SetVariableValue("0XY",new Vector3(0,pos.x,pos.y));
candidate.SetVariableValue("0YX",new Vector3(0,pos.y,pos.x));
//GPの1個体の木の計算結果を取得
return candidate.Evaluate();
}
void Initialize(){
...
//GP木の評価を定義
Brain.AddScanTreeFunction((c, p) => ScanTree(c, p));
...
}
//個体の生成など、GPを初期化
Brain.InitTrainer();
//GP木をもとにテクスチャを生成して貼り付け
GetTextureFromTree();