An error occurred while loading the file. Please try again.
-
Lambert Patrick authored770d96e0
package species;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.Basin;
import environment.BasinNetwork;
import environment.BasinNetworkReal;
import environment.RiverBasin;
import fr.cemagref.observation.kernel.Observable;
import fr.cemagref.simaqualife.kernel.AquaNismsGroup;
import fr.cemagref.simaqualife.kernel.Processes;
import fr.cemagref.simaqualife.pilot.Pilot;
import java.awt.Color;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import miscellaneous.Duo;
import miscellaneous.TreeMapForCentile;
import org.openide.util.lookup.ServiceProvider;
@ServiceProvider(service = AquaNismsGroup.class)
public class DiadromousFishGroup extends AquaNismsGroup< DiadromousFish, BasinNetwork> implements Comparable<DiadromousFishGroup> {
public String name = "species A";
public Color color = Color.RED;
public double linfVonBert = 60.;
public double dMaxDisp = 300.;
public double lFirstMaturity = 40.;
public String fileNameInputForInitialObservation = "data/input/reality/Obs1900.csv";
public double centileForRange = 0.95;
private String parameterSetfileName= "data/input/reality/parameterSet.csv";
private int parameterSetLine =0;
private long yearOfTheUpdate;
private String basinsToUpdateFile = "data/input/reality/basinsToUpdate.csv";
private String outputPath = "data/output/";
private transient Map<String, Duo<Double, Double>> basinsToUpdate;
private transient double kOpt; //parametre de croissance
private transient double tempMinRep; //parametre de reproduction
private transient List<Duo<Double, Double>> parameterSets;
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new DiadromousFishGroup(new Pilot(), null, null)));
}
public DiadromousFishGroup(Pilot pilot, BasinNetwork environment, Processes processes) {
super(pilot, environment, processes);
}
public double getPattractive(String basinName){
if (basinsToUpdate.containsKey(basinName.substring(0, basinName.length()-2)))
return basinsToUpdate.get(basinName.substring(0, basinName.length()-2)).getFirst();
else
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return Double.NaN;
}
public double getPaccessible(String basinName){
if (basinsToUpdate.containsKey(basinName))
return basinsToUpdate.get(basinName).getSecond();
else
return Double.NaN;
}
/**
* @return the yearOfTheUpdate
*/
public long getYearOfTheUpdate() {
return yearOfTheUpdate;
}
/* (non-Javadoc)
* @see fr.cemagref.simaqualife.kernel.AquaNismsGroup#initTransientParameters(fr.cemagref.simaqualife.pilot.Pilot)
*/
@Override
public void initTransientParameters(Pilot pilot)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
super.initTransientParameters(pilot);
if ( basinsToUpdate != null){
String subDir=basinsToUpdateFile;
if (basinsToUpdateFile.lastIndexOf("/")!=-1)
subDir=basinsToUpdateFile.substring(basinsToUpdateFile.lastIndexOf("/")+1,
basinsToUpdateFile.length());
if (subDir.lastIndexOf(".")!=-1)
subDir=subDir.substring(0, subDir.lastIndexOf("."));
outputPath= outputPath.concat(subDir).concat("/");
System.out.println(outputPath);
basinsToUpdate = new HashMap<String, Duo<Double, Double>>();
FileReader reader;
Scanner scanner;
String basins;
double pAttractive;
double pAccessible;
try {
// open the file
reader = new FileReader(basinsToUpdateFile);
// Parsing the file
scanner = new Scanner(reader);
scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!!
scanner.useDelimiter(Pattern.compile("[;\r]"));
scanner.nextLine();
while (scanner.hasNext()) {
basins = scanner.next();
if (basins!= null) {
pAttractive = scanner.nextDouble();
pAccessible = scanner.nextDouble();
scanner.nextLine();
Duo<Double, Double> duo=new Duo<Double, Double>(pAttractive, pAccessible);
basinsToUpdate.put(basins, duo);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// charge kopt et temMinRep depuis le fichier de parametre. Sinon (parameterSetLine<=0), ce sont les
// valeur dasn le procoessus de reroduction qui sont utilis�
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
kOpt=Double.NaN;
tempMinRep =Double.NaN;
if (parameterSetLine>0){
parameterSets = new ArrayList<Duo<Double,Double>>(10);
// open the file
FileReader reader1;
Scanner scanner1;
try {
reader1 = new FileReader(parameterSetfileName);
// Parsing the file
scanner1 = new Scanner(reader1);
scanner1.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!!
scanner1.useDelimiter(Pattern.compile("[;\r]"));
scanner1.nextLine(); // skip the first line
while (scanner1.hasNext()) {
String rien= scanner1.next(); // skip id
//System.out.println(rien.compareTo("\n"));
if(rien.compareTo("\n")!=0){
Duo<Double, Double> duo=new Duo<Double, Double>(scanner1.nextDouble(), scanner1.nextDouble());
//System.out.println(duo.toString());
parameterSets.add(duo);
}
}
scanner1.close();
reader1.close();
} catch (Exception e) {
e.printStackTrace();
}
kOpt = parameterSets.get(parameterSetLine-1).getFirst();
tempMinRep = parameterSets.get(parameterSetLine-1).getSecond();
}
}
public double getKOpt(){
return kOpt;
}
public double getTempMinRep(){
return tempMinRep;
}
@Observable(description = "Nb of SI")
public int getNbSI() {
return this.getAquaNismsList().size();
}
@Observable(description = "Sizes mean of SI")
public double getSizesMeanOfSI() {
double sum = 0;
for (DiadromousFish list : getAquaNismsList()) {
sum += (int) list.getAmount();
}
return sum / getAquaNismsList().size();
}
@Observable(description = "# of SI with ind < 10")
public double getNbLittleSI() {
double nb = 0;
for (DiadromousFish list : getAquaNismsList()) {
if (list.getAmount() < 10) {
nb++;
}
}
return nb;
}
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
public double getMeanLengthOfMatureFish(){
double meanLengthOfMatureFish = 0.;
double sumOfLength = 0.;
double numberOfMatureFish = 0.;
for (DiadromousFish fish : getAquaNismsList()){
if (fish.isMature()){
sumOfLength += fish.getAmount() * fish.getLength();
numberOfMatureFish += fish.getAmount();
}
meanLengthOfMatureFish = sumOfLength / numberOfMatureFish;
}
return meanLengthOfMatureFish;
}
public double getStandardDeviationOfMatureFishLength(){
double standardDeviationOfMatureFishLength = 0.;
double sumOfSquareLength = 0.;
double numberOfMatureFish = 0.;
double meanOfSquareLengthOfMatureFish = 0.;
for (DiadromousFish fish : getAquaNismsList()){
if (fish.isMature()){
sumOfSquareLength += fish.getAmount() * Math.pow(fish.getLength(), 2);
numberOfMatureFish += fish.getAmount();
}
meanOfSquareLengthOfMatureFish = sumOfSquareLength / numberOfMatureFish;
standardDeviationOfMatureFishLength = Math.pow((meanOfSquareLengthOfMatureFish - Math.pow(getMeanLengthOfMatureFish(), 2)), 0.5);
}
return standardDeviationOfMatureFishLength;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
public double getLinfVonBert() {
return linfVonBert;
}
public void setLinfVonBert(double linfVonBert) {
this.linfVonBert = linfVonBert;
}
public double getdMaxDisp() {
return dMaxDisp;
}
public double getlFirstMaturity() {
return lFirstMaturity;
}
public void setlFirstMaturity(double lFirstMaturity) {
this.lFirstMaturity = lFirstMaturity;
}
@Observable(description="Higher Populated Latitude")
public double getHigherPopulatedLatitude() {
double latitude = 0.0;
RiverBasin[] basins = getEnvironment().getRiverBasins();
int[] finalStates = getEnvironment().getFinalStates();
for (int i = 0; i < finalStates.length; i++) {
if ((finalStates[i] == 1) && (basins[i].getLatitude() > latitude)) {
latitude = basins[i].getLatitude();
}
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
}
return latitude;
}
@Observable(description="Spawners For First Time Summary Statistic")
public double computeSpawnerForFirstTimeSummaryStatistic() {
double sum = 0;
double TARGET = 5.0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges().getMeanWithoutZero() > 0.)
sum += Math.pow(riverBasin.getSpawnersForFirstTimeMeanAges().getMeanWithoutZero() - TARGET, 2);
}
return sum;
}
@Observable(description = "Likelihood Summary stat")
public double computeLikelihood() throws IOException {
// 1 : read input file of observation
FileReader reader;
Scanner scanner;
Map<String, Integer> obs1900 = new HashMap<String, Integer>();
try {
reader = new FileReader(fileNameInputForInitialObservation);
// Parsing the file
scanner = new Scanner(reader);
scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!!
scanner.useDelimiter(Pattern.compile("[;\r]"));
scanner.nextLine(); // to skip the file first line of entete
while (scanner.hasNext()) {
obs1900.put(scanner.next().replaceAll("\n", ""), scanner.nextInt());
}
reader.close();
scanner.close();
} catch (IOException ex) {
Logger.getLogger(DiadromousFishGroup.class.getName()).log(Level.SEVERE, null, ex);
}
int obsVal;
double sumLogWherePres = 0.;
double sumLogWhereAbs = 0.;
final double[] probOfNonNulRecruitmentDuringLastYears = getEnvironment().getProbOfNonNulRecruitmentDuringLastYears();
final String[] finalStatesNames = getEnvironment().getRiverBasinNames();
for (int i = 0; i < finalStatesNames.length; i++) {
if (obs1900.containsKey(finalStatesNames[i])) {
obsVal = obs1900.get(finalStatesNames[i]);
if (obsVal == 0) {
sumLogWhereAbs += Math.log(1 - probOfNonNulRecruitmentDuringLastYears[i]);
} else {
sumLogWherePres += Math.log(probOfNonNulRecruitmentDuringLastYears[i]);
}
}
}
return sumLogWhereAbs + sumLogWherePres;
}
@Observable(description="Number of colonized basins")
public double getNbColonizedBasins() {
int nb = 0;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
nb++;
}
}
}
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
return nb;
}
@Observable(description="Northern colonized basins")
public double getNorthernBasins() {
int northernBasin = Integer.MAX_VALUE;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
northernBasin = Math.min(northernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId());
}
}
}
return northernBasin;
}
@Observable(description="Southern colonized basins")
public double getSouthernBasins() {
int southernBasin = Integer.MIN_VALUE;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
southernBasin = Math.max(southernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId());
}
}
}
return southernBasin;
}
@Observable(description = "Range distribution with latitude")
public Double[] getRangeDistributionWithLat() {
//TODO keep the extreme latitudes from the catchment
double southernBasin = 35.;
double northernBasin = 60.;
RiverBasin riverBasin;
TreeMapForCentile latitudeEffective = new TreeMapForCentile();
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
riverBasin = (RiverBasin) getEnvironment().getAssociatedRiverBasin(seaBasin);
long effective = 0;
for (DiadromousFish fish : seaBasin.getFishs(this)){
effective += fish.getAmount();
}
southernBasin = Math.max(southernBasin, riverBasin.getLatitude());
latitudeEffective.putWithAdding(riverBasin.getLatitude(), effective);
northernBasin = Math.min(northernBasin, riverBasin.getLatitude());
}
}
}
Double[] rangeDistribution = new Double[4];
rangeDistribution[0]= (latitudeEffective.isEmpty() ? (southernBasin +northernBasin)/2. :
latitudeEffective.calculateMedian());
rangeDistribution[1]= Math.min(southernBasin,northernBasin);
rangeDistribution[2]= Math.max(southernBasin,northernBasin);
rangeDistribution[3]= latitudeEffective.calculateCentile(centileForRange);
return rangeDistribution;
}
@Observable(description = "Range distribution")
public Double[] getRangeDistribution() {
double southernBasin = 0;
double nbBasin = getEnvironment().getNbBasin();
double northernBasin = nbBasin;
Basin riverBasin;
TreeMapForCentile latitudeEffective = new TreeMapForCentile();
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
riverBasin = getEnvironment().getAssociatedRiverBasin(seaBasin);
long effective = 0;
for (DiadromousFish fish : seaBasin.getFishs(this)){
effective += fish.getAmount();
}
latitudeEffective.putWithAdding(riverBasin.getId(), effective);
southernBasin = Math.max(southernBasin, riverBasin.getId());
northernBasin = Math.min(northernBasin, riverBasin.getId());
}
}
}
southernBasin = nbBasin - southernBasin;
northernBasin = nbBasin - northernBasin;
Double[] rangeDistribution = new Double[3];
rangeDistribution[0]= (latitudeEffective.isEmpty() ? (southernBasin +northernBasin)/2. :
nbBasin - latitudeEffective.calculateMedian());
rangeDistribution[1]= Math.min(southernBasin,northernBasin);
rangeDistribution[2]= Math.max(southernBasin,northernBasin);
return rangeDistribution;
}
@Observable(description = "Number of fishes")
public double getFishEffective() {
long eff = 0;
for (DiadromousFish fish : this.getAquaNismsList()) {
eff += fish.getAmount();
}
return eff;
}
@Override
public void addAquaNism(DiadromousFish fish) {
// avoid utilisation of global fishes list
//super.addAquaNism(fish);
fish.getPosition().addFish(fish, this);
}
@Override
public void removeAquaNism(DiadromousFish fish) {
// avoid utilisation of global fishes list
//super.removeAquaNism(fish);
fish.getPosition().removeFish(fish, this);
}
@Override
public int compareTo(DiadromousFishGroup t) {
return name.compareTo(t.name);
}
public String getSimulationId(){
String id="_";
id=id.concat(Integer.toString(getPilot().getParameters().getRngStatusIndex()));
String temperatureFile = ((BasinNetworkReal) getPilot().getAquaticWorld().getEnvironment()).getTemperatureCatchmentFile();
id=id.concat("-").concat(temperatureFile.substring(temperatureFile.length()-9, temperatureFile.length()-4));
if (parameterSetLine>0){
id=id.concat("-").concat(Integer.toString(parameterSetLine));
}
return id ;
}
public boolean isThereBasinToUpdate(){
return basinsToUpdate != null;
}
/**
* @return the outputPath
*/
public String getOutputPath() {
return outputPath;
491492493494495496
}
}