Commit 2bab1201 authored by Lambert Patrick's avatar Lambert Patrick
Browse files

move static method do BinomialForSuperIndividualGen

parent 9ee28bfa
......@@ -19,6 +19,7 @@
*/
package miscellaneous;
import fr.cemagref.simaqualife.pilot.Pilot;
import umontreal.iro.lecuyer.randvar.NormalACRGen;
import umontreal.iro.lecuyer.randvar.NormalGen;
import umontreal.iro.lecuyer.randvar.UniformGen;
......@@ -107,12 +108,44 @@ public class BinomialForSuperIndividualGen {
if (3. * standardDeviation < 1.)
return constantDraw(mean);
else {
else
return normalDraw(draws, mean, standardDeviation);
}
} else {
return binomialDraw(draws,succesProbability );
}
}
public static long getSuccessNumber(Pilot pilot, long amount, double succesProba, long threshold) {
if (amount > threshold) { // use a normal approximation for huge amount
/* double p=-1.;
while (p<0 | p>1){
p = genAleaNormal.nextDouble() *
Math.sqrt(succesProba * (1 - succesProba) /amount) + succesProba;
}
amountWithSuccess = (long) Math.round(p* amount);*/
double amountWithSuccess = -1;
while (amountWithSuccess < 0 | amountWithSuccess > amount) {
amountWithSuccess = Math.round(NormalGen.nextDouble(pilot.getRandomStream(), 0., 1.) * Math.sqrt(succesProba * (1 - succesProba) * amount)
+ succesProba * amount);
}
return (long) amountWithSuccess;
} else {
UniformGen aleaGen = new UniformGen(pilot.getRandomStream(), 0., 1.);
long amountWithSuccess = 0L;
for (long i = 0; i < amount; i++) {
if (aleaGen.nextDouble() < succesProba) {
amountWithSuccess++;
}
}
return amountWithSuccess;
}
}
public static long getSuccessNumber(Pilot pilot, long amount, double succesProba) {
return getSuccessNumber(pilot, amount, succesProba, 50);
}
}
......@@ -6,37 +6,6 @@ import umontreal.iro.lecuyer.randvar.UniformGen;
public class Miscellaneous {
public static long binomialForSuperIndividual(Pilot pilot, long amount, double succesProba, long threshold) {
long amountWithSuccess;
if (amount > threshold) { // use a normal approximation for huge amount
/* double p=-1.;
while (p<0 | p>1){
p = genAleaNormal.nextDouble() *
Math.sqrt(succesProba * (1 - succesProba) /amount) + succesProba;
}
amountWithSuccess = (long) Math.round(p* amount);*/
amountWithSuccess = -1;
while (amountWithSuccess < 0 | amountWithSuccess > amount) {
amountWithSuccess = Math.round(NormalGen.nextDouble(pilot.getRandomStream(), 0., 1.) * Math.sqrt(succesProba * (1 - succesProba) * amount)
+ succesProba * amount);
}
} else {
UniformGen aleaGen = new UniformGen(pilot.getRandomStream(), 0., 1.);
amountWithSuccess = 0;
for (long i = 0; i < amount; i++) {
if (aleaGen.nextDouble() < succesProba) {
amountWithSuccess++;
}
}
}
return amountWithSuccess;
}
public static long binomialForSuperIndividual(Pilot pilot, long amount, double succesProba) {
return binomialForSuperIndividual(pilot, amount, succesProba, 50);
}
static public double temperatureEffect(double T, double Tmin, double Topt, double Tmax) {
if (T <= Tmin || T >= Tmax) {
return 0;
......
......@@ -11,6 +11,7 @@ import environment.RiverBasin;
import environment.Time;
import environment.Time.Season;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import miscellaneous.BinomialForSuperIndividualGen;
import miscellaneous.Miscellaneous;
import org.openide.util.lookup.ServiceProvider;
......@@ -50,7 +51,7 @@ public class DisperseAndMigrateToRiver extends DisperseAndMigrateToRiverBasic {
if (fish.isMature()) {
// fish with homing
amountWithHoming = Miscellaneous.binomialForSuperIndividual(group.getPilot(), fish.getAmount(), pHoming); // seuil par d�faut fix� � 50
amountWithHoming = BinomialForSuperIndividualGen.getSuccessNumber(group.getPilot(), fish.getAmount(), pHoming); // seuil par d�faut fix� � 50
// strayed fish
strayedAmount = fish.getAmount() - amountWithHoming;
......@@ -93,7 +94,7 @@ public class DisperseAndMigrateToRiver extends DisperseAndMigrateToRiverBasic {
// compute sequentially the prob to go into a basin
for (Basin accBasin : accBasOfFish.keySet()){
probToGo = accBasOfFish.get(accBasin) / totalWeight;
amountToGo = Miscellaneous.binomialForSuperIndividual(group.getPilot(), strayedAmount, probToGo);
amountToGo = BinomialForSuperIndividualGen.getSuccessNumber(group.getPilot(), strayedAmount, probToGo);
if (amountToGo > 0){
newFish.add(fish.duplicateWithNewPositionAndAmount(group.getPilot(), bn.getAssociatedRiverBasin(accBasin), amountToGo));
......
......@@ -16,7 +16,7 @@ import environment.Time.Season;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import fr.cemagref.simaqualife.kernel.util.TransientParameters.InitTransientParameters;
import fr.cemagref.simaqualife.pilot.Pilot;
import miscellaneous.BinomialForSuperIndividualGen;
import miscellaneous.Miscellaneous;
import org.openide.util.lookup.ServiceProvider;
......@@ -140,7 +140,7 @@ public class DisperseAndMigrateToRiverStandardization extends AquaNismsGroupProc
if (fish.isMature()) {
// fish with homing
homingAmount = Miscellaneous.binomialForSuperIndividual(group.getPilot(), fish.getAmount(), pHoming); // seuil par d�faut fix� � 50
homingAmount = BinomialForSuperIndividualGen.getSuccessNumber(group.getPilot(), fish.getAmount(), pHoming); // seuil par d�faut fix� � 50
// strayed fish
strayedAmount = fish.getAmount() - homingAmount;
......@@ -177,7 +177,7 @@ public class DisperseAndMigrateToRiverStandardization extends AquaNismsGroupProc
// compute sequentially the prob to go into a basin
for (RiverBasin destination : wForAccessibleBasins.keySet()){
probToGo = wForAccessibleBasins.get(destination) / totalWeight;
amountToGo = Miscellaneous.binomialForSuperIndividual(group.getPilot(), strayedAmount, probToGo);
amountToGo = BinomialForSuperIndividualGen.getSuccessNumber(group.getPilot(), strayedAmount, probToGo);
if (amountToGo > 0){
// add a "duplicated" fish in the destination basin
......
......@@ -8,6 +8,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import miscellaneous.BinomialForSuperIndividualGen;
import miscellaneous.Duo;
import miscellaneous.Miscellaneous;
import species.DiadromousFish.Gender;
......@@ -160,7 +161,7 @@ public class ReproduceAndSurviveAfterReproduction extends AquaNismsGroupProcess<
fish.incNumberOfReproduction();
// survival after reproduction (semelparity or iteroparity) of SI (change the amount of the SI)
survivalAmount = Miscellaneous.binomialForSuperIndividual(group.getPilot(), fish.getAmount(), survivalRateAfterReproduction);
survivalAmount = BinomialForSuperIndividualGen.getSuccessNumber(group.getPilot(), fish.getAmount(), survivalRateAfterReproduction);
if (survivalAmount > 0)
fish.setAmount(survivalAmount);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment