模拟netlogo中的结婚

时间:2018-03-12 14:20:54

标签: netlogo

在netlogo中,我正在模拟人口,我希望16到50岁之间的人与人口中的另一个人随机结婚。每个人都有一个家庭身份,我希望男性个人将其家庭身份改变为他的妻子"家庭身份,但我不知道该怎么做。现在我有了这段代码

ask individuals [
  if not married? and sex = "male" and age >= 16 and age <= 50 [ 
    let potential-mate one-of individuals with [
      not married? and age >= 16 and age <= 50
      and sex = "female" and household-id != household-id
    ]
    if potential-mate != nobody [
      ; this command do an Bernoulli equation,
      ; the relation is based on empirical data i have
      ifelse random-bernoulli (- 0.765 * ln age + 2.9753) [
        stop
      ] [
        set my-mate potential-mate            
        set married? true            
        ask my-mate [ set married? true ]            
        ask my-mate [ set my-mate myself ]
      ]
    ]
  ]
]

1 个答案:

答案 0 :(得分:4)

Luke C的评论是正确的:你需要的是myself,如:

  //shuffle the arrays
  arrMatch1 = shuffle(match1);
  arrMatch2 = shuffle(match2);

  //insert them into DOM
  $('#source').html(match1.join(''));
  $('#target').html(match2.join(''));
}




function shuffle(v) {
  for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);

  return v;
}




function initQuiz() {
  $('#source li').draggable(
  {
    revert: true,
    revertDuration: 600,
    cursor: "all-scroll"
  });

 var    totalScore  =   0;
$('#score').text(totalScore +   '   correct answer');
$('#target  li').droppable(
{
        accept  :   function(draggable)
        {
                if(parseInt(draggable.data('index'),    10) === 
parseInt($(this).data('index'), 10))
                {
                        return  true;
                }
                else
                {
                        return  false;
                }
        },
        drop:   function(   event,  ui  )   
        {
                var that    =   $(this);
                that.addClass(  "ui-state-highlight").html('Correct!');
                that.droppable('disable');
                ui.draggable.addClass('correct  ui-state-active');
                (ui.draggable).draggable('disable');
                totalScore++;
                $('#score').text(totalScore +   '   matching answer');
                if($('li.correct').length   ==  10)
                {
                        $(  "#dialog-complete"  ).dialog({
                                resizable:  false,
                                modal:  true
                        });
                }
        }
});

}

话虽如此,我强烈建议将诸如婚姻之类的东西建模为links。这是一个有效的例子:

household-id != [ household-id ] of myself

这样,您就不必为breed [individuals individual] individuals-own [age sex household-id] undirected-link-breed [marriages marriage] to setup clear-all create-individuals 100 [ set age random 100 set sex one-of ["male" "female"] set household-id random 100 setxy random-xcor random-ycor ] marry-individuals reset-ticks end to marry-individuals let bachelors individuals with [ not married? and age >= 16 and age <= 50 ] ask bachelors with [ sex = "male" ] [ let potential-mates bachelors with [ sex = "female" and household-id != [ household-id ] of myself ] if any? potential-mates [ if not random-bernoulli (- 0.765 * ln age + 2.9753) [ let mate one-of potential-mates create-marriage-with mate set household-id [ household-id ] of mate ] ] ] end to-report married? ; individual reporter report any? my-marriages end to-report my-mate ; individual reporter report [ other-end ] of one-of my-marriages end married?管理单独的变量:单个链接告诉您需要了解这两个人之间的关系。它的主要优点是更不容易出错:这些变量的值不会变得不一致。另请注意,my-matemarried?如何使这些概念像以前一样易于访问。

关于您的代码的另外几条评论:

  • 如果可能,我通常会避免使用stop。该原语的行为并不总是直观的,有时会导致错误。

  • 请注意我是如何创建临时my-mate代理集的。这样可以避免两次检查bachelorsage条件并使代码更具可读性。

  • 我不知道您打算如何处理这些问题,但您可能需要考虑制作married?代理商,并通过创建指向该家庭的链接来代表其家庭成员资格。使用“id”数字并不是一种非常网络化的做事方式,有时会导致代码效率低下。