Repast / repast.simphony

Git repository for Repast Simphony development
repast.github.io
90 stars 21 forks source link

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

Open etatara opened 5 months ago

etatara commented 5 months ago

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.

jozik commented 5 months ago

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 commented 5 months ago
     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 commented 5 months ago

initial_humans.csv