Appendix F — Kotlin and KSL Setup

NoteLearning objectives

After reading this appendix you should be able to:

  • install the tools needed to run the simulation examples
  • open the course code project and run an example
  • add your own model to the project

F.1 What You Need

Two things, and nothing else:

  • A JDK, version 21 or later. Temurin is a good default. Verify the installation with java -version at a terminal.
  • IntelliJ IDEA, Community Edition. It has Kotlin support built in and understands Gradle projects without configuration.

You do not need to install Kotlin, Gradle, or the KSL separately. The course code project carries a Gradle wrapper that downloads the correct versions of Gradle and Kotlin on first use, and Gradle downloads the KSL from Maven Central.

F.2 Getting the Course Code Project

The Kotlin examples live in their own repository, separate from the book:

https://github.com/rossetti/RossettiInventoryBookCode

Clone it if you have Git. An updated example then costs you one command rather than a second download:

git clone https://github.com/rossetti/RossettiInventoryBookCode.git
cd RossettiInventoryBookCode

If you do not have Git, use the Code button on that page and choose Download ZIP, then unpack it. Either way you end up with one directory, and that directory is the Gradle project. Open it in IntelliJ with File, then Open, and select the directory itself rather than a file inside it. IntelliJ recognizes the Gradle build and configures the rest on its own.

When a chapter says that its code is in code/, it means this project. The book uses the short name because the directory you unpack it under is yours to name.

F.3 What Is in It

The project’s structure mirrors the book:

build.gradle.kts       build configuration; the KSL dependency lives here
settings.gradle.kts
gradlew, gradle/       the Gradle wrapper
src/main/kotlin/inventory/
  <topic>/             one package per subject
src/test/kotlin/       the checks that pin what the book prints
data/                  the demand histories used for fitting

A package is named for its subject, so inventory.dynamiclotsizing holds the code of Chapter 5 and inventory.newsvendor holds the code of Chapter 7. Chapter numbers move when a book is revised and subjects do not, which is the same reason the section labels you see in cross-references are semantic rather than numbered.

Notice the test directory. Every number this book prints from a program is asserted there, so ./gradlew test is a check that the copy you have agrees with the copy the book was written against. Run it once after you clone. If it passes, any later failure is yours and not the book’s, which is worth knowing before you start changing things.

F.4 Running Your First Model

From a terminal, in the project directory:

./gradlew run

The first run takes a minute while Gradle, Kotlin, and the KSL are downloaded; later runs start immediately. The default program runs the examples of Section 7.14 in order, starting with the final buy of the mower deck belt from Example 7.1. Its first two blocks are:

### the final buy, against the normal the Newsvendor sheet uses
order 475.18 units, to a level of 475.18 with 0.00 on hand
  critical ratio        0.542169   F(level) 0.542169
  expected shortage       32.2605 units
  expected leftover       42.0712 units
  expected cost           3050.43
  expected profit        17891.01

### the final buy, against the exact negative binomial
order 469.00 units, to a level of 469.00 with 0.00 on hand
  critical ratio        0.542169   F(level) 0.543428
  expected shortage       35.1478 units
  expected leftover       38.7824 units
  expected cost           3055.38
  expected profit        17886.06

The rest of the output is the comparison of demand families, the spare transformer, and two simulated grids for a scrap price paid in tiers, each described in Section 7.14.

Notice two things about that output before you go further. Every number in the first block but the profit is one Example 7.2 computes by hand: the critical ratio 0.542169, the buy of 475.18 belts, the expected shortage of 32.26 and leftover of 42.07, and the expected cost of $3,050.43. That agreement with the hand computation is how you gain confidence that the code is built correctly. And the two blocks disagree with each other by six belts, because the second uses the exact discrete demand model where the first uses its normal approximation, and the discrete rule stops at the first level whose distribution function clears the critical ratio (0.543428 against 0.542169). Ordering the normal’s 475 instead costs 0.19% more under the exact model, which Table 7.6 measures and the third block of the output prints.

To run it from IntelliJ instead, open the project directory as described in Section F.2 and use the gutter icon beside fun main.

F.5 Adding Your Own Model

Put the file in the package of the subject it belongs to, so a model of a continuous review policy goes in src/main/kotlin/inventory/continuousreview/ beside the chapter’s own examples. Give it a top-level fun main(). Kotlin compiles the top-level functions of a file named MyPolicy.kt into a class named MyPolicyKt, and that class name is what Gradle runs. To run your file from the terminal, pass the class as the mainClass property:

./gradlew run -PmainClass=inventory.continuousreview.MyPolicyKt

To make it the default instead, point application { mainClass } in build.gradle.kts at it; the block already reads the property and falls back to the newsvendor example when none is given. From IntelliJ, the gutter icon beside fun main runs the file with no configuration at all.

Keep the model’s inputs at the top of main as named values, as every example in the project does, so that the next reader can see what was assumed without reading the code that uses it. Thus, a model of your own is one file, one main, and one property on the command line.

F.6 The KSL Itself

The KSL is a large library, and this book uses a small part of it. For the library on its own terms, its packages, its process view, and its output analysis machinery, see Simulation Modeling using the KSL.

The version this course pins is declared in the project’s build.gradle.kts:

implementation("io.github.rossetti:KSLCore:R1.7.1")