<commit_before>package cas.ypsiliform.mediator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import cas.ypsiliform.Constants;
import cas.ypsiliform.mediator.async.Function;
import cas.ypsiliform.mediator.async.Thenable;
import cas.ypsiliform.mediator.negotiation.AgentProxy;
import cas.ypsiliform.mediator.negotiation.SolutionProposal;
import cas.ypsiliform.messages.AgentResponse;
import cas.ypsiliform.messages.EndNegotiation;
import cas.ypsiliform.messages.MediatorRequest;
import cas.ypsiliform.messages.Solution;

/**
 * Mediator which moderates a negotiation between a given list of agents
 * 
 * @author <NAME>
 */
public class Mediator implements Runnable {
	private static Logger log = Logger.getLogger(Mediator.class.getSimpleName());

	private final Map<Integer, AgentProxy> agents;
	private final Integer[] primaryDemands;

	/**
	 * Mediator takes a list of agents that take part in the negotiation and the
	 * primary demands, which are the basis for the negotiation.
	 * 
	 * @param agents
	 * @param primaryDemands
	 */
	public Mediator(Map<Integer, AgentProxy> agents, Integer[] primaryDemands) {
		// validate consecutive IDs
		for (int i = 1; i <= agents.size(); i++) {
			if (!agents.containsKey(i))
				throw new IllegalArgumentException();
		}

		if (agents.size() <= 2)
			throw new IllegalArgumentException();

		if (primaryDemands.length != Constants.Encoding.NUMBER_OF_PERIODS) {
			throw new IllegalArgumentException();
		}

		this.agents = agents;
		this.primaryDemands = primaryDemands;

		registerAgentDeadListener(agents.values());
	}

	@Override
	public void run() {
		log.info("Starting new negotiation with " + agents.size() + " agents");

		List<SolutionProposal> solutions = new ArrayList<SolutionProposal>();
		solutions.add(new SolutionProposal(Constants.Encoding.NUMBER_OF_PERIODS));

		for (int i = 0; i < Constants.Negotiation.NUMBER_OF_ROUNDS; i++) {
			log.fine("Entering round " + i);

			SolutionProposal[] nextGen = generateOffspring(getNumberOfProposals(),
					getNumberOfMutations(i, Constants.Negotiation.NUMBER_OF_ROUNDS),
					solutions.toArray(new SolutionProposal[0]));

			Map<Integer, Proposal> proposals = new HashMap<Integer, Proposal>();

			for (int j = 0; j < nextGen.length; j++) {
				proposals.put(j + 1, new Proposal(nextGen[j], primaryDemands));
			}

			recursiveNegotiation(agents.get(1), proposals).then((Function<Map<Integer, Integer>, Thenable>) votes -> {
				int[] histogram = new int[getNumberOfProposals()];

				votes.forEach((agentId, solution) -> {
					// assuming solutions are numbered 1 - n
					histogram[solution - 1]++;
				});

				int maxVotes = 0;
				for (int j = 1; j < histogram.length; j++) {
					maxVotes = Math.max(histogram[j - 1], histogram[j]);
				}

				solutions.clear();
<commit_msg>Fix determination of parent solutions<commit_after>package cas.ypsiliform.mediator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import cas.ypsiliform.Constants;
import cas.ypsiliform.mediator.async.Function;
import cas.ypsiliform.mediator.async.Thenable;
import cas.ypsiliform.mediator.negotiation.AgentProxy;
import cas.ypsiliform.mediator.negotiation.SolutionProposal;
import cas.ypsiliform.messages.AgentResponse;
import cas.ypsiliform.messages.EndNegotiation;
import cas.ypsiliform.messages.MediatorRequest;
import cas.ypsiliform.messages.Solution;

/**
 * Mediator which moderates a negotiation between a given list of agents
 * 
 * @author <NAME>
 */
public class Mediator implements Runnable {
	private static Logger log = Logger.getLogger(Mediator.class.getSimpleName());

	private final Map<Integer, AgentProxy> agents;
	private final Integer[] primaryDemands;

	/**
	 * Mediator takes a list of agents that take part in the negotiation and the
	 * primary demands, which are the basis for the negotiation.
	 * 
	 * @param agents
	 * @param primaryDemands
	 */
	public Mediator(Map<Integer, AgentProxy> agents, Integer[] primaryDemands) {
		// validate consecutive IDs
		for (int i = 1; i <= agents.size(); i++) {
			if (!agents.containsKey(i))
				throw new IllegalArgumentException();
		}

		if (agents.size() <= 2)
			throw new IllegalArgumentException();

		if (primaryDemands.length != Constants.Encoding.NUMBER_OF_PERIODS) {
			throw new IllegalArgumentException();
		}

		this.agents = agents;
		this.primaryDemands = primaryDemands;

		registerAgentDeadListener(agents.values());
	}

	@Override
	public void run() {
		log.info("Starting new negotiation with " + agents.size() + " agents");

		List<SolutionProposal> solutions = new ArrayList<SolutionProposal>();
		solutions.add(new SolutionProposal(Constants.Encoding.NUMBER_OF_PERIODS));

		for (int i = 0; i < Constants.Negotiation.NUMBER_OF_ROUNDS; i++) {
			log.fine("Entering round " + i);

			SolutionProposal[] nextGen = generateOffspring(getNumberOfProposals(),
					getNumberOfMutations(i, Constants.Negotiation.NUMBER_OF_ROUNDS),
					solutions.toArray(new SolutionProposal[0]));

			Map<Integer, Proposal> proposals = new HashMap<Integer, Proposal>();

			for (int j = 0; j < nextGen.length; j++) {
				proposals.put(j + 1, new Proposal(nextGen[j], primaryDemands));
			}

			recursiveNegotiation(agents.get(1), proposals).then((Function<Map<Integer, Integer>, Thenable>) votes -> {
				int[] histogram = new int[getNumberOfProposals()];

				votes.forEach((agentId, solution) -> {
					// assuming solutions are numbered 1 - n
					histogram[solution - 1]++;
				});

				int maxVotes = 0;
				for (int j = 0; j < histogram.length; j++) {
					maxVotes = Math.max(maxVotes, histogram[j]);
				}

				solutions.clear();

{'fraction_non_alphanumeric': 0.120578231292517, 'fraction_numerical': 0.0039115646258503405, 'mean_word_length': 4.558601134215501, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4042249', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1781, 'n_words': 435}