Advertisement
Guest User

Untitled

a guest
May 26th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1.  
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19.  
  20. import edu.purdue.cs.aggr.NOAHReduceFunction;
  21. import org.apache.flink.api.common.functions.MapFunction;
  22. import org.apache.flink.api.common.functions.ReduceFunction;
  23. import org.apache.flink.api.java.DataSet;
  24. import org.apache.flink.api.java.ExecutionEnvironment;
  25.  
  26. /**
  27. * Estimates the value of Pi using the Monte Carlo method.
  28. * The area of a circle is Pi * R^2, R being the radius of the circle
  29. * The area of a square is 4 * R^2, where the length of the square's edge is 2*R.
  30. *
  31. * Thus Pi = 4 * (area of circle / area of square).
  32. *
  33. * The idea is to find a way to estimate the circle to square area ratio.
  34. * The Monte Carlo method suggests collecting random points (within the square)
  35. * and then counting the number of points that fall within the circle
  36. *
  37. * <pre>
  38. * {@code
  39. * x = Math.random()
  40. * y = Math.random()
  41. *
  42. * x * x + y * y < 1
  43. * }
  44. * </pre>
  45. */
  46. @SuppressWarnings("serial")
  47. public class RandomSummer implements java.io.Serializable {
  48.  
  49. public static void main(String[] args) throws Exception {
  50.  
  51. final long numSamples = args.length > 1 ? Long.parseLong(args[1]) : 1000000;
  52.  
  53. final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  54.  
  55. // count how many of the samples would randomly fall into
  56. // the unit circle
  57. DataSet<CustomLong> count =
  58. env.generateSequence(1, numSamples)
  59. .map(new Sampler())
  60. .reduce(new SumReducer());
  61.  
  62. count.print();
  63.  
  64. env.execute("RandomSummer");
  65. }
  66.  
  67. //*************************************************************************
  68. // USER FUNCTIONS
  69. //*************************************************************************
  70.  
  71. public static class CustomLong {
  72.  
  73. // fields
  74. private Long number;
  75.  
  76. // constructors
  77. public CustomLong() {}
  78.  
  79. public CustomLong(Long n) {
  80. this.number = n;
  81. }
  82.  
  83. // getters setters
  84. public Long getNumber() {
  85. return number;
  86. }
  87. public void setNumber(Long n) {
  88. this.number = n;
  89. }
  90.  
  91. @Override
  92. public String toString() {
  93. return number.toString();
  94. }
  95. }
  96.  
  97. /**
  98. * Sampler randomly emits points that fall within a square of edge x * y.
  99. * It calculates the distance to the center of a virtually centered circle of radius x = y = 1
  100. * If the distance is less than 1, then and only then does it returns a 1.
  101. */
  102. public static class Sampler implements MapFunction<Long, CustomLong> {
  103.  
  104. @Override
  105. public CustomLong map(Long value) throws Exception{
  106. double x = Math.random();
  107. double y = Math.random();
  108. return (x * x + y * y) < 1 ? new CustomLong(1L) : new CustomLong(0L);
  109. }
  110. }
  111.  
  112.  
  113. /**
  114. * Simply sums up all long values.
  115. */
  116. public static final class SumReducer implements
  117. NOAHReduceFunction<CustomLong> {
  118.  
  119. @Override
  120. public CustomLong reduce(CustomLong value1, CustomLong value2) throws Exception {
  121. value1.setNumber(value1.getNumber() + value2.getNumber());
  122. return value1;
  123. }
  124.  
  125. @Override
  126. public float getInputOutputRatio(int numParitions) {
  127. return 1.0f;
  128. }
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement