Advertisement
Guest User

Untitled

a guest
Oct 1st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.61 KB | None | 0 0
  1. import groovy.transform.Canonical
  2. import groovy.transform.EqualsAndHashCode
  3.  
  4. import java.util.concurrent.atomic.AtomicLong
  5. import java.util.stream.Collectors
  6. import java.util.stream.Stream
  7.  
  8. def memo = new HashMap<List<Integer>, Integer>()
  9. //def N = 8
  10. //def input = new IntRange(false, 0, N).collect { (Math.random() * 1000).intValue() }
  11. //println input
  12. /*
  13. def getKey(Integer i, Integer w) {
  14.     return Arrays.asList(i, w)
  15. }
  16.  
  17. Integer m(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo) {
  18.     def key = Arrays.asList(i, w)
  19.     if (memo.containsKey(key)) {
  20.         return memo.get(key)
  21.     }
  22.  
  23.     if (i == -1) {
  24.         return 0
  25.     }
  26.  
  27.     if (input.get(i) > w) {
  28.         def value = m(i - 1, w, input, memo)
  29.         memo.put(key, value)
  30.         return value
  31.     }
  32.  
  33.     def value = Math.max(
  34.             m(i - 1, w, input, memo),
  35.             m(i - 1, w - input.get(i), input, memo) + input.get(i)
  36.     )
  37.     memo.put(key, value)
  38.     return value
  39. }
  40.  
  41. def ans(Integer i, Integer w, List<Integer> input, Map<List<Integer>, Integer> memo, List<Integer> output) {
  42.     if (i == -1) {
  43.         return
  44.     }
  45.  
  46.     if (memo.get(getKey(i, w)) == memo.get(getKey(i - 1, w))) {
  47.         ans(i - 1, w, input, memo, output)
  48.     } else {
  49.         output.push(input.get(i))
  50.         ans(i - 1, w - input.get(i), input, memo, output)
  51.     }
  52. }
  53. */
  54. /*def average = input.stream().collect(Collectors.averagingInt { it }).intValue()
  55. def ref = (average * N * 3 / 4).intValue()
  56. println average
  57. println "ref: $ref"
  58. println m(input.size() - 1, ref, input, memo)
  59.  
  60. def result = new ArrayList<Integer>()
  61. ans(input.size() - 1, ref, input, memo, result)
  62. println "anwser: $result"*/
  63.  
  64. @Canonical
  65. @EqualsAndHashCode(excludes = ['pledges', 'amount'])
  66. class Kd {
  67.     Long id
  68.     Double initial
  69.     Double amount
  70.     List<Oo> pledges
  71.  
  72.     Kd(Long id, Double amount, List<Oo> pledges) {
  73.         this.id = id
  74.         this.initial = this.amount = amount
  75.         this.pledges = pledges
  76.     }
  77. }
  78.  
  79. @Canonical(excludes = 'loans')
  80. @EqualsAndHashCode(excludes = ['loans', 'amount'])
  81. class Oo {
  82.     Long id
  83.     Double initial
  84.     Double amount
  85.     Map<Kd, Double> loans
  86.  
  87.     Oo(Long id, Double amount) {
  88.         this.id = id
  89.         this.amount = this.initial = amount
  90.     }
  91. }
  92.  
  93. /*def reFilter(List<Kd> kdList) {
  94.     List<Kd> newList = kdList.stream().filter { it -> it.amount > 0 }.collect ( Collectors.toList() )
  95.     newList.stream().map { it.pledges }.map { it.stream().filter { it.amount == 0 } }.
  96. }*/
  97.  
  98. def iter(List<Kd> kdList) {
  99.  
  100.     /* 1. Calculate koef */
  101.     Kd kd = kdList.stream()
  102.             .filter {Kd kd -> kd.pledges.stream().mapToDouble { a -> a.amount }.sum() / kd.amount >= 1 }
  103.             .min(Comparator.comparing { Kd kd -> kd.pledges.stream().mapToDouble { a -> a.amount }.sum() / kd.amount })
  104.             .orElse(
  105.             kdList.stream()
  106.                     .filter { !it.pledges.stream().filter { it.amount > 0 }.collect(Collectors.toList()).isEmpty() }
  107.                     .findAny()
  108.                     .orElse(null)
  109.     )
  110.  
  111.     if (kd == null) {
  112.         println "ready exit from that devil code"
  113.         return Collections.emptyList()
  114.     }
  115.  
  116.     print "before: $kd  - "
  117.     /* 2. Select min oo */
  118.     Oo oo = kd.pledges.stream()
  119.             .filter { it.amount > 0d }
  120.             .min(Comparator.comparing { Oo oo -> oo.loans.keySet().size() })
  121.             .get()
  122.  
  123.     /* 3. Push oo to kd and save pledge koef */
  124.     if (kd.amount <= oo.amount) {
  125.         // kd.pledges.put(oo, kd.amount / oo.initial)
  126.         oo.loans.put(kd, oo.loans.get(kd) + kd.amount)
  127.         oo.amount -= kd.amount
  128.         kd.amount = 0
  129.     } else {
  130.         // kd.pledges.put(oo, oo.amount / oo.initial)
  131.         oo.loans.put(kd, oo.loans.get(kd) + oo.amount)
  132.         kd.amount -= oo.amount
  133.         oo.amount = 0
  134.     }
  135.  
  136.     println "after: $kd"
  137.     //kdList.stream().map { it.pledges.keySet().removeIf { it.amount == 0 } }
  138.     return  kdList.stream().filter { it -> it.amount > 0 }.collect ( Collectors.toList() )
  139. }
  140.  
  141. /* 4. Iterate while kd is not empty */
  142. def calc(List<Kd> kdList) {
  143.     while ( !kdList.isEmpty() ) {
  144.         kdList = iter(kdList)
  145.     }
  146. }
  147.  
  148. def prepareData(List<Kd> kdList, List<Oo> ooList) {
  149.     println "- calc 30%"
  150.     /* 30% */
  151.     List<Kd> initial = kdList.stream().peek { Kd kd -> kd.setAmount( kd.initial * 0.3 ) }.collect(Collectors.toList())
  152.     calc(initial)
  153.     if ( !ooList.stream().filter { it.amount > 0 }.findAny().isPresent() ) {
  154.         return
  155.     }
  156.  
  157.     /* 100% */
  158.     println "- calc 100%"
  159.     List<Kd> step2 = kdList.stream().peek { Kd kd -> kd.setAmount( kd.initial * 0.7 ) }.collect(Collectors.toList())
  160.     calc(step2)
  161.     if ( !ooList.stream().filter { it.amount > 0 }.findAny().isPresent() ) {
  162.         return
  163.     }
  164.  
  165.     /* 140% */
  166.     println "- calc 140%"
  167.     List<Kd> step3 = kdList.stream().peek { Kd kd -> kd.setAmount( kd.initial * 0.4 ) }.collect(Collectors.toList())
  168.     calc(step3)
  169. }
  170.  
  171. def test1() {
  172.     println "TEST 1 - BEGIN"
  173.     def N = 6
  174.     def input = new IntRange(false, 0, N).collect { (Math.random() * 1000).intValue() }
  175.     def average = input.stream().collect(Collectors.averagingInt { it }).intValue()
  176.     def ref = (average * N * 3 / 4).doubleValue()
  177.  
  178.     def oo = new Oo(1L, ref.doubleValue())
  179.     AtomicLong idSeq = new AtomicLong(0)
  180.     def input2 = input.stream().map { new Kd(idSeq.incrementAndGet(), it.doubleValue(), [oo]) }.collect(Collectors.toList())
  181.     oo.setLoans(input2.stream().collect(Collectors.toMap({it -> it}, {Double.valueOf(0)})) as HashMap<Kd, Double>)
  182.  
  183.     println "kdList: $input"
  184.     println "oo: $oo"
  185.     prepareData(input2, [oo])
  186.     print "coef: "
  187.     oo.loans.values().stream().mapToDouble { it / oo.initial }.forEach { print "$it " }
  188.     def coefSum = oo.loans.values().sum()
  189.     println()
  190.     println "Sum $coefSum"
  191.     println "TEST 2 - END"
  192. }
  193.  
  194. def test2() {
  195.     println "TEST 2 - BEGIN"
  196.     def input = Arrays.asList(10,10,15)
  197.     def oo = new Oo(1, 20d)
  198.     AtomicLong idSeq = new AtomicLong(0)
  199.     List<Kd> input2 = input.stream().map { new  Kd(idSeq.incrementAndGet() , it.doubleValue(), [oo]) }.collect(Collectors.toList())
  200.     oo.setLoans(input2.stream().collect(Collectors.toMap({it -> it}, {Double.valueOf(0)})) as HashMap<Kd, Double>)
  201.  
  202.     println "kdList: $input"
  203.     println "oo: $oo"
  204.     prepareData(input2, [oo])
  205.     print "coef: "
  206.     oo.loans.values().stream().mapToDouble { it / oo.initial }.forEach { print "$it " }
  207.     def coefSum = oo.loans.values().sum()
  208.     println()
  209.     println "Sum $coefSum"
  210.     println "TEST 2 - END"
  211. }
  212.  
  213. def test3() {
  214.     println "TEST 3 - BEGIN"
  215.     def oo1 = new Oo(1L, 20 )
  216.     def oo2 = new Oo(2L, 150 )
  217.     def oo3 = new Oo(3L, 380 )
  218.  
  219.     def kd1 = new Kd(1L, 100, [oo1, oo2])
  220.     def kd2 = new Kd(2L, 200, [oo1, oo2, oo3])
  221.     def kd3 = new Kd(3L, 300, [oo2, oo3])
  222.  
  223.  
  224.     List<Kd> input = [kd1, kd2, kd3]
  225.     List<Oo> ooList = [oo1, oo2, oo3]
  226.     oo1.setLoans([(kd1):0d, (kd2):0d])
  227.     oo2.setLoans([(kd1):0d, (kd2):0d, (kd3):0d])
  228.     oo3.setLoans([(kd2):0d, (kd3):0d])
  229.  
  230.     println "kdList: $input"
  231.     prepareData(input, ooList)
  232.  
  233.     print "coef: "
  234.     println()
  235.     ooList.forEach {
  236.         print "oo[$it.id]:"
  237.         println()
  238.         it.loans.keySet().forEach {Kd kd ->
  239.             def amount = it.loans.get(kd)
  240.             def koef = amount / it.initial
  241.             println "$kd.id - $amount ($koef)"
  242.         }
  243.         //it.loans.values().stream().mapToDouble { amount -> amount }.forEach { print "$it " }
  244.         println()
  245.  
  246.         def sum = it.loans.values().sum()
  247.         println "sum for links: $sum of $it.initial"
  248.     }
  249.     println "TEST 3 - END"
  250. }
  251.  
  252. def all(List<List> list) {
  253.     if (list.size() == 1) {
  254.         return list
  255.     }
  256.  
  257.     all(list.tail()).stream()
  258.             .flatMap { Stream.of(list.head() + it, it) }
  259.             .collect(Collectors.toList()) << list.head()
  260. }
  261.  
  262. def generate(int count, double chance = 0.2, int ooMax = 50) {
  263.     def kdResult = new IntRange(false, 0, count).collect { id ->
  264.         new Kd(id, (Math.random() * 10).intValue() * 100 + 100d, [])
  265.     }
  266.  
  267.     long idCounter = 0
  268.     def ooResult = all(kdResult.collect { [it] }).stream()
  269.             .filter { Math.random() > chance }
  270.             .filter { it.size() != 1 }
  271.             .map { List<Kd> kdList ->
  272.         def amount = (Math.random() * ooMax).intValue() * 10 + 20
  273.         def oo = new Oo(idCounter++, amount)
  274.         oo.loans = kdList.collectEntries { [(it): 0d] }
  275.         kdList.each { kd ->
  276.             kd.pledges << oo
  277.         }
  278.  
  279.         return oo
  280.     }
  281.     .collect(Collectors.toList())
  282.  
  283.     return [kd: kdResult, oo: ooResult]
  284. }
  285.  
  286. void printTable(List<Kd> kdList, List<Oo> ooList, Closure<Double> mapper = { kd, oo -> oo.loans.get(kd) }) {
  287.     print String.format('%29s', '')
  288.     ooList.each {
  289.         print String.format('%7s', "OO ${it.id + 1}")
  290.     }
  291.     println()
  292.     kdList.each { kd ->
  293.         print String.format('KD %d (%7.1f%7.1f%7.1f):', kd.id + 1, kd.initial * 0.3, kd.initial, kd.initial * 1.4)
  294.         double sum = 0d
  295.         ooList.each { oo ->
  296.             if (kd.pledges.contains(oo)) {
  297.                 sum += mapper(kd, oo)
  298.                 print String.format('%7.1f', mapper(kd, oo))
  299.             } else {
  300.                 print String.format('%6s%1s', '--', '')
  301.             }
  302.         }
  303.         print String.format('%9.1f', sum)
  304.         println()
  305.     }
  306. }
  307.  
  308. def result = generate(5, 0.1, 17)
  309. printTable(result['kd'], result['oo']) { kd, oo -> oo.amount }
  310. prepareData(result['kd'], result['oo'])
  311. printTable(result['kd'], result['oo'])
  312.  
  313. //test1()
  314. //println()
  315. //test2()
  316. //println()
  317. //test3()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement