如何修复序列在C#上没有包含元素错误?

时间:2017-12-03 14:37:19

标签: c# rhino grasshopper

我目前正在制作一个吸引力的代码,我的概念是将细胞分配给人口,绿色空间和居住空间等因素。我想编码这些因素之间的吸引力关系,其中人口被绿色空间和居住空间吸引到绿色空间。我有几个C#组件,但由于此序列包含无元素错误,其中一些组件无法正常工作。我似乎无法在代码中找到错误,如果有人知道如何解决这个问题,我将不胜感激。谢谢你的帮助!

//======================================================
// ------- compute the attractivity landscape ----------
//======================================================
// -- find the max distance value - usually the diagonal from first to last:
int nrLocations = Distances.BranchCount;
int nrDists = Distances.Branch(0).Count;
double maxDist = Distances.Branch(0)[nrDists - 1];

// -- the list store the attractivity values for population to green spaces and housing to green spaces
List<double> attractPop = new List<double>();
List<double> attractSpaces = new List<double>();
List<double> attractResid = new List<double>();
double maxPop = PopulDistrib.Max();
double maxSpaces = GreenSDistrib.Max();
double maxResid = ResiDistrib.Max();

for(int i = 0; i < nrLocations; i++)
{
  // -- reset lists in each loop:
  double curPopAttract = 0;
  double curSpacesAttract = 0;
  double curResidAttract = 0;
  for(int k = 0; k < nrDists; k++)
  {
    double curPop = PopulDistrib[k] / maxPop; // population of all cells
    double curSpaces = GreenSDistrib[k] / maxPop; // Green Spaces of all cells
    double curResid = ResiDistrib[k] / maxResid; // Residential Spaces of all cells

    // -- distances to all cells:
    // -- index i = from current cell // index k = to other cells
    // -- division by maxDist gives normalized values in the range [0; 1] for the distance
    double curDist = Distances.Branch(i)[k] / maxDist;

    // ** Remember: the distances are normalized! Therefore:
    // ** (1 - distance) :: makes close things more important than distant ones.
    // ** (Math.Pow(1-distance, 6); :: decrease the effect of the distance exponentially.
    // ** The perception of people concerning things in a certain distance can now be expressed by:
    double distPerceptPop = Math.Pow(1 - curDist, 7);
    //Print("distPercept: " + distPerceptPop.ToString());

    // -- in addition, we just cut the relevance of distant things completely:
    if (distPerceptPop < 0.4) distPerceptPop = 0;

    // -- corresponding "perception" of Green Spaces concerning things in a distance:
    double distPerceptSpaces = Math.Pow(1 - curDist, 2);

    // -- corresponding "perception" of Residential Space concerning things in a distance:
    double distPerceptResid = Math.Pow(1 - curDist, 4);

    // ----------- Attractivity Functions ----------------------
    // -- define the functions for the attractivity ------------
    // -- here we control, what is prefered by which land use --
    // -- linear attractivity function -> can become more complex functions!!!

    // -- people like to be close to Green Spaces:
    curPopAttract += ((distPerceptPop) * curSpaces );
    //double curAttract = ((distPerceptPop) * curPop );
    //curPopAttract += curAttract;
    //Print("attractivityPop: " + curPopAttract.ToString());

    // -- people DON'T like to be close to other people:
    //curPopAttract += 1- ((distPerceptPop) * curPop );

    // -- people DON'T like to be close to other people, + but to workplaces:
    //double weightImportWork = 0.3; // -- weighting factor to express the importancy to be close to workplaces in contrast to be away from other people
    //curPopAttract += 1- ((distPerceptPop) * curPop )     + weightImportWork * Math.Pow(1 - curDist, 2) * curWork;

    // -- Residential Spaces like to be close to Green Spaces
    curResidAttract += (distPerceptResid) * curSpaces;

    // *** EXPERIMENT WITH THE ATTRACTIVITY FUNCTIONS ABOVE !!! **********************************
    // *** You may also add additional relations similar to the other system dynamics examples ***
  }
  // -- collect the current values in the attractivity values lists:
  attractPop.Add(curPopAttract);
  attractSpaces.Add(curSpacesAttract);
  attractResid.Add(curResidAttract);
}

//=======================================================
// -- normalize the attractivity values for both lists --
double minPopAtt = attractPop.Min();
double maxPopAtt = attractPop.Max();
double minSpacesAtt = attractSpaces.Min();
double maxSpacesAtt = attractSpaces.Max();
double minResidAtt = attractResid.Min();
double maxResidAtt = attractResid.Max();

// -- formula for normalization to the range [0; 1]:
// -- normalized valueOfList = (valueOfList - minValueOfList) * 1 / maxValueOfList
for(int i = 0; i < attractPop.Count; i++)
{
  attractPop[i] = (attractPop[i] - minPopAtt) * (1 / maxPopAtt);
  attractSpaces[i] = (attractSpaces[i] - minSpacesAtt) * (1 / maxSpacesAtt);
  attractResid[i] = (attractResid[i] - minResidAtt) * (1 / maxResidAtt);
}

//=======================================================
// -- return the lists:
AttractPop = attractPop;
AttractSpaces = attractSpaces;
AttractResid = attractResid;

2 个答案:

答案 0 :(得分:0)

您正在空集合上调用.Max().Min()

检查空集合,例如使用if (!collection.Any()) ...,然后中止(如果这是一个输入错误),或调整算法以解释它。

答案 1 :(得分:0)

正如彼得所说,你在空集合上呼叫Min()Max()

解决方法是:

double minPopAtt = attractPop.DefaultIfEmpty(-1).Min();
如果您的收藏是空的,

-1将是您的默认值。