Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create example with loading agents from CSV in a demo + documentation #90

Open
etatara opened this issue Mar 26, 2024 · 3 comments
Open

Comments

@etatara
Copy link
Member

etatara commented Mar 26, 2024

Using OpenCSV (https://javadoc.io/doc/au.com.bytecode/opencsv/latest/index.html) update one of the existing demo models to show how to load agents from a CSV file. Predator-Prey might be a good candidate. Should also provide a brief section in the Reference guide.

@etatara etatara added this to the v.2.11.0 milestone Mar 26, 2024
@etatara etatara self-assigned this Mar 26, 2024
@jozik
Copy link
Member

jozik commented Mar 26, 2024

For responding to the Repast interest mailing list:
Can read in a CSV like so (from here):

List<String[]> rows = new CSVReader(
				new InputStreamReader(new FileInputStream(fileName)))
				.readAll()

Replace the Humans agent creation in the Java Zombies model with this.

For the actual example we put in a new demo model, we'd want more attributes.

@etatara
Copy link
Member Author

etatara commented Mar 26, 2024

     String fileName = "data/initial_humans.csv";
		
		List<String[]> rows = null;

		try {
			Reader reader = new InputStreamReader(new FileInputStream(fileName));
			CSVReader csvReader = new CSVReader(reader, CSVParser.DEFAULT_SEPARATOR,
		               CSVParser.DEFAULT_QUOTE_CHARACTER, 1);
			rows = csvReader.readAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		for (String[] row : rows) {
			int id = Integer.parseInt(row[0]);
			int energy = Integer.parseInt(row[1]);			
			Human h = new Human(space, grid, energy);
			context.add(h);
		}

@etatara
Copy link
Member Author

etatara commented Mar 26, 2024

initial_humans.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants