Я пытаюсь реализовать Akka для вычисления числа Монте-Карло, но у меня проблемы с отсутствием соответствующего конструктора. Есть ли способ улучшить основной метод? Я мог бы иметь значение для статических переменных, но я хочу иметь их с вводом. Кроме того, каким образом я могу улучшить это решение, поскольку я также должен реализовать его для Грегори-Лейбница?
package com.actors.montecarlo;
import akka.actor.UntypedActor;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
import akka.actor.Props;
import akka.actor.ActorRef;
public class Main {
static class Data {
public static int ACTOR_COUNT;
public static int DARTS_PER_ACTOR;
// Sum of the Dart Actor results.
public static double sum = 0.0;
// Keep track of actors still going.
public static Integer actorsLeft = ACTOR_COUNT;
}
public class Dart extends UntypedActor {
/**
* Use Monte Carlo integration to approximate pi.
*/
private float approximatePi(int NUMBER_OF_POINTS) {
int inside = 0; // Keep track of points inside the circle.
for (int i = 0; i < NUMBER_OF_POINTS; i++) {
Point p = Point.genRandPoint();
if (p.x * p.x + p.y * p.y <= 1) { // Check if point is inside circle.
inside += 1;
}
}
return 4 * ((float) inside) / NUMBER_OF_POINTS;
}
@Override
public void onReceive(Object msg) {
if (msg != null) {
getSender().tell(approximatePi(Data.DARTS_PER_ACTOR), getSelf());
} else {
getContext().stop(getSelf());
unhandled(msg);
}
}
}
public static class Point {
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Generates a random point in the unit square.
* @return a point in the unit square
*/
static public Point genRandPoint() {
return new Point(
ThreadLocalRandom
.current()
.nextDouble(-1, 1),
ThreadLocalRandom
.current()
.nextDouble(-1, 1)
);
}
}
public class World extends UntypedActor {
/**
* Calculate the "average of the averages".
* Each Dart actor approximates pi. This function takes those results
* and averages them to get a *hopefully* better approximation of pi.
* TODO: There is almost certainly a better reduction method.
*/
public void onReceive(Object msg) {
if (msg != null) {
Data.sum += (Float) msg;
Data.actorsLeft--;
if (Data.actorsLeft == 0) {
System.out.println(Data.sum / Data.ACTOR_COUNT);
getContext().stop(getSelf());
}
} else {
unhandled(msg);
}
}
/**
* Initializes a number of darts and tells the Dart actors
* and tells them to start computing.
*/
@Override
public void preStart() {
for (int i = 0; i < Data.ACTOR_COUNT; i++) {
final ActorRef dart = getContext()
.actorOf(
Props.create(Dart.class),
"dart" + Integer.toString(i));
// The choice of "0" is used, but anything non-null would
// work. (If it were null, the Dart actor would die before
// it did any work.
dart.tell(0, getSelf());
}
}
}
public static void main(String[] args) {
Scanner scannerObj = new Scanner(System.in);
System.out.print("Insert number of actors: ");
Data.ACTOR_COUNT = scannerObj.nextInt();
System.out.println("Nº actors : " + Data.ACTOR_COUNT);
System.out.print("Insert darts per actor: ");
Data.DARTS_PER_ACTOR = scannerObj.nextInt();
System.out.println("Darts: " + Data.DARTS_PER_ACTOR);
/*
long start = System.nanoTime();
akka.Main.main(new String[] { World.class.getName() });
long stop = System.nanoTime();
long timeRes = (stop - start)/1_000_000_000;
long ttTime = (stop - start) / ACTOR_COUNT;
double ttTimeSeconds = ((double) ttTime / 1_000_000_000);*/
akka.Main.main(new String[] { World.class.getName() });
}
}
Это ошибка, которую я получаю
Exception in thread "main" java.lang.IllegalArgumentException: no matching constructor found on class com.actors.montecarlo.Main$World for arguments []
На главной акке
Любая помощь?