Advertisement
Guest User

Untitled

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