void populate() { m_populateRetry = true; var dx = new UniformIntDistribution(m_region.min.x, m_region.max.x); var dy = new UniformIntDistribution(m_region.min.y, m_region.max.y); var gen = new StaticRandomGenerator <DefaultRandomGenerator>(); var x = dx.Next(gen); var y = dy.Next(gen); if (m_snake != null && m_snake.haveNode(x, y)) { return; } if (!m_populatableZone.Contains(TilemapInfos.instance.tileID(x, y))) { return; } var obj = Instantiate(m_powerupPrefab, new Vector3(x, y, -0.5f), Quaternion.identity); m_currentPowerup = obj.GetComponent <SnakePowerupLogic>(); m_populateRetry = false; }
public void kill() { m_animator.SetTrigger(dieTrigger); Event <BossDieEvent> .Broadcast(new BossDieEvent()); var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); var d = new UniformFloatDistribution(-m_explosionRadius, m_explosionRadius); for (float i = 0; i <= m_explosionTime; i += m_explosionDelta) { DOVirtual.DelayedCall(i, () => { var pos = transform.position + new Vector3(d.Next(rand), d.Next(rand), -0.5f); Instantiate(m_explosionPrefab, pos, Quaternion.identity); }); } DOVirtual.DelayedCall(m_explosionTime, () => { DOVirtual.DelayedCall(0.2f, () => gameObject.SetActive(false)); Event <PlaySoundEvent> .Broadcast(new PlaySoundEvent(m_bossExplosionClip)); for (int i = 0; i < m_explosionFinalCount; i++) { var pos = transform.position + new Vector3(d.Next(rand), d.Next(rand), -0.5f); Instantiate(m_explosionPrefab, pos, Quaternion.identity); } }); }
public GameObject(int objectId, int orientation, int type, int vertexHeightBottomRight, int vertexHeightTopRight, int vertexHeightBottomLeft, int vertexHeightTopLeft, int animationId, bool animating) { this.objectId = objectId; this.type = type; this.orientation = orientation; this.vertexHeightBottomLeft = vertexHeightBottomLeft; this.vertexHeightBottomRight = vertexHeightBottomRight; this.vertexHeightTopRight = vertexHeightTopRight; this.vertexHeightTopLeft = vertexHeightTopLeft; if (animationId != -1) { animation = AnimationSequence.animations[animationId]; frame = 0; nextFrameTime = clientInstance.CurrentTick; if (animating && animation.frameStep != -1) { frame = (int)(StaticRandomGenerator.Next() * animation.frameCount); nextFrameTime -= (int)(StaticRandomGenerator.Next() * animation.getFrameLength(frame)); } } GameObjectDefinition definition = GameObjectDefinition.getDefinition(this.objectId); varBitId = definition.varBitId; configId = definition.configIds; childrenIds = definition.childIds; }
void spawnBoss() { var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); for (int i = 0; i < GameInfos.bossKillCount + 1; i++) { Vector2 pos = Vector2.zero; for (int j = 0; j < 10; j++) { pos = new UniformVector2SquareDistribution(-m_spawnRadius, m_spawnRadius, -m_spawnRadius, m_spawnRadius).Next(rand); if (pos.sqrMagnitude < m_dontSpawnRadius * m_dontSpawnRadius) { continue; } break; } var mob = Instantiate(m_bossPrefab); mob.transform.position = new Vector3(pos.x, pos.y, -1); } var exit = Instantiate(m_exitPrefab); exit.transform.position = new Vector3(0, 200, -1); exit.name = m_exitPrefab.name; }
void populate() { Matrix <int> tiles = new Matrix <int>(-1); var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); var d = new UniformIntDistribution(m_tiles.Count() - 1); for (int i = m_bounds.min.x; i <= m_bounds.max.x; i++) { for (int j = m_bounds.min.y; j < m_bounds.max.y; j++) { tiles.set(i, j, d.Next(rand)); } } for (int i = m_bounds.min.x; i <= m_bounds.max.x; i++) { for (int j = m_bounds.min.y; j < m_bounds.max.y; j++) { if (m_tilemap.GetTile(new Vector3Int(i, j, 0)) == null) { m_tilemap.SetTile(new Vector3Int(i, j, 0), m_tiles[tiles.get(i, j)]); } } } }
private void Awake() { var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); var d = new UniformFloatDistribution(m_minModifierLevel, m_maxModifierLevel); modifier.life *= (int)((float)modifier.life * m_baseModifierValue + d.Next(rand) * GameInfos.level); modifier.speed *= (int)((float)modifier.speed * m_baseModifierValue + d.Next(rand) * GameInfos.level); modifier.fireRate *= (int)((float)modifier.fireRate * m_baseModifierValue + d.Next(rand) * GameInfos.level); modifier.power *= (int)((float)modifier.power * m_baseModifierValue + d.Next(rand) * GameInfos.level); }
private void Awake() { if ((GameInfos.level + 1) % m_bossLevel == 0) { spawnBoss(); return; } var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); int nb = m_enemyCountBase + new UniformIntDistribution((int)(m_minEnemyCountPerLevel * GameInfos.level), (int)(m_maxEnemyCountPerLevel * GameInfos.level) + 1).Next(rand); for (int i = 0; i < nb; i++) { Vector2 pos = Vector2.zero; for (int j = 0; j < 10; j++) { pos = new UniformVector2SquareDistribution(-m_spawnRadius, m_spawnRadius, -m_spawnRadius, m_spawnRadius).Next(rand); if (pos.sqrMagnitude < m_dontSpawnRadius * m_dontSpawnRadius) { continue; } break; } List <float> weights = new List <float>(); foreach (var en in m_enemy) { weights.Add(en.baseWeight + en.levelWeight * GameInfos.level); } var index = new DiscreteDistribution(weights).Next(rand); var e = m_enemy[index]; var mob = Instantiate(e.enemyPrefab); mob.transform.position = new Vector3(pos.x, pos.y, -1); Modifier m = new Modifier(); m.life = (int)(e.levelLife * GameInfos.level); m.speed = (int)(e.levelSpeed * GameInfos.level); m.fireRate = (int)(e.levelFireRate * GameInfos.level); m.power = (int)(e.levelPower * GameInfos.level); var s = mob.GetComponent <ShipLogic>(); s.modifiers.Add(m); s.updateModifierStats(); } spawnExit(); }
public DynamicCombinedMutation( IList <IMutationOperation <List <Genome>, Problem, Fitness> > operations, IList <double> initial, IList <double> increase_rounds, IList <double> decrease_rounds, IList <double> decrease_time) : base(StaticRandomGenerator.Get(), operations, initial) { _initial = initial; _decrease_rounds = decrease_rounds; _increase_rounds = increase_rounds; _decrease_time = decrease_time; }
void startButtons() { float multiplier = 1 + m_currentTime / m_timeReductionDelay; var gen = new StaticRandomGenerator <DefaultRandomGenerator>(); float multiButtonProbability = Mathf.Min(m_maxMultipleButtonChance, m_multipleButtonChance * multiplier); var dMultibutton = new BernoulliDistribution(multiButtonProbability); var dButtons = new UniformIntDistribution(m_buttons.Count - 1); do { m_buttons[dButtons.Next(gen)].startButton(m_buttonActionTime); }while (dMultibutton.Next(gen)); selectNextDelay(); }
internal static Genome SelectRandom(Individual <List <Genome>, Problem, Fitness> indivdual, bool smallest_first) { double random_time; double current_time; if (smallest_first) { double average = indivdual.Fitness.TotalTime / indivdual.Genomes.Count; random_time = StaticRandomGenerator.Get().Generate( indivdual.Fitness.TotalTime); current_time = 0; for (int idx = 0; idx < indivdual.Genomes.Count; idx++) { double time = indivdual.Fitness.Times[idx]; if (time < average) { // small round: make big time = time + (average - time); } else { // large round: make small time = time - (time - average); } current_time = current_time + time; if (current_time > random_time) { return(indivdual.Genomes[idx]); } } } else { random_time = StaticRandomGenerator.Get().Generate( indivdual.Fitness.TotalTime); current_time = 0; for (int idx = 0; idx < indivdual.Genomes.Count; idx++) { current_time = current_time + indivdual.Fitness.Times[idx]; if (current_time > random_time) { return(indivdual.Genomes[idx]); } } } throw new Exception("At least one genome should be found!"); }
void updateTarget() { m_time = m_updateTime; int x = Mathf.RoundToInt(m_pacman.transform.position.x); int y = Mathf.RoundToInt(m_pacman.transform.position.y); var gen = new StaticRandomGenerator <DefaultRandomGenerator>(); if (new BernoulliDistribution().Next(gen)) { var dir = new UniformVector2SquareDistribution(m_radius).Next(gen); x = Mathf.RoundToInt(transform.position.x + dir.x); y = Mathf.RoundToInt(transform.position.y + dir.y); } m_ghost.setTarget(x, y); }
void spawnExit() { var rand = new StaticRandomGenerator <DefaultRandomGenerator>(); Vector2 pos = Vector2.zero; for (int i = 0; i < 10; i++) { pos = new UniformVector2SquareDistribution(-m_spawnRadius, m_spawnRadius, -m_spawnRadius, m_spawnRadius).Next(rand); if (pos.sqrMagnitude < m_dontSpawnRadius * m_dontSpawnRadius) { continue; } break; } var mob = Instantiate(m_exitPrefab); mob.transform.position = new Vector3(pos.x, pos.y, -1); mob.name = m_exitPrefab.name; }
/// <summary> /// Calculates and places the given cities by randomly selecting them and placing them into the least cost position. /// </summary> /// <param name="problem"></param> /// <param name="calculator"></param> /// <param name="genomes"></param> /// <param name="cities_to_place"></param> public void DoFast(GeneticProblem problem, FitnessCalculator calculator, List <int> genomes, List <int> cities_to_place) { this.RaiseNewStart(); while (cities_to_place.Count > 0) { // select a random city. int random_idx = StaticRandomGenerator.Get().Generate(cities_to_place.Count); int city = cities_to_place[random_idx]; this.RaiseNewCity(city); // remove the selected city. cities_to_place.RemoveAt(random_idx); // calculate the result. BestPlacementResult result = this.CalculateBestPlacement(problem, calculator, genomes, city); // do the placement. this.Do(genomes, result); this.RaiseNewRoute(genomes); } }
public GeoCoordinate OffsetRandom(Meter meter) { return(this.OffsetRandom(StaticRandomGenerator.Get(), meter)); }
void RealyStartSpawning() { m_time = m_spawningTime; m_velocity = new Vector2(0, 0); var rand = new StaticRandomGenerator <MT19937>(); float spawnMultiplier = m_isPowered ? m_poweredSpawnMultiplier : 1; int spawnNb = Mathf.RoundToInt(new UniformIntDistribution(m_minSpawnNb, m_maxSpawnNb + 1).Next(rand) * spawnMultiplier); if (spawnNb > 0) { var pos = transform.position; float angle = new UniformFloatDistribution(0, Mathf.PI * 2).Next(rand); float minAngle = Mathf.PI / spawnNb / 2; float maxAngle = minAngle * 4; int tryCount = 0; for (int i = 0; i < spawnNb; i++) { var dir = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * new UniformFloatDistribution(m_minSpawnDistance, m_maxSpawnDistance).Next(rand); var ray = Physics2D.Raycast(transform.position, dir, dir.magnitude, LayerMask.GetMask("Default")); if (ray.collider != null) { tryCount++; if (tryCount < 10) { i--; } else { tryCount = 0; } } else { tryCount = 0; DOVirtual.DelayedCall(new UniformFloatDistribution(m_minSpawnDelay, m_maxSpawnDelay).Next(rand), () => { if (this == null) { return; } DOVirtual.DelayedCall(0.3f, () => { if (this == null) { return; } var index = new UniformIntDistribution(0, m_entities.Count).Next(rand); var obj = Instantiate(m_entities[index]); obj.transform.position = pos + new Vector3(dir.x, dir.y); }); if (m_spawnFx != null) { var fx = Instantiate(m_spawnFx); fx.transform.position = pos + new Vector3(dir.x, dir.y, -0.1f); Destroy(fx, 2); } SoundSystem.Instance().play(m_spawnSound, 0.5f, true); }); } angle += new UniformFloatDistribution(minAngle, maxAngle).Next(rand); } } m_animator.SetTrigger("Invoke"); m_state = State.Spawning; }
/// <summary> /// Executes a solver procedure. /// </summary> /// <param name="problem"></param> /// <returns></returns> internal override MaxTimeSolution Solve(MaxTimeProblem problem) { _customers = problem.Customers; //float[] solutions = OsmSharp.Math.VRP.Core.BestPlacement.CheapestInsertionHelper.CalculateBestValues( // problem, _customers); generations = 0; _max_generations = 10000000; // calculate one tsp solution. //Tools.Math.TSP.ISolver tsp_solver = new OsmSharp.Math.TSP.EdgeAssemblyGenetic.EdgeAssemblyCrossOverSolver(_population, _stagnation, // new OsmSharp.Math.TSP.Genetic.Solver.Operations.Generation._3OptGenerationOperation(), // new OsmSharp.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover(30, // OsmSharp.Math.TSP.Genetic.Solver.Operations.CrossOver.EdgeAssemblyCrossover.EdgeAssemblyCrossoverSelectionStrategyEnum.SingleRandom, // true)); //IRoute tsp_solution = tsp_solver.Solve(new OsmSharp.Routing.VRP.NoDepot.MaxTime.TSPPlacement.TSPProblem( // problem)); // initialize the generation. IGenerationOperation <MaxTimeSolution, MaxTimeProblem, Fitness> generation = //new SolverGenerationOperation(new TSPPlacement.TSPPlacementSolver<ResolvedType>( // this.Router, this.Max, this.DeliveryTime, tsp_solution)); new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.Generation.RandomBestPlacement(); //new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>( // this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, false, 1f, null, null)); //new SolverGenerationOperation(new CheapestInsertionSolverWithImprovements<ResolvedType>( // this.Router, this.Max, this.DeliveryTime, 5, 0.1f, true, 0.1f, true, 1f)); // initialize the crossover. ICrossOverOperation <MaxTimeSolution, MaxTimeProblem, Fitness> cross_over = new OsmSharp.Routing.VRP.NoDepot.MaxTime.Genetic.CrossOver.RouteExchangeOperation(); // initialize the mutation. //IMutationOperation<MaxTimeSolution, MaxTimeProblem, Fitness> mutation = // new VehicleMutation(); List <IMutationOperation <MaxTimeSolution, MaxTimeProblem, Fitness> > mutators = new List <IMutationOperation <MaxTimeSolution, MaxTimeProblem, Fitness> >(); mutators.Add(new VehicleMutation()); //mutators.Add(new ThreeOptMutation()); //mutators.Add(new RedivideRouteMutation()); mutators.Add(new RoutePartExchangeMutation()); if (_probabilities == null) { _probabilities = new List <double>(); _probabilities.Add(0.2); _probabilities.Add(0.6); _probabilities.Add(0.2); } CombinedMutation <MaxTimeSolution, MaxTimeProblem, Fitness> mutation = new CombinedMutation <MaxTimeSolution, MaxTimeProblem, Fitness>( StaticRandomGenerator.Get(), mutators, _probabilities); SolverSettings settings = new SolverSettings(_stagnation, _population, _max_generations, _elitism_percentage, _cross_percentage, _mutation_percentage); MaxTimeProblem genetic_problem = problem;// new MaxTimeProblem(max, problem, solutions); Solver <MaxTimeSolution, MaxTimeProblem, Fitness> solver = new Solver <MaxTimeSolution, MaxTimeProblem, Fitness>(genetic_problem, settings, new TournamentBasedSelector <MaxTimeSolution, MaxTimeProblem, Fitness>(5, 0.5), mutation, //new ThreeOptMutation(), cross_over, // new RouteExchangeOperation(), //new RouteExchangeOperation(), //new RouteExchangeAndVehicleOperation(), // Order1CrossoverOperation() generation, //new RandomBestPlacement(),//new RandomGeneration(), //new RandomBestPlacement(), new FitnessCalculator()); solver.NewFittest += new Solver <MaxTimeSolution, MaxTimeProblem, Fitness> .NewFittestDelegate(solver_NewFittest); //solver.NewGeneration += new Solver<MaxTimeSolution, Problem, Fitness>.NewGenerationDelegate(solver_NewGeneration); Individual <MaxTimeSolution, MaxTimeProblem, Fitness> solution = solver.Start(null); //this.solver_NewFittest(solution); MaxTimeSolution routes = solution.Genomes; long ticks_after = DateTime.Now.Ticks; StringBuilder sizes = new StringBuilder(); foreach (int size in routes.Sizes) { sizes.Append(size); sizes.Append(" "); } StringBuilder weights = new StringBuilder(); foreach (double weight in solution.Fitness.Weights) { weights.Append(weight.ToString(CultureInfo.InvariantCulture)); weights.Append(" "); } return(routes); }
public static string RandomString(int length) { byte[] numArray = new byte[length]; StaticRandomGenerator.Get().Generate(numArray); return(Convert.ToBase64String(numArray).Remove(length)); }
public static void Shuffle <T>(this IList <T> list) { list.Shuffle <T>(StaticRandomGenerator.Get()); }
public GeoCoordinate GenerateRandomIn() { return(this.GenerateRandomIn(StaticRandomGenerator.Get())); }