<commit_before>package de.fhwedel.antscout
package antnet

import scala.collection.mutable.{HashMap => MutableHashMap, Map => MutableMap}
import akka.actor.ActorRef
import net.liftweb.common.Logger
import collection.IterableView

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 26.12.11
 * Time: 20:55
 */

class PheromoneMatrix(destinations: IterableView[ActorRef, Iterable[ActorRef]], outgoingWays: List[ActorRef], travelTimes: Map[ActorRef, Double]) extends MutableHashMap[ActorRef, MutableMap[ActorRef, Double]] with Logger {

  val alpha = 0.3
  val heuristicValues: Map[ActorRef, Double] = initHeuristicValues
  val pheromones = MutableMap.empty[ActorRef, MutableMap[ActorRef, Double]] 
    
  this ++= destinations.map((_ -> MutableHashMap.empty[ActorRef, Double]))
  initPheromones()
  calculatePropabilities()

  /**
   * Berechnet die heuristische Größe η, die sich durch Normalisierung der Größe q aller Kanten, die zu einem benachbarten Knoten führen, ergibt. q ist gleich der Fahrdauer im statischen Fall.
   */
  def initHeuristicValues = {
    val travelTimesSum = travelTimes.values.sum
    travelTimes.map {
      case (antWay, travelTime) => (antWay, 1 - travelTime / travelTimesSum)
    }
  }

  def initPheromones() {
    val pheromone = 1.0 / outgoingWays.size
    destinations.foreach(d => {
      pheromones += d -> MutableMap.empty[ActorRef, Double]
      outgoingWays.foreach(ow => {
        pheromones(d) += ow -> pheromone
      })
    })
  }
  
  def calculatePropabilities() = {
    destinations.foreach(destination => {
      outgoingWays.foreach(outgoingWay => {
        this(destination) += outgoingWay -> (pheromones(destination)(outgoingWay) + alpha * heuristicValues(outgoingWay) / (1 + alpha * (outgoingWays.size - 1)))
      })
    })
  }
  
  def calculatePropabilities(destination: ActorRef) = {
    outgoingWays.foreach(ow => {
      this(destination) += ow -> (pheromones(destination)(ow) + alpha * heuristicValues(ow) / (1 + alpha * (outgoingWays.size - 1)))
    })
  }

  def updatePheromones(destination: ActorRef, way: ActorRef, reinforcement: Double) {
    trace("updatePheromones")
    outgoingWays.foreach(ow => {<commit_msg>Refactoring<commit_after>package de.fhwedel.antscout
package antnet

import akka.actor.ActorRef
import collection.IterableView
import collection.mutable
import net.liftweb.common.Logger

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 26.12.11
 * Time: 20:55
 */

class PheromoneMatrix(destinations: IterableView[ActorRef, Iterable[ActorRef]], outgoingWays: List[ActorRef], travelTimes: Map[ActorRef, Double]) extends mutable.HashMap[ActorRef, mutable.Map[ActorRef, Double]] with Logger {

  val alpha = 0.3
  val heuristicValues: Map[ActorRef, Double] = initHeuristicValues
  val pheromones = mutable.Map[ActorRef, mutable.Map[ActorRef, Double]]()
    
  this ++= destinations.map((_ -> mutable.Map[ActorRef, Double]()))
  initPheromones()
  calculatePropabilities()

  /**
   * Berechnet die heuristische Größe η, die sich durch Normalisierung der Größe q aller Kanten, die zu einem benachbarten Knoten führen, ergibt. q ist gleich der Fahrdauer im statischen Fall.
   */
  def initHeuristicValues = {
    val travelTimesSum = travelTimes.values.sum
    travelTimes.map {
      case (antWay, travelTime) => (antWay, 1 - travelTime / travelTimesSum)
    }
  }

  def initPheromones() {
    val pheromone = 1.0 / outgoingWays.size
    destinations.foreach(d => {
      pheromones += d -> mutable.Map[ActorRef, Double]()
      outgoingWays.foreach(ow => {
        pheromones(d) += ow -> pheromone
      })
    })
  }
  
  def calculatePropabilities(): Unit = {
    destinations.foreach(calculatePropabilities _)
  }
  
  def calculatePropabilities(destination: ActorRef) = {
    outgoingWays.foreach(ow => {
      this(destination) += ow -> (pheromones(destination)(ow) + alpha * heuristicValues(ow) / (1 + alpha * (outgoingWays.size - 1)))
    })
  }

  def updatePheromones(destination: ActorRef, way: ActorRef, reinforcement: Double) {
    trace("updatePheromones")
    outgoingWays.foreach(ow => {
{'fraction_non_alphanumeric': 0.11812179016874541, 'fraction_numerical': 0.00880410858400587, 'mean_word_length': 3.933655006031363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '677687', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1319, 'n_words': 290}