Skip to content

Commit 4367c30

Browse files
cleanup
1 parent 732bec8 commit 4367c30

File tree

4 files changed

+134
-93
lines changed

4 files changed

+134
-93
lines changed

scala.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Scala CI
2+
3+
on:
4+
push:
5+
branches: [ Accounts ]
6+
pull_request:
7+
branches: [ Accounts ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v2
18+
with:
19+
java-version: '11'
20+
distribution: 'adopt'
21+
- name: Run tests
22+
run: sbt run test.runner

src/main/scala/test/Income.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ object Income {
1111
type IncomeEventGenDeps = Incomes with Date
1212
type IncomeEventDeps = Date
1313
//define incomes
14-
trait Income extends (IncomeEventDeps ==> Income) with produces[Seq[IncomeEvent]]{
14+
trait Income
15+
extends (IncomeEventDeps ==> Income)
16+
with produces[Seq[IncomeEvent]]{
1517
val id:Long
1618
val payableTo:Long
1719
val amount:Double
@@ -46,7 +48,8 @@ object Income {
4648
case class taxableIncomeEvent(incomeId:Long,payableTo:Long,amount:Double,date:LocalDate) extends IncomeEvent
4749

4850

49-
case class Incomes(value:Seq[Income],eventLog:Seq[IncomeEvent] = Seq()) extends (IncomeEventGenDeps ==> Incomes) with produces[Seq[Income]]{
51+
case class Incomes(value:Seq[Income],eventLog:Seq[IncomeEvent] = Seq())
52+
extends (IncomeEventGenDeps ==> Incomes) with produces[Seq[Income]]{
5053
override def apply(src: dataset[IncomeEventGenDeps]): dataset[Incomes] = for{
5154
incomes <- src.incomes
5255
}yield {
@@ -56,6 +59,7 @@ object Income {
5659
updatedIncomes <- incomes.update(inc)
5760
}yield accumSrc ++ updatedIncomes).incomes
5861
}
62+
5963
private def apply(income:Income):dataset[Incomes] = {
6064
val incomeMapCurr = this.incomeMap
6165
val exists = incomeMap.get(income.id).isDefined
Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
package test
2-
import Typical.core.dataset._
3-
import Typical.core.grammar._
4-
import Consumption._
5-
import scala.util.Random
6-
object RandomSim{
7-
case class RandomSim(prob:Double,proportion:Double,value:BigDecimal) extends ==>[RandomSim,RandomSim] {
8-
override def apply(src: dataset[RandomSim]): dataset[RandomSim] = for{
9-
r <- src.<--[RandomSim]
10-
}yield {
11-
if(r.value <= 0 ) r
12-
else if (Random.nextDouble() <= prob){
13-
RandomSim(prob,proportion,r.value + r.value*proportion)
14-
}
15-
else {
16-
val bd = r.value - r.value*proportion
17-
r.copy(value = if (bd < 0) 0 else bd )
18-
}
19-
}
20-
}
21-
22-
def fRun(r:RandomSim):RandomSim = {
23-
if(r.value <= 0 ) r
24-
else if (Random.nextDouble() <= r.prob){
25-
RandomSim(r.prob,r.proportion,r.value + r.value*r.proportion)
26-
}
27-
else {
28-
val bd = r.value - r.value*r.proportion
29-
r.copy(value = if (bd < 0) 0 else bd )
30-
}
31-
}
32-
def run(src:dataset[RandomSim with Counter],eps:Double):dataset[RandomSim] = try for{
33-
r <- src.<--[RandomSim]
34-
}yield {
35-
if (r.value <= eps) src else run(src.+->[Counter].+->[RandomSim],eps)
36-
}catch {
37-
case e:Exception =>
38-
println("An Error occured")
39-
src
40-
}
41-
42-
def run2(randomSim: RandomSim,eps:Double):RandomSim = if (randomSim.value <= eps) randomSim else run2(fRun(randomSim),eps)
43-
44-
def main(args:Array[String]):Unit = {
45-
val startSim = RandomSim(0.70,0.4,10000000)
46-
val dat = data[RandomSim with Counter]()
47-
.++(startSim)
48-
.++(Counter(0))
49-
//val res = (0 to 100000).foldLeft[dataset[RandomSim]](dat)((d,_) => d.calc[RandomSim])
50-
val res2 = run2(startSim,1000)
51-
println(res2)
52-
}
53-
}
1+
//package test
2+
//import Typical.core.dataset._
3+
//import Typical.core.grammar._
4+
//import Consumption._
5+
//import scala.util.Random
6+
//object RandomSim{
7+
// case class RandomSim(prob:Double,proportion:Double,value:BigDecimal) extends ==>[RandomSim,RandomSim] {
8+
// override def apply(src: dataset[RandomSim]): dataset[RandomSim] = for{
9+
// r <- src.<--[RandomSim]
10+
// }yield {
11+
// if(r.value <= 0 ) r
12+
// else if (Random.nextDouble() <= prob){
13+
// RandomSim(prob,proportion,r.value + r.value*proportion)
14+
// }
15+
// else {
16+
// val bd = r.value - r.value*proportion
17+
// r.copy(value = if (bd < 0) 0 else bd )
18+
// }
19+
// }
20+
// }
21+
//
22+
// def fRun(r:RandomSim):RandomSim = {
23+
// if(r.value <= 0 ) r
24+
// else if (Random.nextDouble() <= r.prob){
25+
// RandomSim(r.prob,r.proportion,r.value + r.value*r.proportion)
26+
// }
27+
// else {
28+
// val bd = r.value - r.value*r.proportion
29+
// r.copy(value = if (bd < 0) 0 else bd )
30+
// }
31+
// }
32+
// def run(src:dataset[RandomSim with Counter],eps:Double):dataset[RandomSim] = try for{
33+
// r <- src.<--[RandomSim]
34+
// }yield {
35+
// if (r.value <= eps) src else run(src.+->[Counter].+->[RandomSim],eps)
36+
// }catch {
37+
// case e:Exception =>
38+
// println("An Error occured")
39+
// src
40+
// }
41+
//
42+
// def run2(randomSim: RandomSim,eps:Double):RandomSim = if (randomSim.value <= eps) randomSim else run2(fRun(randomSim),eps)
43+
//
44+
// def main(args:Array[String]):Unit = {
45+
// val startSim = RandomSim(0.70,0.4,10000000)
46+
// val dat = data[RandomSim with Counter]()
47+
// .++(startSim)
48+
// .++(Counter(0))
49+
// //val res = (0 to 100000).foldLeft[dataset[RandomSim]](dat)((d,_) => d.calc[RandomSim])
50+
// val res2 = run2(startSim,1000)
51+
// println(res2)
52+
// }
53+
//}

src/main/scala/test/Test.scala

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,44 @@ object runner {
1818
import AccountRates._
1919
import Income._
2020
println("Initializing")
21-
val starterAccounts = Accounts(Seq(CheckingAccount(1,30000),BokerageAccount(2,60000)),Seq())
22-
val rentPeriod = dates(Month(LocalDate.now()),LocalDate.now().plusYears(20))
23-
val starterProperties = Properties(Seq(RentalProperty(1,1300,rentPeriod)),Seq())
24-
val startingDate = Month(LocalDate.now())
25-
val incomemeta = Seq(
26-
ficaTaxableincome(1,3500,1,dates(Month(LocalDate.now()),LocalDate.now().plusYears(10),Seq())),
27-
ficaTaxableincome(2,500,1,dates(Week(LocalDate.now().plusMonths(6)),LocalDate.now().plusYears(2),Seq())),
28-
ficaTaxableincome(3,5500,1,dates(Year(LocalDate.now()),LocalDate.now().plusYears(10),Seq())),
29-
ficaTaxableincome(4,1500,1,dates(Month(LocalDate.now().plusYears(1)),LocalDate.now().plusYears(10),Seq()))
30-
)
31-
val incomes = Incomes(incomemeta)
21+
3222
type ProgramDependencies =
3323
Accounts with
3424
AccountRates with
3525
Properties with
3626
Date with
3727
Incomes
3828

39-
val dat:dataset[ProgramDependencies] = data[ProgramDependencies]()
40-
//define start data
41-
.++[Date,Month](startingDate)++
42-
starterAccounts ++
43-
AccountRates(0.67,0.01,0.06) ++
44-
incomes ++
45-
starterProperties +-
46-
Prog(1000000)
47-
//include the program we want to loop over
48-
// if we dont' want to build an implicit grammar for it
4929

50-
def main(args: Array[String]): Unit = {
51-
val start = System.currentTimeMillis()
52-
//run base loop
53-
val res = dat.toWeeklyCadence.solve[Prog].properties.events.sortWith((a,b) => a.date.isBefore(b.date))//.get.get(3).value//.events.filter(_.incomeId == 1)
54-
println(res)
55-
val end = System.currentTimeMillis()
56-
println(s"time elapsed:${end - start} milliseconds")
57-
}
58-
case class Prog(limit:Double) extends (ProgramDependencies ==> ProgramDependencies) with solveable[ProgramDependencies] {
30+
////////////////////////////////////
31+
//define our top level simulation
32+
case class Prog(limit:Double)
33+
extends (ProgramDependencies ==> ProgramDependencies)
34+
with solveable[ProgramDependencies] {
5935
override def apply(src: dataset[ProgramDependencies]): dataset[ProgramDependencies] = for{
6036
date <- src.currentDate
6137
}yield{
6238
date match {
6339
case _:Month| _:Week =>
6440
src
65-
.growAccounts
66-
.accrueRent
67-
.receiveIncomes
68-
.payRents
69-
.nextPeriod
41+
.growAccounts
42+
.accrueRent
43+
.receiveIncomes
44+
.payRents
45+
.nextPeriod
7046
case y:Year =>
7147
(0 until 12).foldLeft(src)((src_,_) =>
7248
src_
7349
.toMonthlyCadence
74-
.runSim
50+
.runSim
7551
).toYearlyCadence
7652
}
7753
}
7854

7955
override def solved(src: dataset[ProgramDependencies]):Boolean =
8056
(src
81-
.underlyingAccounts.exists) &&
82-
src.underlyingAccounts
57+
.underlyingAccounts.exists) &&
58+
src.underlyingAccounts
8359
.value
8460
.map(_.balance)
8561
.sum > limit
@@ -88,10 +64,49 @@ object runner {
8864
src.runSim
8965
)
9066
}
67+
////////////////////////////////
68+
//define implicit grammar for sim
9169
import scala.reflect.runtime.universe.TypeTag
9270
implicit class RunSim[A<:ProgramDependencies](src:dataset[A])(implicit taga:TypeTag[A]){
9371
def runSim:dataset[A] = src.+-(Prog(10000000)).-->[Prog]
9472
}
9573

74+
/////////////////////////////////////////////////////////////
75+
///////////Define Starting Data/////////////////////////////
76+
val starterAccounts = Accounts(Seq(CheckingAccount(1,30000),BokerageAccount(2,60000)),Seq())
77+
val rentPeriod = dates(Month(LocalDate.now()),LocalDate.now().plusYears(20))
78+
val starterProperties = Properties(Seq(RentalProperty(1,1300,rentPeriod)),Seq())
79+
val startingDate = Month(LocalDate.now())
80+
val incomemeta = Seq(
81+
ficaTaxableincome(1,3500,1,dates(Month(LocalDate.now()),LocalDate.now().plusYears(10),Seq())),
82+
ficaTaxableincome(2,500,1,dates(Week(LocalDate.now().plusMonths(6)),LocalDate.now().plusYears(2),Seq())),
83+
ficaTaxableincome(3,5500,1,dates(Year(LocalDate.now()),LocalDate.now().plusYears(10),Seq())),
84+
ficaTaxableincome(4,1500,1,dates(Month(LocalDate.now().plusYears(1)),LocalDate.now().plusYears(10),Seq()))
85+
)
86+
val incomes = Incomes(incomemeta)
87+
val dat:dataset[ProgramDependencies] = data[ProgramDependencies]()
88+
//include initial data
89+
.++[Date,Month](startingDate)++
90+
starterAccounts ++
91+
AccountRates(0.67,0.01,0.06) ++
92+
incomes ++
93+
starterProperties +-
94+
//include the program we want to loop over
95+
// if we dont' want to build an implicit grammar for it
96+
Prog(1000000)
97+
98+
99+
def main(args: Array[String]): Unit = {
100+
val start = System.currentTimeMillis()
101+
//run solve baseLoop for solve condition
102+
val res = dat.toWeeklyCadence.solve[Prog]
103+
.properties
104+
.events
105+
.sortWith((a,b) => a.date.isBefore(b.date))
106+
println(res)
107+
val end = System.currentTimeMillis()
108+
println(s"time elapsed:${end - start} milliseconds")
109+
}
110+
96111

97112
}

0 commit comments

Comments
 (0)