Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #79] Replace Breakable For Loops By While Loops #503

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.spark.mllib.tree

import scala.util.control.Breaks._

import org.apache.spark.annotation.Experimental
import org.apache.spark.{Logging, SparkContext}
import org.apache.spark.SparkContext._
Expand Down Expand Up @@ -82,31 +80,34 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
* still survived the filters of the parent nodes.
*/

// TODO: Convert for loop to while loop
breakable {
for (level <- 0 until maxDepth) {

logDebug("#####################################")
logDebug("level = " + level)
logDebug("#####################################")

// Find best split for all nodes at a level.
val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy,
level, filters, splits, bins)

for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex) {
// Extract info for nodes at the current level.
extractNodeInfo(nodeSplitStats, level, index, nodes)
// Extract info for nodes at the next lower level.
extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
filters)
logDebug("final best split = " + nodeSplitStats._1)
}
require(scala.math.pow(2, level) == splitsStatsForLevel.length)
// Check whether all the nodes at the current level at leaves.
val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
logDebug("all leaf = " + allLeaf)
if (allLeaf) break // no more tree construction
var level = 0
var break = false
while (level < maxDepth && !break) {

logDebug("#####################################")
logDebug("level = " + level)
logDebug("#####################################")

// Find best split for all nodes at a level.
val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy,
level, filters, splits, bins)

for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex) {
// Extract info for nodes at the current level.
extractNodeInfo(nodeSplitStats, level, index, nodes)
// Extract info for nodes at the next lower level.
extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
filters)
logDebug("final best split = " + nodeSplitStats._1)
}
require(scala.math.pow(2, level) == splitsStatsForLevel.length)
// Check whether all the nodes at the current level at leaves.
val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
logDebug("all leaf = " + allLeaf)
if (allLeaf) {
break = true // no more tree construction
} else {
level += 1
}
}

Expand Down Expand Up @@ -146,8 +147,8 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
parentImpurities: Array[Double],
filters: Array[List[Filter]]): Unit = {
// 0 corresponds to the left child node and 1 corresponds to the right child node.
// TODO: Convert to while loop
for (i <- 0 to 1) {
var i = 0
while (i <= 1) {
// Calculate the index of the node from the node level and the index at the current level.
val nodeIndex = scala.math.pow(2, level + 1).toInt - 1 + 2 * index + i
if (level < maxDepth - 1) {
Expand All @@ -166,6 +167,7 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
logDebug("Filter = " + filter)
}
}
i += 1
}
}
}
Expand Down