Skip to content

Commit

Permalink
build: adds scala 3 support
Browse files Browse the repository at this point in the history
- replaces enumeratum enums with custom case objects in order to be
  backwards compatible.
- adds missing import to tests.
- applies code styling.
  • Loading branch information
vagmcs committed Aug 27, 2022
1 parent 6f749f2 commit 02a5800
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
scala: [ 2.12.15, 2.13.8 ]
scala: [ 2.12.16, 2.13.8, 3.1.3 ]
steps:
- uses: actions/checkout@v2
- name: Set up JDK
Expand Down
58 changes: 31 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ val logger = ConsoleLogger()

sonatypeProfileName := "com.github.vagmcs"

lazy val root = project.in(file("."))
lazy val root = project
.in(file("."))
.aggregate(core, oj, lpsolve, gurobi, mosek)
.settings(publish / skip := true)

// Build settings for Optimus core
lazy val core = project.in(file("core"))
lazy val core = project
.in(file("core"))
.enablePlugins(AutomateHeaderPlugin)
.settings(Test / logLevel := Level.Info)
.settings(name := "optimus")
.settings(Seq(
libraryDependencies ++= Dependencies.Logging,
libraryDependencies ++= Dependencies.ScalaTest,
libraryDependencies ++= Dependencies.Tools
))
.settings(
Seq(
libraryDependencies ++= Dependencies.Logging,
libraryDependencies ++= Dependencies.ScalaTest,
libraryDependencies ++= Dependencies.Tools
)
)

// Build settings for Optimus oj solver
lazy val oj = Project("solver-oj", file("solver-oj"))
Expand All @@ -36,31 +40,31 @@ lazy val lpsolve = Project("solver-lp", file("solver-lp"))
.settings(libraryDependencies += Dependencies.LpSolve)

// Build settings for Optimus gurobi solver
lazy val gurobi = if (file("lib/gurobi.jar").exists)
Project("solver-gurobi", file("solver-gurobi"))
lazy val gurobi =
if (file("lib/gurobi.jar").exists) Project("solver-gurobi", file("solver-gurobi"))
.dependsOn(core % "compile->compile ; test->test")
.enablePlugins(AutomateHeaderPlugin)
.settings(name := "optimus-solver-gurobi")
.settings(Compile / unmanagedJars += file("lib/gurobi.jar"))
else
Project("solver-gurobi", file("solver-gurobi"))
.settings({
else Project("solver-gurobi", file("solver-gurobi"))
.settings {
logger.warn {
"Building in the absence of support for the Gurobi solver [ 'gurobi.jar' not found in 'lib' directory ]."}
Seq(name := "optimus-solver-gurobi", publish := { }, publishLocal := { })
})
"Building in the absence of support for the Gurobi solver [ 'gurobi.jar' not found in 'lib' directory ]."
}
Seq(name := "optimus-solver-gurobi", publish := {}, publishLocal := {})
}

// Build settings for Optimus mosek solver
lazy val mosek = if (file("lib/mosek.jar").exists)
Project("solver-mosek", file("solver-mosek"))
.dependsOn(core % "compile->compile ; test->test")
.enablePlugins(AutomateHeaderPlugin)
.settings(name := "optimus-solver-mosek")
.settings(Compile / unmanagedJars += file("lib/mosek.jar"))
else
Project("solver-mosek", file("solver-mosek"))
.settings({
lazy val mosek =
if (file("lib/mosek.jar").exists) Project("solver-mosek", file("solver-mosek"))
.dependsOn(core % "compile->compile ; test->test")
.enablePlugins(AutomateHeaderPlugin)
.settings(name := "optimus-solver-mosek")
.settings(Compile / unmanagedJars += file("lib/mosek.jar"))
else Project("solver-mosek", file("solver-mosek"))
.settings {
logger.warn {
"Building in the absence of support for the Mosek solver [ 'mosek.jar' not found in 'lib' directory ]."}
Seq(name := "optimus-solver-mosek", publish := { }, publishLocal := { })
})
"Building in the absence of support for the Mosek solver [ 'mosek.jar' not found in 'lib' directory ]."
}
Seq(name := "optimus-solver-mosek", publish := {}, publishLocal := {})
}
9 changes: 2 additions & 7 deletions core/src/main/scala/optimus/algebra/ConstraintRelation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package optimus.algebra

import enumeratum._
import scala.collection.immutable._
sealed abstract class ConstraintRelation(val entryName: String)

sealed abstract class ConstraintRelation(override val entryName: String) extends EnumEntry

object ConstraintRelation extends Enum[ConstraintRelation] {

val values: IndexedSeq[ConstraintRelation] = findValues
object ConstraintRelation {

case object GE extends ConstraintRelation(">=")
case object LE extends ConstraintRelation("<=")
Expand Down
20 changes: 9 additions & 11 deletions core/src/main/scala/optimus/algebra/Expression.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package optimus.algebra

import com.typesafe.scalalogging.LazyLogging
import gnu.trove.procedure.TLongDoubleProcedure
import optimus.algebra.ConstraintRelation._
import optimus.algebra.AlgebraOps._

/**
* Expression abstraction, should be extended by anything that is
Expand Down Expand Up @@ -50,7 +48,7 @@ abstract class Expression extends LazyLogging {
case _ => Product(this, that)
}

def unary_-(): Expression = Minus(0, this)
def unary_- : Expression = Minus(0, this)

def <:=(that: Expression): Constraint = Constraint(this, LE, that)

Expand Down Expand Up @@ -116,7 +114,7 @@ abstract class Var(val symbol: String) extends Expression {
case _ => Product(this, other)
}

override def unary_-(): Expression = Term(Const(-1), Vector(this))
override def unary_- : Expression = Term(Const(-1), Vector(this))

/** @return the symbol of the variable */
override def toString: String = if (symbol != ANONYMOUS) symbol else s"x@$index"
Expand Down Expand Up @@ -162,7 +160,7 @@ case class Term(scalar: Const, vars: Vector[Var]) extends Expression {
case _ => Product(this, that)
}

override def unary_-(): Expression = Term(Const(-scalar.value), vars)
override def unary_- : Expression = Term(Const(-scalar.value), vars)

override def toString: String = s"$scalar${vars.mkString("*")}"
}
Expand Down Expand Up @@ -198,7 +196,7 @@ class Const(val value: Double) extends Expression {

override def *(other: Expression): Expression = ConstProduct(this, other)

override def unary_-(): Expression = Const(-value)
override def unary_- : Expression = Const(-value)

override def toString: String = value.toString

Expand Down Expand Up @@ -232,14 +230,14 @@ case object Zero extends Const(0) {

override def *(expression: Expression): Expression = this

override def unary_-(): Expression = this
override def unary_- : Expression = this
}

case object One extends Const(1) {

override def *(expression: Expression): Expression = this

override def unary_-(): Const = Const(-1)
override def unary_- : Const = Const(-1)
}

/**
Expand All @@ -258,7 +256,7 @@ case class ConstProduct(scalar: Const, a: Expression) extends Expression {
case _ => LongDoubleMap(a.terms.keys, a.terms.values.map(_ * scalar.value))
}

override def unary_-(): Expression = ConstProduct(Const(-scalar.value), a)
override def unary_- : Expression = ConstProduct(Const(-scalar.value), a)
}

// ------------------------------------- Operator Expressions -------------------------------------
Expand Down Expand Up @@ -307,7 +305,7 @@ case class Plus(override val a: Expression, override val b: Expression) extends

protected def op(x: Double, y: Double): Double = x + y

override def unary_-(): Expression = Plus(-a, -b)
override def unary_- : Expression = Plus(-a, -b)
}

/**
Expand All @@ -320,7 +318,7 @@ case class Minus(override val a: Expression, override val b: Expression) extends

protected def op(x: Double, y: Double): Double = x - y

override def unary_-(): Expression = Minus(b, a)
override def unary_- : Expression = Minus(b, a)
}

/**
Expand Down
9 changes: 2 additions & 7 deletions core/src/main/scala/optimus/algebra/ExpressionType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package optimus.algebra

import enumeratum._
import scala.collection.immutable._
sealed trait ExpressionType

sealed trait ExpressionType extends EnumEntry

object ExpressionType extends Enum[ExpressionType] {

val values: IndexedSeq[ExpressionType] = findValues
object ExpressionType {

case object CONSTANT extends ExpressionType
case object LINEAR extends ExpressionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* \///// \/// \///// \/// \/// \/// \/// \///////// \//////////
*
* The mathematical programming library for Scala.
*
*
*/

package optimus.optimization
Expand Down
9 changes: 2 additions & 7 deletions core/src/main/scala/optimus/optimization/enums/PreSolve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package optimus.optimization.enums

import enumeratum._
import scala.collection.immutable._
sealed abstract class PreSolve(val entryName: String)

sealed abstract class PreSolve(override val entryName: String) extends EnumEntry

object PreSolve extends Enum[PreSolve] {

val values: IndexedSeq[PreSolve] = findValues
object PreSolve {

case object DISABLED extends PreSolve("Disabled")
case object CONSERVATIVE extends PreSolve("Conservative")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package optimus.optimization.enums

import enumeratum._
import scala.collection.immutable._
sealed abstract class SolutionStatus(val entryName: String)

sealed abstract class SolutionStatus(override val entryName: String) extends EnumEntry

object SolutionStatus extends Enum[SolutionStatus] {

val values: IndexedSeq[SolutionStatus] = findValues
object SolutionStatus {

case object NOT_SOLVED extends SolutionStatus("Not solved")
case object OPTIMAL extends SolutionStatus("Optimal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package optimus.optimization.enums

import enumeratum._
import scala.collection.immutable._
sealed trait SolverLib

sealed trait SolverLib extends EnumEntry

object SolverLib extends Enum[SolverLib] {

val values: IndexedSeq[SolverLib] = findValues
object SolverLib {

case object oJSolver extends SolverLib
case object LpSolve extends SolverLib
Expand Down
17 changes: 10 additions & 7 deletions core/src/test/scala/optimus/algebra/AlgebraSpecTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import optimus.optimization.MPModel
import optimus.algebra.AlgebraOps._
import optimus.optimization.model.{ INFINITE, MPFloatVar, MPIntVar }

/**
* Specification test for algebra.
*/
/** Specification test for algebra. */
final class AlgebraSpecTest extends AnyFunSpec with Matchers {

implicit val model: MPModel = MPModel()
Expand Down Expand Up @@ -116,7 +114,7 @@ final class AlgebraSpecTest extends AnyFunSpec with Matchers {
}

it("x * (-1) should be equal to -x") {
x * (-1.0) shouldEqual -x
x * -1.0 shouldEqual -x
}

it("0 - x should be equal to -x") {
Expand Down Expand Up @@ -179,7 +177,9 @@ final class AlgebraSpecTest extends AnyFunSpec with Matchers {
/*
* Produces 3.0(x * t) + 2.0(x * y) + 2.0(x * z) + 1.0(y * z) + 2.0(z * t) + 2.0z + 4.0t + 9.0
*/
it("(2*x*y + 2*z*t) + (2*x*z + 4*t + 5) + (3*x*t + z + y*z) + 4 + z should be equal to 3*x*t + 2*x*y + 2*x*z + 1*y*z + 2*z*t + 2*z + 4*t + 9") {
it(
"(2*x*y + 2*z*t) + (2*x*z + 4*t + 5) + (3*x*t + z + y*z) + 4 + z should be equal to 3*x*t + 2*x*y + 2*x*z + 1*y*z + 2*z*t + 2*z + 4*t + 9"
) {
expression1 + expression2 + expression3 + expression4 shouldEqual sum(expressions)
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ final class AlgebraSpecTest extends AnyFunSpec with Matchers {
}

it("-x + 4*y + x should equal to 4*y") {
-x + 4 * y + x should equal (4 * y)
-x + 4 * y + x should equal(4 * y)
}

it("(x + y) + (2*x + y) should equal to 3*x + 2*y") {
Expand Down Expand Up @@ -286,7 +286,10 @@ final class AlgebraSpecTest extends AnyFunSpec with Matchers {

val startSum = System.currentTimeMillis()
sum(variables1)
info("Summation of " + variables1.length + " variables took " + (System.currentTimeMillis() - startSum) + "ms to calculate")
info(
"Summation of " + variables1.length + " variables took " + (System
.currentTimeMillis() - startSum) + "ms to calculate"
)

val startProd = System.currentTimeMillis()
val expr = (x + y + x + y + t + z + t + z + 4.1 * y + x + 5) * (x + y + x + y + t + z + t + z + y + x + 2)
Expand Down
17 changes: 7 additions & 10 deletions core/src/test/scala/optimus/algebra/EncodeDecodeSpecTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ final class EncodeDecodeSpecTest extends AnyFunSpec with Matchers with ScalaChec
} yield (x, y)

forAll(generator) {
case (x: Int, y: Int) => whenever (x != y) {
encode(x) shouldNot be (encode(y))
}
case (x: Int, y: Int) => whenever(x != y) {
encode(x) shouldNot be(encode(y))
}
}
}
}
Expand All @@ -76,10 +76,7 @@ final class EncodeDecodeSpecTest extends AnyFunSpec with Matchers with ScalaChec
y <- Gen.choose(0, Int.MaxValue)
} yield (x, y)

forAll(generator) {
case (x: Int, y: Int) =>
encode(x, y) shouldEqual encode(y, x)
}
forAll(generator) { case (x: Int, y: Int) => encode(x, y) shouldEqual encode(y, x) }
}

it("An encoding should never be identical to another") {
Expand All @@ -92,9 +89,9 @@ final class EncodeDecodeSpecTest extends AnyFunSpec with Matchers with ScalaChec
} yield (x, y, z, q)

forAll(generator) {
case (x: Int, y: Int, z: Int, q: Int) => whenever (x != z && y != q) {
encode(x, y) shouldNot be (encode(z, q))
}
case (x: Int, y: Int, z: Int, q: Int) => whenever(x != z && y != q) {
encode(x, y) shouldNot be(encode(z, q))
}
}
}
}
Expand Down
Loading

0 comments on commit 02a5800

Please sign in to comment.