public static List <T> Choose <T>(this List <T> list, List <float> weights, int num) { //Debug.Log(list.Count + " : " + weights.Count + " : " + num); if (num >= list.Count) { return(list.Shuffled()); } List <T> stuff = list.Clone(); List <float> weightsCopy = weights.Clone(); List <T> chosen = new List <T>(); for (int i = 0; i < num; i++) { int index = RandomF.WeightedChoose(weightsCopy); //Debug.Log(index); chosen.Add(stuff[index]); stuff.RemoveAt(index); weightsCopy.RemoveAt(index); } return(chosen); }
public static NeuralNetwork ChooseOne(Generation generation, NeuralNetwork other_network = null) { float r = RandomF.NextFloat(); NeuralNetwork network = null; if (other_network != null) { r = RandomF.NextFloat(0f, 1f - other_network.fitness); } int index = 0; for (int i = 0; i < generation.Population; i++) { if (other_network != null) { if (i == other_network.id) { continue; } } r -= generation[i].fitness; if (r < 0) { index = i; break; } } network = generation[index]; return(network); }
public static NeuralNetwork Diseased(NeuralNetwork neuralNetwork, float rate) { for (int i = 0; i < neuralNetwork.Length; i++) { for (int j = 0; j < neuralNetwork[i].Length; j++) { for (int k = 0; k < neuralNetwork[i][j].Length; k++) { if (RandomF.NextFloat() < rate) { float value = neuralNetwork[i][j][k].x; var offset = RandomF.NextGaussian() * 0.5f; value += offset; neuralNetwork[i][j][k].x = value; } } } if (!neuralNetwork[i].IsBiasNull()) { for (int k = 0; k < neuralNetwork[i].GetBias().Length; k++) { if (RandomF.NextFloat() < rate) { float value = neuralNetwork[i].GetBias()[k].x; var offset = RandomF.NextGaussian() * 0.5f; value += offset; neuralNetwork[i].GetBias()[k].x = value; } } } } return(new NeuralNetwork(neuralNetwork)); }
void Start() { if (displayLine) { lineRenderer = transform.Require <LineRenderer>(); lineRenderer.useWorldSpace = false; } RaycastHit rayhit; float dist = 100; if (Physics.Raycast(transform.position, transform.forward, out rayhit)) { dist = (transform.position - rayhit.point).magnitude; if (decal != null) { Transform t = Instantiate(decal, rayhit.point, Quaternion.identity) as Transform; t.forward = rayhit.normal; t.parent = rayhit.collider.transform; t.Rotate(0, 0, RandomF.Range(0, 360)); } Unit unit = rayhit.collider.GetComponentOnOrAbove <Unit>(); if (unit != null) { unit.HitEffect(rayhit.point); unit.mortality.Hit(atk); } } if (lineRenderer != null) { lineRenderer.SetPosition(1, new Vector3(0, 0, dist)); } }
void SetMaterial() { int index = 0; if (usePerlin) { Vector3 pos = Vector3.Scale(transform.position, fieldScale); pos.x += pos.z; pos.y += pos.z * .5f; float f = Noise(pos.x, pos.y); index = (int)(materials.Length * f * .99999f); } else if (useSeed) { int prevSeed = Random.seed; Random.seed = seed++; if (weights.Length == 0) { index = materials.RandomIndex(); } else { index = RandomF.WeightedChoose(weights); } Random.seed = prevSeed; } else { if (weights.Length == 0) { index = materials.RandomIndex(); } else { index = RandomF.WeightedChoose(weights); } } if (renderer != null) { renderer.material = materials[index]; } foreach (Renderer r in alsoSetThese) { r.material = materials[index]; } Destroy(this); }
void Spawn() { if (randomness == RandomType.Seeded) { RandomF.Push(seed++); } else if (randomness == RandomType.Perlin) { Vector3 pos = transform.position; RandomF.Push((int)(SEEDSCALE * PerlinNoise.GetValue(pos.x, pos.z))); } int num = Random.Range(minNum, maxNum); for (int i = 0; i < num; i++) { Transform obj = Instantiate(things[(int)(things.Length * Random.value * .99999f)], transform.position + area.RandomInside(), Quaternion.identity) as Transform; obj.Rotate(orientation); obj.parent = transform; if (makePushable) { obj.gameObject.AddComponent <Pushable>(); } } if (randomness != RandomType.Normal) { RandomF.Pop(); } }
public void Unfocus() { string name = "Control" + RandomF.Range(10000, 19999); GUI.SetNextControlName(name); GUI.Button(new Rect(-100, -100, 5, 5), ""); GUI.FocusControl(name); }
void Update() { timeout += Time.deltaTime; if (timeout >= spawnTime) { timeout -= spawnTime; spawnTime = RandomF.Range(minTime, maxTime); Spawn(); } }
float Eval(bool b, float min, float max) { if (!b) { return(1.0f); } if (normalDist) { return(RandomF.Normal(min, max)); } return(RandomF.Range(min, max)); }
public static T RandomKey <T, K>(this Dictionary <T, K> d) { int i = 0; int chosen = (int)RandomF.Range(0, d.Count); foreach (T t in d.Keys) { if (i == chosen) { return(t); } i++; } return(default(T)); }
void Emit() { ParticleMaker p = MakeParticle(); p.position = transform.position + Vector3.Scale(range, RandomF.insideUnitSphere); p.size = RandomF.Range(minSize, maxSize); p.color = ColorF.HSVLerp(colorRangeStart, colorRangeEnd, Random.value); if (randomRotation) { p.rotation = RandomF.Range(0f, 360f); } p.angularVelocity = angularVelocity + RandomF.Range(-randAngularVelocity, randAngularVelocity); //p.color = C target.SetNext(p); }
public void Update() { const int objectCount = 20; _renderers.ForEach(_ => _.Dispose()); for (int i = 0; i < objectCount; i++) { var sceneObject = new Node(Scene); sceneObject.Transform.Parent = Transform; sceneObject.Transform.LocalPosition = 0.3f * new Vector3(-objectCount / 2 + i, 0, 0); var renderer = sceneObject.AddComponent <MeshRenderable>(); renderer.Mesh = _mesh; var material = renderer.Material = new Material(MaterialType.DiffuseColor); material.Color = new Vector4(RandomF.InsideUnitSphere().Normalized, 1); _renderers.Add(renderer); } }
public static Mesh Create(CloudShape shape, float diameter = 1, int vertexCount = 300000) { var vertices = new Vector3Buffer(vertexCount); var normals = new Vector3Buffer(vertexCount); var texCoords = new Vector2Buffer(vertices.Length); for (int i = 0; i < vertices.Length; i++) { Vector3 point; switch (shape) { case CloudShape.Sphere: point = RandomF.InsideUnitSphere(); break; case CloudShape.Cube: point = RandomF.InsideUnitCube(); break; default: throw new InvalidOperationException(); } vertices[i] = point * diameter; normals[i] = point.Normalized; texCoords[i] = new Vector2(vertices[i].X + 0.5f, -vertices[i].Y + 0.5f); } var result = new Mesh() { Vertices = vertices, TexCoords = texCoords, Normals = normals, Type = PrimitiveType.Points }; result.CalculateBounds(); return(result); }
private void AddDeferredTest() { var planeSo = new Node(_scene, "plane"); planeSo.AddComponent <MeshRenderable>().Mesh = Plane.Create(10, 10); planeSo.Transform.LocalRotation = new Quaternion(Vector3.Left, MathF.PI / 2); var sphereMesh = Icosahedron.Create(0.01f, 2); var sphereMaterial = new Material(MaterialType.FlatColor); sphereMaterial.Color = Vector4.One; var lightsParent = new Node(_scene, "lights"); lightsParent.AddComponent <Rotator>().Rotation = new Vector3(0.7, 0, 0); var lightCount = 50; for (int i = 0; i < lightCount; i++) { var so = new Node(_scene); so.Transform.Parent = lightsParent.Transform; var light = so.AddComponent <LightSource>(); var color = new Vector3(RandomF.Float(), RandomF.Float(), RandomF.Float()); color /= MathF.Max(color.X, color.Y, color.Z); light.Diffuse = color; light.Attenuation = new Vector3(0, 4f, 0); var position = Vector3.Zero; while (position.LengthSquared < 0.05f) { position = RandomF.InsideUnitSphere(); } so.Transform.LocalPosition = position * 5; var meshRenderer = so.AddComponent <MeshRenderable>(); meshRenderer.Mesh = sphereMesh; meshRenderer.Material = sphereMaterial; } }
private void InitBufferValues() { for (int i = 0; i < MaxParticles; i++) { _particlePositions[i] = RandomF.InsideUnitSphere(); var color = RandomF.InsideUnitCube(); var maxColor = MathF.Max(color.X, color.Y, color.Z); color /= maxColor; _particleColors[i] = new Vector4(color, 1); _particleData1[i] = new Vector4(50, 0, 0, 0); _particleData2[i] = new Vector4(0); _particleData3[i] = new Vector4(0); } var data = _computeData.Data; data.CurrentParticles = MaxParticles; data.MaxParticles = MaxParticles; data.Time = 0; data.DeltaTime = 0; _computeData.Data = data; }
public static NeuralNetwork Child(NeuralNetwork mum, NeuralNetwork dad) { NeuralNetwork child = new NeuralNetwork(mum); for (int i = 0; i < child.Length; i++) { for (int j = 0; j < child[i].Length; j++) { for (int k = 0; k < child[i][j].Length; k++) { float value = child[i][j][k].x; int rand = RandomF.Next(2); if (rand == 0) { value = dad[i][j][k].x; } child[i][j][k].x = value; } } if (!child[i].IsBiasNull()) { for (int k = 0; k < child[i].GetBias().Length; k++) { float value = child[i].GetBias()[k].x; int rand = RandomF.Next(2); if (rand == 0) { value = dad[i].GetBias()[k].x; } child[i].GetBias()[k].x = value; } } } return(child); }
private void formUI_Load(object sender, EventArgs e) { ToolTip toolTip = new ToolTip(); months.Add("January"); months.Add("February"); months.Add("March"); months.Add("April"); months.Add("May"); months.Add("June"); months.Add("July"); months.Add("August"); months.Add("September"); months.Add("October"); months.Add("November"); months.Add("December"); string path = @"images\"; string[] lines = File.ReadAllLines(path + "data.txt"); linesList.AddRange(lines); for (int i = 0; i < lines.Length; i++) { string[] split = lines[i].Split(' '); string name = split[0]; PictureBox pictureBoxPoster = new PictureBox() { Name = Path.GetFileName(name), Location = new Point((int)Map.Float(0f, RandomF.NextFloat(this.Width), this.Width, this.Width * padding, this.Width - (this.Width * padding) - size.Width), RandomF.Next(this.Height - size.Height)), Image = Image.FromFile(path + name), Size = size, SizeMode = PictureBoxSizeMode.StretchImage, Tag = i.ToString(), BorderStyle = BorderStyle.FixedSingle }; pictureBoxPoster.MouseDown += PictureBoxPoster_MouseDown; pictureBoxPoster.MouseUp += PictureBoxPoster_MouseUp; pictureBoxPoster.MouseMove += PictureBoxPoster_MouseMove; toolTip.SetToolTip(pictureBoxPoster, split[0]); pictureBoxes.Add(pictureBoxPoster); this.Controls.Add(pictureBoxPoster); // Categories string[] categoriesArr = split[4].Split(','); for (int j = 0; j < categoriesArr.Length; j++) { if (!categories.Contains(categoriesArr[j])) { categories.Add(categoriesArr[j]); } } // Directors string[] directorsArr = split[5].Split(','); for (int j = 0; j < directorsArr.Length; j++) { if (!directors.Contains(directorsArr[j])) { directors.Add(directorsArr[j]); } } // Stars string[] starsArr = split[6].Split(','); for (int j = 0; j < starsArr.Length; j++) { if (!stars.Contains(starsArr[j])) { stars.Add(starsArr[j]); } } } neuralNetwork = new NeuralNetwork(31 + 12 + 1 + categories.Count + directors.Count + stars.Count, 64, 1) { max_circle = 10000, learning_rate = 0.5f, momentum_rate = 0.96f }; }
public static Color RandomHue(float s, float v, float a = 1) { return(new Color(RandomF.Range(0, 1), s, v, a).HSVtoRGB()); }
public Synapse() { this.x = RandomF.NextFloat(-1f, 1f); this.error = 0f; }
private void PictureBoxPoster_MouseUp(object sender, MouseEventArgs e) { toggleMove = false; PictureBox pictureBox = (PictureBox)sender; bool train = false; float value = 0f; if (pictureBox.Location.X >= (this.Width - (this.Width * padding))) { value = 1f; train = true; existRight = true; } else if (pictureBox.Location.X <= (this.Width * padding - size.Width)) { value = 0f; train = true; existLeft = true; } else if (trainingData.Length > 0 && (existLeft && existRight)) { pictureBox.Location = new Point(lastPoint.X, pictureBox.Location.Y); pictureBox.BringToFront(); } if (train) { string[] split = linesList[int.Parse(pictureBox.Tag.ToString())].Split(' '); int day = int.Parse(split[1]) - 1; int month = months.IndexOf(split[2]); float year = Map.Float(0f, float.Parse(split[3]), 9999f, 0f, 1f); List <float> inputs = new List <float>(); for (int i = 0; i < 31; i++) { if (i == day) { inputs.Add(1f); } else { inputs.Add(0f); } } for (int i = 0; i < 12; i++) { if (i == month) { inputs.Add(1f); } else { inputs.Add(0f); } } inputs.Add(year); string[] categoriesArr = split[4].Split(','); for (int i = 0; i < categories.Count; i++) { bool exist = false; for (int j = 0; j < categoriesArr.Length; j++) { if (categories[i] == categoriesArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } string[] directorsArr = split[5].Split(','); for (int i = 0; i < directors.Count; i++) { bool exist = false; for (int j = 0; j < directorsArr.Length; j++) { if (directors[i] == directorsArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } string[] starsArr = split[6].Split(','); for (int i = 0; i < stars.Count; i++) { bool exist = false; for (int j = 0; j < starsArr.Length; j++) { if (stars[i] == starsArr[j]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } trainingData.Add(inputs.ToArray(), new[] { value }); stopwatch.Start(); neuralNetwork.Training(trainingData); stopwatch.Stop(); Console.WriteLine("Training : " + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); pictureBox.MouseDown -= PictureBoxPoster_MouseDown; pictureBox.MouseUp -= PictureBoxPoster_MouseUp; pictureBox.MouseMove -= PictureBoxPoster_MouseMove; pictureBoxes.Remove(pictureBox); if (existLeft && existRight) { for (int i = 0; i < pictureBoxes.Count; i++) { split = linesList[int.Parse(pictureBoxes[i].Tag.ToString())].Split(' '); day = int.Parse(split[1]) - 1; month = months.IndexOf(split[2]); year = Map.Float(1f, float.Parse(split[3]), 9999f, 0f, 1f); inputs = new List <float>(); for (int j = 0; j < 31; j++) { if (j == day) { inputs.Add(1f); } else { inputs.Add(0f); } } for (int j = 0; j < 12; j++) { if (j == month) { inputs.Add(1f); } else { inputs.Add(0f); } } inputs.Add(year); categoriesArr = split[4].Split(','); for (int j = 0; j < categories.Count; j++) { bool exist = false; for (int z = 0; z < categoriesArr.Length; z++) { if (categories[j] == categoriesArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } directorsArr = split[5].Split(','); for (int j = 0; j < directors.Count; j++) { bool exist = false; for (int z = 0; z < directorsArr.Length; z++) { if (directors[j] == directorsArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } starsArr = split[6].Split(','); for (int j = 0; j < stars.Count; j++) { bool exist = false; for (int z = 0; z < starsArr.Length; z++) { if (stars[j] == starsArr[z]) { exist = true; break; } } if (exist) { inputs.Add(1f); } else { inputs.Add(0f); } } stopwatch.Start(); Result[] guesses = neuralNetwork.Predict(inputs.ToArray()); stopwatch.Stop(); Console.WriteLine("Predict:" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); pictureBoxes[i].Location = new Point((int)Map.Float(0f, guesses[0].value, 1f, this.Width * padding, this.Width - (this.Width * padding) - size.Width), (int)RandomF.NextFloat(this.Height - size.Height)); } } } }
public static T Choose <T>(this List <T> list, float[] weights) { return(list[RandomF.WeightedChoose(weights)]); }
void Start() { spawnTime = RandomF.Range(minTime, maxTime); }
//Choose an element from an array using weights public static T Choose <T>(this T[] array, float[] weights) { int index = (int)Mathf.Clamp(RandomF.WeightedChoose(weights), 0, array.Length - 1); return(array[index]); }