Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Expand the class heirachy to include functions, ranges, and domains

  • Loading branch information...
commit cdf1945c319b4cd6d08979688f490612eeaa1be8 1 parent 13177f5
@javajawa authored
View
13 src/networkssim/NetworksSim.java
@@ -39,20 +39,23 @@ public static void main(String[] args)
return new Distribution<Integer>()
{
@Override
- public double probabilityOf(Integer value)
+ public Double valueOf(Integer value)
{
return Math.E / value;
}
+
@Override
- public Integer rangeMin()
+ public Domain<Integer> domain()
{
- return 0;
+ return new IntRange(0, Integer.MAX_VALUE);
}
+
@Override
- public Integer rangeMax()
+ public Domain<Double> range()
{
- return Integer.MAX_VALUE;
+ return new DoubleRange(0.0D, 1.0D, Double.MIN_NORMAL);
}
+
};
}
View
4 src/networkssim/entropy/DiscreteDistribution.java
@@ -1,8 +1,6 @@
package networkssim.entropy;
-import java.util.Set;
-
+@SuppressWarnings("MarkerInterface")
public interface DiscreteDistribution<T extends Comparable<T> > extends Distribution<T>
{
- Set<T> domain();
}
View
14 src/networkssim/entropy/Distribution.java
@@ -1,12 +1,10 @@
package networkssim.entropy;
-/**
- *
- * @author Benedict
- */
-public interface Distribution<T extends Comparable<T> >
+import networkssim.functions.Function;
+
+@SuppressWarnings("MarkerInterface")
+public interface Distribution<T extends Comparable<T> > extends Function<T, Double>
{
- double probabilityOf(T value);
- T rangeMin();
- T rangeMax();
+ @Override
+ Double valueOf(T value);
}
View
27 src/networkssim/entropy/implementations/Bernoulli.java
@@ -7,9 +7,12 @@
import java.util.Random;
import networkssim.entropy.DiscreteDistribution;
import networkssim.entropy.Randomised;
+import networkssim.sets.Domain;
+import networkssim.sets.DoubleRange;
+import networkssim.sets.IntRange;
import networkssim.utilities.Combinatorics;
-public class Bernoulli extends Randomised implements DiscreteProcess, DiscreteDistribution<Long>
+public class Bernoulli extends Randomised implements DiscreteProcess, DiscreteDistribution<Integer>
{
private final int trials;
private final double p;
@@ -68,30 +71,21 @@ private static long sample(int trials, double p, double rand)
}
@Override
- public Set<Long> domain()
+ public Domain<Integer> domain()
{
- Set<Long> s = new HashSet<Long>(trials + 1);
- for (long i = 0; i <= trials; ++i)
- s.add(i);
- return s;
+ return new IntRange(0, trials);
}
@Override
- public double probabilityOf(Long value)
+ public Domain<Double> range()
{
- return probabilities[value.intValue()];
+ return new DoubleRange(0, 1, Double.MIN_NORMAL);
}
@Override
- public Long rangeMin()
+ public Double valueOf(Integer value)
{
- return 0L;
- }
-
- @Override
- public Long rangeMax()
- {
- return (long)trials;
+ return probabilities[value.intValue()];
}
public static double probabilityOf(int trials, double p, int value)
@@ -101,4 +95,5 @@ public static double probabilityOf(int trials, double p, int value)
d = d.multiply(BigDecimal.valueOf(1-p).pow(trials - value));
return d.doubleValue();
}
+
}
View
10 src/networkssim/functions/Function.java
@@ -0,0 +1,10 @@
+package networkssim.functions;
+
+import networkssim.sets.Domain;
+
+public interface Function<X,Y>
+{
+ Y valueOf(X value);
+ Domain<X> domain();
+ Domain<Y> range();
+}
View
60 src/networkssim/sets/Domain.java
@@ -0,0 +1,60 @@
+package networkssim.sets;
+
+import java.util.Collection;
+import java.util.Set;
+
+public abstract class Domain<T> implements Set<T>
+{
+
+ @Override
+ public final boolean isEmpty()
+ {
+ return false;
+ }
+
+ @Override
+ public final boolean add(T e)
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+ @Override
+ public final boolean remove(Object o)
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+ @Override
+ @SuppressWarnings("element-type-mismatch")
+ public boolean containsAll(Collection<?> c)
+ {
+ for (Object o : c)
+ if (!contains(o)) return false;
+ return true;
+ }
+
+ @Override
+ public final boolean addAll(Collection<? extends T> c)
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+ @Override
+ public final boolean retainAll(Collection<?> c)
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+ @Override
+ public final boolean removeAll(Collection<?> c)
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+ @Override
+ public final void clear()
+ {
+ throw new UnsupportedOperationException("Domains are immutable");
+ }
+
+}
View
87 src/networkssim/sets/DoubleRange.java
@@ -0,0 +1,87 @@
+package networkssim.sets;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+public class DoubleRange extends Domain<Double>
+{
+ private final double lower;
+ private final double upper;
+ private final double step;
+ private final int size;
+
+ public DoubleRange(double lower, double upper, double step)
+ {
+ this.lower = lower;
+ this.upper = upper;
+ this.step = step;
+ this.size = (int)Math.floor((upper - lower) / step);
+ }
+
+ @Override
+ public int size()
+ {
+ return size;
+ }
+
+ @Override
+ public boolean contains(Object o)
+ {
+ if (!(o instanceof Number))
+ return false;
+
+ final double n = ((Number)o).doubleValue() - lower;
+
+ if (n < 0 || n > (upper - lower))
+ return false;
+
+ final double del = Math.round(n/step) - n/step;
+
+ return (del < step/20 && del > -step/20);
+ }
+
+ @Override
+ public Iterator<Double> iterator()
+ {
+ return new Iterator<Double>() {
+ private int i = -1;
+ @Override
+ public boolean hasNext()
+ {
+ return i + 1 < size();
+ }
+
+ @Override
+ public Double next()
+ {
+ ++i;
+ return lower + step * i;
+
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException("Ranges are not modifable");
+ }
+ };
+ }
+
+ @Override
+ public Double[] toArray()
+ {
+ Double[] ret = new Double[size()];
+ int i = 0;
+ for (Double d : this)
+ ret[i++] = d;
+ return ret;
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a)
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+}
View
90 src/networkssim/sets/IntRange.java
@@ -0,0 +1,90 @@
+package networkssim.sets;
+
+import java.util.Iterator;
+
+public class IntRange extends Range<Integer>
+{
+ private final int lower;
+ private final int upper;
+ private final int step;
+ private final int size;
+
+ public IntRange(int lower, int upper)
+ {
+ this(lower, upper, 1);
+ }
+
+ public IntRange(int lower, int upper, int step)
+ {
+ this.lower = lower;
+ this.upper = upper;
+ this.step = step;
+ this.size = (int)Math.floor((upper - lower) / step);
+ }
+
+ @Override
+ public int size()
+ {
+ return size;
+ }
+
+ @Override
+ public boolean contains(Object o)
+ {
+ if (!(o instanceof Number))
+ return false;
+
+ final double n = ((Number)o).doubleValue() - lower;
+
+ if (n < 0 || n > (upper - lower))
+ return false;
+
+ final double del = Math.round(n/step) - n/step;
+
+ return (del < step/20 && del > -step/20);
+ }
+
+ @Override
+ public Iterator<Integer> iterator()
+ {
+ return new Iterator<Integer>() {
+ private int i = -1;
+ @Override
+ public boolean hasNext()
+ {
+ return i + 1 < size();
+ }
+
+ @Override
+ public Integer next()
+ {
+ ++i;
+ return lower + step * i;
+
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException("Ranges are not modifable");
+ }
+ };
+ }
+
+ @Override
+ public Integer[] toArray()
+ {
+ Integer[] ret = new Integer[size()];
+ int i = 0;
+ for (Integer d : this)
+ ret[i++] = d;
+ return ret;
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a)
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+}
View
40 src/networkssim/sets/Range.java
@@ -0,0 +1,40 @@
+package networkssim.sets;
+import java.util.Iterator;
+/**
+ *
+ * @author Benedict
+ */
+public class Range<T> extends Domain<T>
+{
+
+ @Override
+ public int size()
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public boolean contains(Object o)
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public Iterator<T> iterator()
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public Object[] toArray()
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a)
+ {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+}
View
2  src/networkssim/simulations/SlottedAloha.java
@@ -81,7 +81,7 @@ public void run()
while (s.sampleCount() < samples)
{
arrivals = arrivalProcess.next();
- retransmissions = Bernoulli.sample(curr, retransmissionProbability.probabilityOf(curr), r);
+ retransmissions = Bernoulli.sample(curr, retransmissionProbability.valueOf(curr), r);
if (arrivals + retransmissions == 1)
{
Please sign in to comment.
Something went wrong with that request. Please try again.