All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.agentsoz.bdimatsim.Utils Maven / Gradle / Ivy

package io.github.agentsoz.bdimatsim;

/*
 * #%L
 * BDI-ABM Integration Package
 * %%
 * Copyright (C) 2014 - 2015 by its authors. See AUTHORS file.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import io.github.agentsoz.bdimatsim.moduleInterface.data.SimpleMessage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Person;
import org.matsim.core.mobsim.framework.MobsimAgent;


/**
 * Extends the original withinday controler listener so that 
 * the updated engine can be initialised here
 * @author QingyuChen
 *
 */
final class Utils {
	private Utils(){} // do not instantiate. kai, mar'15

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(Utils.class);
	
	static List> getBDIAgentIDs( Scenario scenario, MatsimParameterHandler matSimParameterHandler ) {
		// this goes through all matsim agents, ignores the stub agents, and returns as many of those agent ids as the
		// bdi module wants as bdi agents (the remaining agents will, I guess, remain normal matsim agents).  kai, mar'15
		
		ArrayList> agentIDs = new ArrayList>();

		int ii =0;
		ArrayList> agents = new ArrayList>(scenario.getPopulation().getPersons().keySet());
		Collections.sort(agents);
		Id stubId = Id.createPersonId("StubAgent");
		for(Id id:agents)
		{
			if(id.compareTo(stubId) == 0){
				continue;
			}
			agentIDs.add(id);
			ii ++;

			if(ii >= matSimParameterHandler.getNumberOfAgents()){
				break;
			}
		}
		return agentIDs;
	}

	static void initialiseVisualisedAgents(MATSimModel matSimModel){
		Map,? extends Link> links = matSimModel.getScenario().getNetwork().getLinks();
		for(MobsimAgent agent: matSimModel.getMobsimAgentMap().values()){
			SimpleMessage m = new SimpleMessage();
			m.name = "initAgent";
			//m.params = new Object[]{agent.getId().toString(),links.get(agent.getCurrentLinkId()).getFromNode().getId().toString(),(((MATSimReplannableAgent)agent).taxi == true?"T":"N")};
			m.params = new Object[]{//agent.getId().toString(),links.get(agent.getCurrentLinkId()).getFromNode().getId().toString(),"T"};
					agent.getId().toString(),
					links.get(agent.getCurrentLinkId()).getFromNode().getCoord().getX(),
					links.get(agent.getCurrentLinkId()).getFromNode().getCoord().getY()
			};
			matSimModel.addExternalEvent("initAgent",m);
		}
		//interfaceV.sendInitialAgentData(new ArrayList());
	}

	final static double[]  computeBoundingBox(Collection links) {
		double[] boundingBox0 = {Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY };
		for ( Link link : links ) {
			final double xx = link.getCoord().getX();
			final double yy = link.getCoord().getY();
	
			boundingBox0[0] = Math.min( boundingBox0[0],  xx ) ; 
			boundingBox0[1] = Math.min( boundingBox0[1],  yy ) ; 
			boundingBox0[2] = Math.max( boundingBox0[2],  xx ) ; 
			boundingBox0[3] = Math.max( boundingBox0[3],  yy ) ; 
		}
		return boundingBox0 ;
	}

	final static double[] generateLinkCoordsAsArray(Collection links) {
		double[] array = new double[links.size()*2];
		int ii = -1 ;
		for ( Link link : links ) {
			ii++ ;
	
			final double xx = link.getCoord().getX();
			final double yy = link.getCoord().getY();
	
			array[ii*2] = xx ;
			array[ii*2+1] = yy;
		}
		return array ;
	}

	final static String[] createFlatLinkIDs(Collection> allLinkIDs) {
		String[] flatLinkIDList = new String[allLinkIDs.size()];
		int j = 0;
		for(Id i :allLinkIDs){
			flatLinkIDList[j] = i.toString();
			j ++;
		}
		return flatLinkIDList;
	}

	 final static String[] getPersonIDsAsArray( List> personIds ){
		List> list = personIds ;
		String[] agentIDArray = new String[list.size()];
		int j = 0;
		for(Id id: list){
			agentIDArray[j] = id.toString();
			j ++;
		}
		return agentIDArray;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy