本文整理汇总了C#中Chromosome.setBranches方法的典型用法代码示例。如果您正苦于以下问题:C# Chromosome.setBranches方法的具体用法?C# Chromosome.setBranches怎么用?C# Chromosome.setBranches使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chromosome
的用法示例。
在下文中一共展示了Chromosome.setBranches方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
void Start()
{
spw = Spawner.getInstance();
settings = Settings.getInstance();
eth = Ether.getInstance();
max_root_scale = new Vector3();
max_root_scale.x = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["x"].ToString() );
max_root_scale.y = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["y"].ToString() );
max_root_scale.z = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["z"].ToString() );
min_root_scale = new Vector3();
min_root_scale.x = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["x"].ToString() );
min_root_scale.y = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["y"].ToString() );
min_root_scale.z = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["z"].ToString() );
max_limb_scale = new Vector3();
max_limb_scale.x = float.Parse( settings.contents["creature"]["limb"]["max_limb_scale"]["x"].ToString() );
max_limb_scale.y = float.Parse( settings.contents["creature"]["limb"]["max_limb_scale"]["y"].ToString() );
max_limb_scale.z = float.Parse( settings.contents["creature"]["limb"]["max_limb_scale"]["z"].ToString() );
min_limb_scale = new Vector3();
min_limb_scale.x = float.Parse( settings.contents["creature"]["limb"]["min_limb_scale"]["x"].ToString() );
min_limb_scale.y = float.Parse( settings.contents["creature"]["limb"]["min_limb_scale"]["y"].ToString() );
min_limb_scale.z = float.Parse( settings.contents["creature"]["limb"]["min_limb_scale"]["z"].ToString() );
starting_creatures = (int) settings.contents["ether"]["starting_creatures"];
creature_spread = float.Parse( settings.contents["ether"]["creature_spread"].ToString() );
double creature_init_energy = (double) settings.contents["creature"]["init_energy"];
int branch_limit = (int) settings.contents["creature"]["branch_limit"];
int recurrence_limit = (int) settings.contents["creature"]["recurrence_limit"];
/*
* For each new creature, generate random genes and spawn the bugger
*/
for (int i=0; i<starting_creatures; i++) {
chromosome = new Chromosome();
// random colours
Color col = new Color( (float)Random.Range(0.0F,1.0F),
(float)Random.Range(0.0F,1.0F),
(float)Random.Range(0.0F,1.0F)
);
chromosome.setColour(col.r, col.g, col.b);
chromosome.setLimbColour(col.r, col.g, col.b);
chromosome.hunger_threshold = float.Parse(settings.contents["creature"]["hunger_threshold"].ToString());
// random root scale
Vector3 rootScale = new Vector3((float) Random.Range(min_root_scale.x,max_root_scale.x),
(float) Random.Range(min_root_scale.y,max_root_scale.y),
(float) Random.Range(min_root_scale.z,max_root_scale.z)
);
chromosome.setRootScale(rootScale);
// random initial limbs
int bs = Random.Range (1,branch_limit+1);
chromosome.setNumBranches(bs);
ArrayList branches = new ArrayList();
for (int j=0; j<bs; j++) {
ArrayList limbs = new ArrayList();
int recurrences = Random.Range(0,recurrence_limit);
chromosome.num_recurrences[j] = recurrences;
for (int k=0; k<=recurrences; k++) {
Vector3 scale = new Vector3 ((float) Random.Range(min_limb_scale.x,max_limb_scale.x),
(float) Random.Range(min_limb_scale.y,max_limb_scale.y),
(float) Random.Range(min_limb_scale.z,max_limb_scale.z)
);
Vector3 position = Utility.RandomPointInsideCube(rootScale);
ArrayList limb = new ArrayList();
limb.Add (position);
limb.Add (scale);
limbs.Add (limb);
}
branches.Add(limbs);
}
chromosome.setBaseFequency (Random.Range (3,20));
chromosome.setBaseAmplitude (Random.Range (3,6));
chromosome.setBasePhase (Random.Range (0,360));
chromosome.setBranches(branches);
if (eth.enoughEnergy(creature_init_energy)) {
spw.spawn(Utility.RandomVec(-creature_spread,creature_spread,creature_spread), Utility.RandomRotVec(), creature_init_energy, chromosome);
eth.subtractEnergy(creature_init_energy);
}
}
}
示例2: crossover
//.........这里部分代码省略.........
}
else
{
c_branches = c2_branches;
other_crt_branches = c1_branches;
}
select = Random.Range(0,2);
if (select == 0)
{
c.setRootScale(c1.getRootScale());
}
else
{
c.setRootScale(c2.getRootScale());
}
// Randomly select attributes from the selected creature's limbs to
// assign to child creature's limbs
c.num_recurrences = new int[c_branches.Count];
for (int i=0; i<c_branches.Count; i++)
{
ArrayList c_limbs = (ArrayList) c_branches[i];
c.num_recurrences[i] = c_limbs.Count;
for (int j=1; j<c_limbs.Count; j++)
{
ArrayList c_attributes = (ArrayList) c_limbs[j];
//select random limb segment from other creature
ArrayList other_crt_limbs = (ArrayList) other_crt_branches[Random.Range (0,other_crt_branches.Count)];
ArrayList other_crt_attributes = (ArrayList) other_crt_limbs[Random.Range(0,other_crt_limbs.Count)];
Vector3 c_scale = (Vector3) c_attributes[1];
Vector3 other_crt_scale = (Vector3) other_crt_attributes[1];
for (int s=0; s<3; s++)
{
rand = rnd.NextDouble();
if (rand < rate) {
c_scale[s] = other_crt_scale[s];
}
}
//select random limb segment from other creature
other_crt_limbs = (ArrayList) other_crt_branches[Random.Range (0,other_crt_branches.Count)];
other_crt_attributes = (ArrayList) other_crt_limbs[Random.Range(0,other_crt_limbs.Count)];
Vector3 c_pos = (Vector3) c_attributes[0];
Vector3 other_crt_pos = (Vector3) other_crt_attributes[0];
for (int p=0; p<3; p++)
{
rand = rnd.NextDouble();
if (rand < rate)
{
c_pos[p] = other_crt_pos[p];
}
}
}
c_branches[i] = c_limbs;
c.num_branches = c_branches.Count;
}
// Crossover frequency and amplitude
c.base_joint_amplitude = c1.base_joint_amplitude;
c.base_joint_frequency = c1.base_joint_frequency;
c.base_joint_phase = c1.base_joint_phase;
rand = rnd.NextDouble();
if (rand < 0.5f)
{
c.base_joint_amplitude = c2.base_joint_amplitude;
}
rand = rnd.NextDouble();
if (rand < 0.5f)
{
c.base_joint_frequency = c2.base_joint_frequency;
}
rand = rnd.NextDouble();
if (rand < 0.5f)
{
c.base_joint_phase = c2.base_joint_phase;
}
rand = rnd.NextDouble();
if (rand < 0.5f)
{
c.hunger_threshold = c2.hunger_threshold;
}
else
{
c.hunger_threshold = c1.hunger_threshold;
}
c.setBranches(c_branches);
return (c);
}
示例3: mutate
public static Chromosome mutate(Chromosome c, double rate, float factor)
{
// Mutate colour
float[] cs = new float[3];
Color cc = c.getColour();
cs[0] = cc.r;
cs[1] = cc.g;
cs[2] = cc.b;
for (int i=0; i<3; i++)
{
rand = rnd.NextDouble();
if (rand < rate)
cs[i] += randomiseGene(factor);
}
c.setColour(cs[0], cs[1], cs[2]);
// Mutate root scale
Vector3 rc = c.getRootScale();
if (rc.x > 1F && rc.y > 1F && rc.z > 1F)
{
float[] rs = new float[3];
rs[0] = rc.x;
rs[1] = rc.y;
rs[2] = rc.z;
for (int i=0; i<3; i++)
{
rand = rnd.NextDouble();
if (rand < rate)
rs[i] += randomiseGene(factor);
}
Vector3 rootScale = new Vector3 (rs[0], rs[1], rs[2]);
c.setRootScale(rootScale);
}
// mutate limbs
cc = c.getLimbColour();
cs[0] = cc.r;
cs[1] = cc.g;
cs[2] = cc.b;
for (int i=0; i<3; i++)
{
rand = rnd.NextDouble();
if (rand < rate)
cs[i] += randomiseGene(factor);
}
c.setLimbColour(cs[0], cs[1], cs[2]);
ArrayList branches = c.branches;
for (int b=0; b<branches.Count; b++)
{
ArrayList limbs = (ArrayList) branches[b];
for (int i=0; i<limbs.Count; i++)
{
ArrayList limb = (ArrayList) limbs[i];
Vector3 v = (Vector3) limb[1];
for (int k=0; k<3; k++)
{
rand = rnd.NextDouble();
if(rand < rate)
v[k] += randomiseGene(factor);
}
}
}
// mutate base frequency and amplitude
rand = rnd.NextDouble();
if(rand < rate)
{
c.base_joint_amplitude += randomiseGene(factor);
}
rand = rnd.NextDouble();
if(rand < rate)
{
c.base_joint_frequency += randomiseGene(factor);
}
rand = rnd.NextDouble();
if(rand < rate)
{
c.base_joint_phase += randomiseGene(factor);
}
rand = rnd.NextDouble();
if(rand < rate)
{
c.hunger_threshold += randomiseGene(factor);
}
c.setBranches(branches);
return c;
}
示例4: LoadCreatures
void LoadCreatures()
{
creatures.Clear();
string[] fs;
creature_files = Directory.GetDirectories(creatures_folder);
foreach (var s in creature_files)
{
fs = Directory.GetFiles(s, "*.json");
foreach (var f in fs)
{
Chromosome chromosome = new Chromosome();
sr = new StreamReader(f);
raw_contents = sr.ReadToEnd();
contents = JsonMapper.ToObject(raw_contents);
sr.Close();
string name = contents["name"].ToString();
Color root_col = new Color();
root_col.r = float.Parse(contents["attributes"]["colour"]["r"].ToString());
root_col.g = float.Parse(contents["attributes"]["colour"]["g"].ToString());
root_col.b = float.Parse(contents["attributes"]["colour"]["b"].ToString());
root_col.a = 1;
Color limb_col = new Color();
limb_col.r = float.Parse(contents["attributes"]["limb_colour"]["r"].ToString());
limb_col.g = float.Parse(contents["attributes"]["limb_colour"]["g"].ToString());
limb_col.b = float.Parse(contents["attributes"]["limb_colour"]["b"].ToString());
limb_col.a = 1;
Vector3 root_scale = new Vector3();
root_scale.x = float.Parse(contents["attributes"]["root_scale"]["x"].ToString());
root_scale.y = float.Parse(contents["attributes"]["root_scale"]["y"].ToString());
root_scale.z = float.Parse(contents["attributes"]["root_scale"]["z"].ToString());
float bjf = float.Parse(contents["attributes"]["base_joint_frequency"].ToString());
float bja = float.Parse(contents["attributes"]["base_joint_amplitude"].ToString());
float bjp = float.Parse(contents["attributes"]["base_joint_phase"].ToString());
float ht = float.Parse(contents["attributes"]["hunger_threshold"].ToString());
ArrayList branches = new ArrayList();
int num_branches = (int)contents["attributes"]["branches"];
chromosome.num_recurrences = new int[num_branches];
for (int j = 0; j < num_branches ; j++)
{
ArrayList limbs = new ArrayList();
int recurrences = (int)contents["attributes"]["recurrences"][j];
chromosome.num_recurrences[j] = recurrences;
for (int k = 0; k < recurrences; ++k)
{
float x = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["position"]["x"].ToString());
float y = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["position"]["y"].ToString());
float z = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["position"]["z"].ToString());
Vector3 position = new Vector3(x,y,z);
x = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["scale"]["x"].ToString());
y = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["scale"]["y"].ToString());
z = float.Parse(contents["attributes"]["limbs"][j.ToString()][k]["scale"]["z"].ToString());
Vector3 scale = new Vector3(x,y,z);
ArrayList limb = new ArrayList();
limb.Add(position);
limb.Add(scale);
limbs.Add(limb);
}
branches.Add(limbs);
}
chromosome.colour = root_col;
chromosome.limb_colour = limb_col;
chromosome.hunger_threshold = ht;
chromosome.setRootScale(root_scale);
chromosome.setBaseFequency(bjf);
chromosome.setBaseAmplitude(bja);
chromosome.setBasePhase(bjp);
chromosome.setBranches(branches);
creatures.Add(name, chromosome);
}
}
}