Example usage for com.google.common.collect HashBasedTable contains

List of usage examples for com.google.common.collect HashBasedTable contains

Introduction

In this page you can find the example usage for com.google.common.collect HashBasedTable contains.

Prototype

@Override
    public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) 

Source Link

Usage

From source file:carskit.alg.cars.adaptation.dependent.dev.GCSLIM_CC.java

@Override
protected void buildModel() throws Exception {

    // number of iteration cycles
    for (int iter = 1; iter <= numIters; iter++) {

        loss = 0;//from   w  ww.  j  a  va 2 s .  c  om

        for (MatrixEntry me : trainMatrix) {

            int ui = me.row(); // user-item
            int u = rateDao.getUserIdFromUI(ui);
            int j = rateDao.getItemIdFromUI(ui);
            int c = me.column(); // context
            double rujc = me.get();

            Collection<Integer> nns = knn > 0 ? itemNNs.get(j) : allItems;
            SparseVector Ru = userCache.get(u);

            HashBasedTable<Integer, Integer, Double> Dev_weights = HashBasedTable.create();
            HashBasedTable<Integer, Integer, Double> Weight_devs = HashBasedTable.create();

            double pred = 0;
            for (int k : nns) {
                if (Ru.contains(k)) {
                    if (k != j) {
                        // extract a random contextual rating by user u and item k
                        String key = u + "," + k;
                        int uiid = rateDao.getUserItemId(key);
                        List<Integer> ctxid = this.trainMatrix.getColumns(uiid);

                        Random r = new Random();
                        int index = r.nextInt(ctxid.size());
                        int ctx = ctxid.get(index);

                        // get rating for u, k, ctx
                        double ruk = this.trainMatrix.get(uiid, ctx);

                        String[] sfrom = rateDao.getContextId(ctx).split(",");
                        String[] sto = rateDao.getContextId(c).split(",");
                        double dev_c = 0;
                        double w = W.get(k, j);
                        for (int i = 0; i < sfrom.length; ++i) {
                            int cond1 = Integer.valueOf(sfrom[i]);
                            int cond2 = Integer.valueOf(sto[i]);
                            dev_c += ccDev.get(cond1, cond2);
                            if (cond1 != cond2) {
                                if (Dev_weights.contains(cond1, cond2))
                                    Dev_weights.put(cond1, cond2, w + Dev_weights.get(cond1, cond2));
                                else
                                    Dev_weights.put(cond1, cond2, w);
                            }
                        }
                        Weight_devs.put(k, j, dev_c + ruk);
                        pred += (ruk + dev_c) * w;

                    }

                }
            }

            double eujc = rujc - pred;
            loss += eujc * eujc;

            for (int idk : Weight_devs.rowKeySet())
                for (int idj : Weight_devs.row(idk).keySet()) {

                    double update = W.get(idk, idj);

                    loss += regLw2 * update * update + regLw1 * update;

                    double delta_w = eujc * Weight_devs.get(idk, idj) - regLw2 * update - regLw1;
                    update += lRate * delta_w;
                    W.set(idk, idj, update);

                }

            // start updating cDev
            for (int cond1 : Dev_weights.rowKeySet())
                for (int cond2 : Dev_weights.row(cond1).keySet()) {

                    double update = ccDev.get(cond1, cond2);
                    loss += regLc2 * update * update + regLc1 * update;

                    double delta_c = eujc * Dev_weights.get(cond1, cond2) - regLc2 * update - regLc1;
                    update += lRate * delta_c;
                    ccDev.set(cond1, cond2, update);

                }

        }

    }
}