protected override void Harvest()
 {
     log.WriteLine("Run Mode1 harvesting ...");
     ClearHarvested();
     GenerateNewPrescriptions(sosielData);
     foreach (var agent in Agents)
     {
         var areas = sheParameters.AgentToManagementAreaList
                     .First(map => map.Agent.Equals(agent.Id)).ManagementAreas
                     .Select(ma => Areas.First(area => area.Key.Equals(ma)).Value);
         foreach (var area in areas)
         {
             var key = HarvestResults.GetKey(ModeId, agent, area);
             //Debugger.Launch();
             if (sosielData.SelectedDecisions.ContainsKey(key))
             {
                 var selectedPrescriptionNames = sosielData.SelectedDecisions[key];
                 log.WriteLine($"\t\t{key}: decisions: {string.Join(",", selectedPrescriptionNames)}");
                 var selectedPrescriptions = _prescriptions.Where(
                     p => selectedPrescriptionNames.Contains(p.Name));
                 var harvestManager =
                     new HarvestManager(area, selectedPrescriptions, _harvestPrescriptionName, _siteCohorts);
                 _harvested[key] = harvestManager.Harvest();
                 log.WriteLine($"\t\t{key}: harvested {harvestManager.HarvestedSitesNumber} sites");
             }
             else
             {
                 log.WriteLine($"\t\t{key}: harvested 0 sites (no decision found)");
             }
         }
     }
     log.WriteLine("Mode 1: Harvesting finished.");
 }
        protected override HarvestResults AnalyzeHarvestingResult()
        {
            var results = new HarvestResults();

            foreach (var agent in Agents)
            {
                var areas = sheParameters.AgentToManagementAreaList
                            .First(map => map.Agent == agent.Id).ManagementAreas
                            .Select(ma => Areas.First(area => area.Key == ma).Value);
                foreach (var area in areas)
                {
                    var key = HarvestResults.GetKey(1, agent, area);
                    results.ManagementAreaBiomass[key]         = 0;
                    results.ManagementAreaMaturityPercent[key] = 0;

                    double manageAreaMaturityProportion = 0;
                    foreach (var stand in area.ManagementArea)
                    {
                        double standMaturityProportion = 0;
                        foreach (var site in stand)
                        {
                            double siteBiomass  = 0;
                            double siteMaturity = 0;

                            foreach (var species in PlugIn.ModelCore.Species)
                            {
                                var cohorts = _siteCohorts[site][species];
                                if (cohorts == null)
                                {
                                    continue;
                                }
                                double siteSpeciesMaturity = 0;
                                foreach (var cohort in cohorts)
                                {
                                    siteBiomass += cohort.Biomass;
                                    if (cohort.Age >= PlugIn.ModelCore.Species[species.Name].Maturity)
                                    {
                                        siteSpeciesMaturity += cohort.Biomass;
                                    }
                                }
                                siteMaturity += siteSpeciesMaturity;
                            }

                            var siteMaturityProportion = Math.Abs(siteBiomass) < 0.0001
                                ? 0 : (siteMaturity / siteBiomass) * 2;
                            results.ManagementAreaBiomass[key] += siteBiomass;
                        }
                        standMaturityProportion      /= stand.Count();
                        manageAreaMaturityProportion += standMaturityProportion;
                    }

                    manageAreaMaturityProportion              /= area.ManagementArea.StandCount;
                    results.ManagementAreaBiomass[key]         = results.ManagementAreaBiomass[key] / 100 * PlugIn.ModelCore.CellArea;
                    results.ManagementAreaHarvested[key]       = _harvested[key] * PlugIn.ModelCore.CellArea;
                    results.ManagementAreaMaturityPercent[key] = 100 * manageAreaMaturityProportion;
                }
            }
            return(results);
        }
        protected override HarvestResults AnalyzeHarvestingResult()
        {
            var results = new HarvestResults();

            foreach (var managementArea in Areas.Values.Select(a => a.ManagementArea))
            {
                var key = managementArea.MapCode.ToString();
                results.ManagementAreaBiomass[key]         = 0.0;
                results.ManagementAreaHarvested[key]       = 0.0;
                results.ManagementAreaMaturityPercent[key] = 0.0;

                double managementAreaMaturityProportion = 0.0;
                foreach (var stand in managementArea)
                {
                    double standMaturityProportion = 0.0;
                    foreach (var site in stand)
                    {
                        double siteBiomass  = 0;
                        double siteMaturity = 0;
                        foreach (var species in PlugIn.ModelCore.Species)
                        {
                            var cohorts = BiomassHarvest.SiteVars.Cohorts[site][species];
                            if (cohorts != null)
                            {
                                double siteSpeciesMaturity = 0.0;
                                foreach (var cohort in cohorts)
                                {
                                    siteBiomass += cohort.Biomass;
                                    if (cohort.Age >= PlugIn.ModelCore.Species[species.Name].Maturity)
                                    {
                                        siteSpeciesMaturity += cohort.Biomass;
                                    }
                                }
                                siteMaturity += siteSpeciesMaturity;
                            }
                        }
                        var siteMaturityProportion = Math.Abs(siteBiomass) < kEpsilon
                            ? 0.0 : (siteMaturity / siteBiomass);
                        standMaturityProportion              += siteMaturityProportion;
                        results.ManagementAreaBiomass[key]   += siteBiomass;
                        results.ManagementAreaHarvested[key] += BiomassHarvest.SiteVars.BiomassRemoved[site];
                    }
                    managementAreaMaturityProportion += standMaturityProportion / stand.Count();
                }

                managementAreaMaturityProportion  /= managementArea.StandCount;
                results.ManagementAreaBiomass[key] =
                    (results.ManagementAreaBiomass[key] / 100) * PlugIn.ModelCore.CellArea;
                results.ManagementAreaHarvested[key] =
                    (results.ManagementAreaHarvested[key] / 100) * PlugIn.ModelCore.CellArea;
                results.ManagementAreaMaturityPercent[key] = 100 * managementAreaMaturityProportion;
            }
            return(results);
        }
 private void ClearHarvested()
 {
     foreach (var agent in Agents)
     {
         PlugIn.ModelCore.UI.WriteLine($"ClearHarvested: agent {agent.Id}");
         var areas = sheParameters.AgentToManagementAreaList
                     .First(map => map.Agent.Equals(agent.Id))
                     .ManagementAreas.Select(ma => Areas.First(area => area.Key.Equals(ma)).Value);
         foreach (var area in areas)
         {
             var key = HarvestResults.GetKey(1, agent, area);
             _harvested[key] = 0.0;
         }
     }
 }
        protected override HarvestResults AnalyzeHarvestingResult()
        {
            // Report all zeros
            var results = new HarvestResults();

            foreach (var agent in Agents)
            {
                var areas = sheParameters.AgentToManagementAreaList
                            .First(map => map.Agent == agent.Id).ManagementAreas
                            .Select(ma => Areas.First(area => area.Key == ma).Value);
                foreach (var area in areas)
                {
                    var key = HarvestResults.GetKey(ModeId, agent, area);
                    results.ManagementAreaBiomass[key]         = 0;
                    results.ManagementAreaMaturityPercent[key] = 0;
                    results.ManagementAreaBiomass[key]         = 0;
                    results.ManagementAreaHarvested[key]       = 0;
                    results.ManagementAreaMaturityPercent[key] = 0;
                }
            }
            return(results);
        }