Search in sources :

Example 1 with Streamlet_.forInt

use of suite.util.Streamlet_.forInt in project suite by stupidsing.

the class SparseMatrix method transpose.

public SparseMatrix transpose() {
    var pq = new PriorityQueue<>(IntIntPair.class, height, Comparator.comparingInt(IntIntPair::snd));
    var matrix1 = forInt(width_).map(i -> new Spans()).toList();
    var js = new int[height];
    IntSink enqRow = r -> {
        var j = js[r];
        if (j < matrix.get(r).size)
            pq.add(IntIntPair.of(r, j));
    };
    for (var r = 0; r < height; r++) enqRow.f(r);
    while (!pq.isEmpty()) {
        var pair = pq.extractMin();
        var r = pair.t0;
        var j = js[r]++;
        var spans = matrix.get(r);
        matrix1.get(spans.columns[j]).add(r, spans.values[j]);
        enqRow.f(r);
    }
    return new SparseMatrix(width_, height, matrix1);
}
Also used : IntIntPair(primal.primitive.adt.pair.IntIntPair) Arrays(java.util.Arrays) List(java.util.List) IntSink(primal.primitive.IntPrim.IntSink) Comparator(java.util.Comparator) PriorityQueue(suite.adt.PriorityQueue) Streamlet_.forInt(suite.util.Streamlet_.forInt) IntSink(primal.primitive.IntPrim.IntSink) PriorityQueue(suite.adt.PriorityQueue)

Example 2 with Streamlet_.forInt

use of suite.util.Streamlet_.forInt in project suite by stupidsing.

the class UctSearch method search.

public Move search() {
    for (var move : visitor.getAllMoves()) {
        nRaveWins.put(move, new AtomicInteger());
        nRaveVisits.put(move, new AtomicInteger());
    }
    root = new UctNode<>();
    var count = new AtomicInteger();
    var end = System.currentTimeMillis() + boundedTime;
    forInt(numberOfThreads).collect(As.executeThreadsByInt(i -> {
        var j = 0;
        while (count.getAndIncrement() < numberOfSimulations) {
            playSimulation(visitor.cloneVisitor(), root, 0);
            if (100 < ++j) {
                j = 0;
                if (end < System.currentTimeMillis())
                    break;
            }
        }
    }));
    // finds best node
    best = root.bestChild;
    return best != null ? best.move : null;
}
Also used : Math.sqrt(java.lang.Math.sqrt) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Build(primal.Verbs.Build) Math.log1p(java.lang.Math.log1p) Map(java.util.Map) DecimalFormat(java.text.DecimalFormat) Weiqi(suite.weiqi.Weiqi) Math.max(java.lang.Math.max) As(suite.streamlet.As) HashMap(java.util.HashMap) Math.min(java.lang.Math.min) Streamlet_.forInt(suite.util.Streamlet_.forInt) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with Streamlet_.forInt

use of suite.util.Streamlet_.forInt in project suite by stupidsing.

the class Render method renderPixels.

public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
    var nThreads = Defaults.nThreads;
    var txs = NewInt.array(nThreads + 1, i -> width * i / nThreads);
    var pixels = new R3[width][height];
    forInt(nThreads).collect(As.executeThreadsByInt(t -> {
        for (var x = txs[t]; x < txs[t + 1]; x++) for (var y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
    }));
    var image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
    for (var x = 0; x < width; x++) for (var y = 0; y < height; y++) {
        var pixel = limit(pixels[x][y]);
        image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
    }
    return image;
}
Also used : Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) R3(suite.math.R3) To(suite.util.To) Math.min(java.lang.Math.min) IntInt_Obj(primal.primitive.IntInt_Obj) Log_(primal.os.Log_) Streamlet_.forInt(suite.util.Streamlet_.forInt) Defaults(suite.cfg.Defaults) JLabel(javax.swing.JLabel) Math.max(java.lang.Math.max) ImageIcon(javax.swing.ImageIcon) As(suite.streamlet.As) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) NewInt(primal.primitive.IntVerbs.NewInt) BiFun(primal.fp.Funs2.BiFun) R3(suite.math.R3) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 4 with Streamlet_.forInt

use of suite.util.Streamlet_.forInt in project suite by stupidsing.

the class Trade_ method collectBrokeredTrades.

public static Streamlet<Trade> collectBrokeredTrades(Puller<Trade> puller) {
    var trades0 = puller.toArray(Trade.class);
    var trades1 = new ArrayList<Trade>();
    var length0 = trades0.length;
    var i0 = 0;
    IntIntSink tx = (i0_, i1_) -> {
        if (forInt(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
            while (i0_ < i1_) {
                var trade0 = trades0[i0_++];
                if (!Equals.string(trade0.remark, "#"))
                    trades1.add(trade0);
            }
    };
    for (var i = 1; i < length0; i++) {
        var trade0 = trades0[i0];
        var trade1 = trades0[i];
        var isGroup = // 
        true && // 
        Equals.string(trade0.date, trade1.date) && // 
        Equals.string(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
        if (!isGroup) {
            tx.sink2(i0, i);
            i0 = i;
        }
    }
    tx.sink2(i0, length0);
    return Read.from(trades1);
}
Also used : Union(primal.Verbs.Union) Math.log1p(java.lang.Math.log1p) Fun(primal.fp.Funs.Fun) Math_(suite.math.Math_) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) Math.abs(java.lang.Math.abs) Obj_Flt(primal.primitive.FltPrim.Obj_Flt) Dbl_Dbl(primal.primitive.Dbl_Dbl) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Math.expm1(java.lang.Math.expm1) Valuation(suite.trade.Account.Valuation) Pair(primal.adt.Pair) IntFltPair(primal.primitive.adt.pair.IntFltPair) IntIntSink(primal.primitive.IntIntSink) Source(primal.fp.Funs.Source) Set(java.util.Set) Math.min(java.lang.Math.min) Read(primal.MoreVerbs.Read) LngIntPair(primal.primitive.adt.pair.LngIntPair) List(java.util.List) Eod(suite.trade.data.DataSource.Eod) AsInt(primal.primitive.fp.AsInt) Math.max(java.lang.Math.max) Math.floor(java.lang.Math.floor) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) IntIntSink(primal.primitive.IntIntSink) ArrayList(java.util.ArrayList)

Example 5 with Streamlet_.forInt

use of suite.util.Streamlet_.forInt in project suite by stupidsing.

the class NioClusterMapTest method testClusterMap.

@Test
public void testClusterMap() throws IOException {
    var nNodes = 3;
    var peers = forInt(nNodes).map2(i -> "NODE" + i, i -> new InetSocketAddress(localHost, 3000 + i)).toMap();
    var clusters = // 
    Read.from2(// 
    peers).keys().map2(name -> name, // 
    name -> ex(() -> new NioCluster(name, peers))).toMap();
    for (var cluster : clusters.values()) cluster.start();
    var peerNames = new ArrayList<>(peers.keySet());
    var clMap = // 
    Read.from2(// 
    peers).keys().map2(name -> name, // 
    name -> new NioClusterMap<Integer, String>(clusters.get(name))).toMap();
    Sleep.quietly(5 * 1000);
    System.out.println("=== CLUSTER FORMED (" + LocalDateTime.now() + ") ===\n");
    Source<NioClusterMap<Integer, String>> peerf = () -> clMap.get(peerNames.get(random.nextInt(nNodes)));
    Int_Obj<Sink<Runnable>> setf = i -> cont -> peerf.g().set(i, Integer.toString(i), v0 -> cont.run(), fail);
    Int_Obj<Sink<Runnable>> getf = i -> cont -> peerf.g().get(i, v -> {
        assertEquals(Integer.toString(i), v);
        cont.run();
    }, fail);
    Fun<NioCluster, Sink<Runnable>> closef = cluster -> cont -> {
        try {
            cluster.stop();
            System.out.println("=== CLUSTER STOPPED (" + LocalDateTime.now() + ") ===\n");
        } catch (IOException ex) {
            fail(ex);
        }
        cont.run();
    };
    var sinks = // 
    Streamlet.concat(// 
    forInt(9).map(setf), // 
    forInt(9).map(getf), Read.from2(clusters).values().map(closef)).toList();
    new Object() {

        public void run(int i) {
            if (i < sinks.size())
                sinks.get(i).f(() -> run(i + 1));
        }
    }.run(0);
    Read.from2(clusters).values().map(cluster -> New.thread(cluster::run)).collect(Start::thenJoin);
    for (var cluster : clusters.values()) cluster.close();
}
Also used : NioClusterMap(suite.net.cluster.impl.NioClusterMap) Fail.fail(primal.statics.Fail.fail) Fun(primal.fp.Funs.Fun) Source(primal.fp.Funs.Source) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) Random(java.util.Random) Start(primal.Verbs.Start) New(primal.Verbs.New) Log_(primal.os.Log_) InetSocketAddress(java.net.InetSocketAddress) Sink(primal.fp.Funs.Sink) Read(primal.MoreVerbs.Read) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Streamlet(primal.streamlet.Streamlet) Rethrow.ex(primal.statics.Rethrow.ex) Rethrow(primal.statics.Rethrow) Int_Obj(primal.primitive.IntPrim.Int_Obj) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Sleep(primal.Verbs.Sleep) NioCluster(suite.net.cluster.impl.NioCluster) NioClusterMap(suite.net.cluster.impl.NioClusterMap) Start(primal.Verbs.Start) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NioCluster(suite.net.cluster.impl.NioCluster) Sink(primal.fp.Funs.Sink) Test(org.junit.jupiter.api.Test)

Aggregations

Streamlet_.forInt (suite.util.Streamlet_.forInt)8 Map (java.util.Map)4 Test (org.junit.jupiter.api.Test)4 Read (primal.MoreVerbs.Read)4 Math.max (java.lang.Math.max)3 Math.min (java.lang.Math.min)3 HashMap (java.util.HashMap)3 Fun (primal.fp.Funs.Fun)3 Log_ (primal.os.Log_)3 As (suite.streamlet.As)3 IOException (java.io.IOException)2 Math.abs (java.lang.Math.abs)2 Math.log1p (java.lang.Math.log1p)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Random (java.util.Random)2