Skip to content
Snippets Groups Projects
MainAmakaque.java 3.70 KiB
package amakaque;

import java.util.ArrayList;
import java.util.List;

import eval.Eval;
import eval.fun.Rastrigin;
import eval.fun.SchwefelFunction;
import eval.fun.SchwefelFunction1_2;
import eval.fun.StepFunction;
import mas.core.Schedulable;

public class MainAmakaque {

	public static void main(String[] args) {
		// [PARAM]
		
		int nbrAgent = 50;
		
		// [0.1 : 0.9]
		double pr = 0.1;
		
		//
		int maxGroup = 5;
		
		int localLimit = 1500;
		int globalLimit = 50;
	
		int res = 0;
		int nbrTry = 100;
		
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < nbrTry; i++) {
			res += findSolution(nbrAgent, pr, maxGroup, localLimit, globalLimit);
			// System.out.println("run : "+i);
		}
		long estimatedTime = System.currentTimeMillis() - startTime;
		
		System.out.println("Time : "+estimatedTime/1000+ "s "+estimatedTime%1000+"ms");
		// System.out.println(res);
		System.out.println("Average cycle : "+(double)res/(double)nbrTry);
		


		// ******************** SCHEDULER ************************
		/*
		res = 0;
		startTime = System.currentTimeMillis();
		for (int i = 0; i < nbrTry; i++) {
			res += findSolution2(nbrAgent, pr, maxGroup, localLimit, globalLimit);
			// System.out.println("run : "+i);
		}
		estimatedTime = System.currentTimeMillis() - startTime;
		
		System.out.println("Time : "+estimatedTime/1000+ "s "+estimatedTime%1000+"ms");
		// System.out.println(res);
		System.out.println("Average cycle : "+(double)res/(double)nbrTry);
		*/
	}
	
	private static int findSolution(
			int nbrAgent,
			double pr,
			int maxGroup,
			int localLimit,
			int globalLimit
			) {
		
		// [INIT]
		Eval eval = new StepFunction();
		//Eval eval = new SchwefelFunction1_2();
		//Eval eval = new SchwefelFunction();
		//Eval eval = new Rastrigin();
		
		
		
		Env env = new Env(localLimit, globalLimit, maxGroup, eval);
		
		List<SMAgent> agents = new ArrayList<SMAgent>();
		for (int i = 0; i < nbrAgent; i++) {
			agents.add(new SMAgent(i, pr, env, eval));
		}
		
		Group group = new Group();
		group.addAgents(agents);
		env.initGroup(group);
		
		
		// [RUN]
		
		int cycle = 0;
		
		while(Math.abs(eval.evaluate(env.getGlobalLeader())-eval.getObjective())>eval.getErrorDelta()) {
			do {
				for (SMAgent agent : agents) {
					agent.perceive();
				}
				for (SMAgent agent : agents) {
					agent.decide();
					agent.act();

				}
				cycle++;
			}while (agents.get(0).getPhase() != Phase.LOCAL_LEADER_PHASE);
			//cycle ++;
			//System.out.println("Cycle : "+cycle+" Value : "+eval.evaluate(env.getGlobalLeader()));
			//System.out.println(env.getGlobalLeader());
			
			/*
			// Pas à Pas
			try {
				System.in.read();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
			
		}
		//System.out.println("Best solution : "+env.getGlobalLeader());
		//System.out.println("That evaluate to : "+eval.evaluate(env.getGlobalLeader()));
		//System.out.println("Eval : "+eval.getCount());
		return cycle;
		
	}
	
	private static int findSolution2(
			int nbrAgent,
			double pr,
			int maxGroup,
			int localLimit,
			int globalLimit) {
		
		// [INIT]
		Eval eval = new StepFunction();
		//Eval eval = new SchwefelFunction1_2();
		//Eval eval = new SchwefelFunction();
		//Eval eval = new Rastrigin();
		
		Env env = new Env(localLimit, globalLimit, maxGroup, eval);
		
		List<SMAgent> agents = new ArrayList<SMAgent>();
		for (int i = 0; i < nbrAgent; i++) {
			agents.add(new SMAgent(i, pr, env, eval));
		}
		
		Group group = new Group();
		group.addAgents(agents);
		env.initGroup(group);
		
		
		// [RUN]
		Scheduler scheduler = new Scheduler(env, eval,  agents.toArray(new SMAgent[agents.size()]));
		scheduler.start();
		
		scheduler.waitUntilFinish();
		
		return scheduler.getNbOfCycles();
	}

}