June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,23 @@
|
|||
using JuMP
|
||||
using GLPKMathProgInterface
|
||||
|
||||
model = Model(solver=GLPKSolverMIP())
|
||||
|
||||
@variable(model, vials_of_panacea >= 0, Int)
|
||||
@variable(model, ampules_of_ichor >= 0, Int)
|
||||
@variable(model, bars_of_gold >= 0, Int)
|
||||
|
||||
@objective(model, Max, 3000*vials_of_panacea + 1800*ampules_of_ichor + 2500*bars_of_gold)
|
||||
|
||||
@constraint(model, 0.3*vials_of_panacea + 0.2*ampules_of_ichor + 2.0*bars_of_gold <= 25.0)
|
||||
@constraint(model, 0.025*vials_of_panacea + 0.015*ampules_of_ichor + 0.002*bars_of_gold <= 0.25)
|
||||
|
||||
println("The optimization problem to be solved is:")
|
||||
println(model)
|
||||
|
||||
status = solve(model)
|
||||
|
||||
println("Objective value: ", getobjectivevalue(model))
|
||||
println("vials of panacea = ", getvalue(vials_of_panacea))
|
||||
println("ampules of ichor = ", getvalue(ampules_of_ichor))
|
||||
println("bars of gold = ", getvalue(bars_of_gold))
|
||||
|
|
@ -1,42 +1,38 @@
|
|||
import scala.annotation.tailrec
|
||||
|
||||
case class Item(name: String, value: Int, weight: Double, volume: Double)
|
||||
object UnboundedKnapsack extends App {
|
||||
private val (maxWeight, maxVolume) = (BigDecimal(25.0), BigDecimal(0.25))
|
||||
private val items = Seq(Item("panacea", 3000, 0.3, 0.025), Item("ichor", 1800, 0.2, 0.015), Item("gold", 2500, 2.0, 0.002))
|
||||
|
||||
val items = List(
|
||||
Item("panacea", 3000, 0.3, 0.025),
|
||||
Item("ichor", 1800, 0.2, 0.015),
|
||||
Item("gold", 2500, 2.0, 0.002))
|
||||
@tailrec
|
||||
private def packer(notPacked: Seq[Knapsack], packed: Seq[Knapsack]): Seq[Knapsack] = {
|
||||
def fill(knapsack: Knapsack): Seq[Knapsack] = items.map(i => Knapsack(i +: knapsack.bagged))
|
||||
|
||||
val (maxWeight, maxVolume) = (25, 0.25)
|
||||
def stuffer(Seq: Seq[Knapsack]): Seq[Knapsack] = // Cause brute force
|
||||
Seq.map(k => Knapsack(k.bagged.sortBy(_.name))).distinct
|
||||
|
||||
def show(is: List[Item]) =
|
||||
(items.map(_.name) zip items.map(i => is.count(_ == i))).map {
|
||||
case (i, c) => s"$i: $c"
|
||||
}.mkString(", ")
|
||||
|
||||
case class Knapsack(items: List[Item]) {
|
||||
def value = items.foldLeft(0)(_ + _.value)
|
||||
def weight = items.foldLeft(0.0)(_ + _.weight)
|
||||
def volume = items.foldLeft(0.0)(_ + _.volume)
|
||||
def isFull = !((weight <= maxWeight) && (volume <= maxVolume))
|
||||
override def toString =
|
||||
s"[${show(items)} | value: $value, weight: $weight, volume: $volume]"
|
||||
}
|
||||
|
||||
def fill(knapsack: Knapsack): List[Knapsack] =
|
||||
items.map(i => Knapsack(i :: knapsack.items))
|
||||
|
||||
//cause brute force
|
||||
def distinct(list: List[Knapsack]) =
|
||||
list.map(k => Knapsack(k.items.sortBy(_.name))).distinct
|
||||
|
||||
@tailrec
|
||||
def f(notPacked: List[Knapsack], packed: List[Knapsack]): List[Knapsack] =
|
||||
notPacked match {
|
||||
case Nil => packed.sortBy(_.value).takeRight(4)
|
||||
case _ =>
|
||||
val notFull = distinct(notPacked.flatMap(fill)).filterNot(_.isFull)
|
||||
f(notFull, notPacked ::: packed)
|
||||
if (notPacked.isEmpty) packed.sortBy(-_.totValue).take(4)
|
||||
else packer(stuffer(notPacked.flatMap(fill)).filter(_.isNotFull), notPacked ++ packed)
|
||||
}
|
||||
|
||||
f(items.map(i => Knapsack(List(i))), Nil).foreach(println)
|
||||
private case class Item(name: String, value: Int, weight: BigDecimal, volume: BigDecimal)
|
||||
|
||||
private case class Knapsack(bagged: Seq[Item]) {
|
||||
def isNotFull: Boolean = totWeight <= maxWeight && totVolume <= maxVolume
|
||||
|
||||
override def toString = s"[${show(bagged)} | value: $totValue, weight: $totWeight, volume: $totVolume]"
|
||||
|
||||
def totValue: Int = bagged.map(_.value).sum
|
||||
|
||||
private def totVolume = bagged.map(_.volume).sum
|
||||
|
||||
private def totWeight = bagged.map(_.weight).sum
|
||||
|
||||
private def show(is: Seq[Item]) =
|
||||
(items.map(_.name) zip items.map(i => is.count(_ == i)))
|
||||
.map { case (i, c) => f"$i:$c%3d" }
|
||||
.mkString(", ")
|
||||
}
|
||||
|
||||
packer(items.map(i => Knapsack(Seq(i))), Nil).foreach(println)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue