-1

我目前正在研究吸引力代码,我的概念是将单元格分配给人口、绿色空间和住宅空间等因素。我想编写这些因素之间的吸引力关系,其中,人口被绿色空间所吸引,住宅空间被绿色空间所吸引。我有几个 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;
4

2 回答 2

0

正如彼得所说,您正在调用Min()一个Max()空集合。

解决方法是:

double minPopAtt = attractPop.DefaultIfEmpty(-1).Min();

-1 将是您的默认值,以防您的收藏为空。

于 2017-12-03T15:03:39.447 回答
0

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

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

于 2017-12-03T14:44:24.027 回答