Hi to everyone...
I am told to code a Schelling's Segregation Model simulation in C++ . This is about agent simulation and let me explain shortly what it is wanted:

* There is a place like the chessboard (8*8)
* there are 2 kind of people , the X and the O
* place randomly 20 X and 20 O , with no overlapping.
* there is no runaway from the board. (i mean, at the end, still will be 40 people and 24 empty cells.
* Then you should move someone to somewhere randomly, that at least there will be 3 agents with the same type (x or O).
* I mean, a agent will be happy if it has at least 2 neighnours. else, it will change its position till it can found a place with at least 2 other neighbours.
* the program will just run and print to a file.

I coded nearly all the needed things, but confused what to do next when i come to deciding of the moving process.

Now, I look at the [0][0] th cell, if there is an agent there, then I look to its neighbours. just like Linked lists, a data called m_pNext is used in the class of agent. It will mimic like list, and will show its next agent. I set something to next, but i dont know how i will decide this for all of the agents.

plz give me a way about this confusion. Which way can be the sortest?
I run this code in mingw developer studio, known as a g++ compiler.

code is attached, and one result text file.

Thanks

Dani AI

Generated

A practical way to keep your linked-list agent structure (m_pNext) while making moves simple and fast is to use the list for iteration and an auxiliary occupancy array for neighbor checks. That keeps you faithful to the assignment () while avoiding the performance pain of searching the whole list for every neighbor (the point was hinting at). The occupancy array is just a small 8x8 map of pointers or chars that you update whenever an agent moves.

# build occupancy from linked list
for(node = head; node; node = node->m_pNext) board[node.r][node.c] = node

# main loop
repeat until no unhappy agents or max_iters:
  unhappy = []
  for(node = head; node; node = node->m_pNext)
    if(countSameNeighbors(board,node.r,node.c) < 2) {
      unhappy.push_back(node)
      empty_cells.push_back((node.r,node.c))   # mark old spot empty for this round
    }

  shuffle(empty_cells)
  for(node in unhappy)
    target = findFirst(empty_cells, t -> wouldBeHappy(board,t,node.type))
    if(target) {
      board[node.r][node.c] = null
      node.r,node.c = target
      board[target] = node
      remove target from empty_cells
    }

Implementation tips: do moves in this two-phase style (collect unhappy, then move) to avoid order-dependency. Use a shuffled empty_cells vector or sample a fixed number of random empties per agent to avoid O(N^2) behavior. Set sensible caps (max tries per agent, max iterations overall) so the simulation terminates. When using the linked list, only change node position fields and the occupancy array — don’t rewire m_pNext unless you really need to insert/delete nodes.

Common bugs to watch for: off-by-one neighbor loops (stay inside 0..7), forgetting to clear an old cell in the occupancy array, failing to remove a chosen empty from the pool, and RNG not being seeded. Log a few board snapshots (or counts of unhappy agents) each iteration to verify progress.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Looks like the game of life, of some sort. Personally, I'd say you're over complicating this with linked list.

Basic arrays is all you need.

Looks like the game of life, of some sort. Personally, I'd say you're over complicating this with linked list.

Basic arrays is all you need.

sure an array is enouhg but, this is what is wanted.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.