Example usage for java.util List set

List of usage examples for java.util List set

Introduction

In this page you can find the example usage for java.util List set.

Prototype

E set(int index, E element);

Source Link

Document

Replaces the element at the specified position in this list with the specified element (optional operation).

Usage

From source file:eu.crisis_economics.abm.strategy.clearing.LCQPUncollateralizedReturnMaximizer.java

private List<Double> performOptimization(final List<Pair<Double, Double>> knownAssetRates,
        final List<Pair<Double, Double>> knownLiabilityRates, final double cashToSpend,
        final double capitalConstraint, final double toleranceFeasibleSolution, final double solutionTolerance,
        final int numberOfIterations) {
    if (VERBOSE_MODE) {
        System.out.printf("LCQP session:\n");
        for (int i = 0; i < knownAssetRates.size(); ++i) {
            Pair<Double, Double> assetRate = knownAssetRates.get(i);
            System.out.printf("asset:     %16.10g*r + %16.10g\n", assetRate.getFirst(), assetRate.getSecond());
        }/* www  .j  a  v  a 2s .  c  om*/
        for (int i = 0; i < knownLiabilityRates.size(); ++i) {
            Pair<Double, Double> liabilityRate = knownLiabilityRates.get(i);
            System.out.printf("liabiliy:  %16.10g*r + %16.10g\n", liabilityRate.getFirst(),
                    liabilityRate.getSecond());
        }
        System.out.printf("capital constraint: %16.10g\n" + "cash to spend:      %16.10g\n", capitalConstraint,
                cashToSpend);
    }

    // minimum market impact
    double smallestMarketImpact = Double.MAX_VALUE;
    for (final Pair<Double, Double> rate : knownAssetRates)
        if (rate.getFirst() != 0.)
            smallestMarketImpact = Math.min(smallestMarketImpact, Math.abs(rate.getFirst()));
    for (final Pair<Double, Double> rate : knownLiabilityRates)
        if (rate.getFirst() != 0.)
            smallestMarketImpact = Math.min(smallestMarketImpact, Math.abs(rate.getFirst()));

    // QP merit function
    final double scaleDown = 1. / capitalConstraint, scaleUp = 1. / scaleDown;
    final int dimension = knownAssetRates.size() + knownLiabilityRates.size();
    final double[][] P = new double[dimension][dimension];
    final double[] Q = new double[dimension];
    final int numAssetOptions = knownAssetRates.size(), numLiabilityOptions = knownLiabilityRates.size();
    for (int i = 0; i < knownAssetRates.size(); ++i) {
        P[i][i] = -2. * knownAssetRates.get(i).getFirst() * scaleUp;
        Q[i] = -knownAssetRates.get(i).getSecond();
    }
    for (int j = 0; j < knownLiabilityRates.size(); ++j) {
        P[j + numAssetOptions][j + numAssetOptions] = 2. * knownLiabilityRates.get(j).getFirst() * scaleUp;
        Q[j + numAssetOptions] = knownLiabilityRates.get(j).getSecond();
    }
    PDQuadraticMultivariateRealFunction objectiveFunction = new PDQuadraticMultivariateRealFunction(P, Q, 0);

    // G * x \leq h inequality constraints
    // Domain bounds
    final int numInequalityConstraints = (cashToSpend == 0.) ? dimension + 1 : dimension + 2;
    final ConvexMultivariateRealFunction[] ineqConstraints = new ConvexMultivariateRealFunction[numInequalityConstraints];
    for (int i = 0; i < numAssetOptions; ++i) {
        final double[] linearCoeffs = new double[dimension];
        linearCoeffs[i] = -1.;
        ineqConstraints[i] = new LinearMultivariateRealFunction(linearCoeffs, 0.);
    }
    for (int j = 0; j < numLiabilityOptions; ++j) {
        final double[] linearCoeffs = new double[dimension];
        linearCoeffs[j + numAssetOptions] = -1.;
        ineqConstraints[j + numAssetOptions] = new LinearMultivariateRealFunction(linearCoeffs, 0.);
    }
    // Equity constraint, including cash to spend.
    if (cashToSpend > 0.) {
        double[] equityConstraintIneqs = new double[numAssetOptions + numLiabilityOptions];
        for (int i = 0; i < numAssetOptions; ++i)
            equityConstraintIneqs[i] = +1.;
        for (int j = 0; j < numLiabilityOptions; ++j)
            equityConstraintIneqs[j + numAssetOptions] = -1.;
        ineqConstraints[dimension + 1] = new LinearMultivariateRealFunction(equityConstraintIneqs,
                -cashToSpend * scaleDown);
    }

    // Capital inequality constraint
    final double[] capitalIneqCoeffs = new double[dimension];
    double capitalIneqCoeffsScalar = 0.;
    for (int i = 0; i < numAssetOptions; ++i) {
        capitalIneqCoeffs[i] = 1.;
    }
    capitalIneqCoeffsScalar += 1.;
    ineqConstraints[dimension] = new LinearMultivariateRealFunction(capitalIneqCoeffs,
            -capitalIneqCoeffsScalar);

    OptimizationRequest session = new OptimizationRequest();
    final double[] strictlyFeasibleInitialPoint = new double[dimension];
    for (int i = 0; i < numAssetOptions; ++i)
        strictlyFeasibleInitialPoint[i] = 1.e-100 * numLiabilityOptions;
    for (int j = 0; j < numLiabilityOptions; ++j)
        strictlyFeasibleInitialPoint[j + numAssetOptions] = 1.e-100 * numAssetOptions;
    session.setInitialPoint(strictlyFeasibleInitialPoint);

    session.setF0(objectiveFunction);
    session.setFi(ineqConstraints);
    if (cashToSpend == 0.) {
        // A * x = b equality constraints
        final double[][] A = new double[1][dimension];
        for (int i = 0; i < numAssetOptions; ++i)
            A[0][i] = 1.;
        for (int j = 0; j < numLiabilityOptions; ++j)
            A[0][j + numAssetOptions] = -1.;
        final double[] b = new double[] { 0. };
        session.setA(A);
        session.setB(b);
    }
    session.setToleranceFeas(Math.max(smallestMarketImpact * 1.e-7, toleranceFeasibleSolution));
    session.setTolerance(Math.max(smallestMarketImpact * 1.e-7, solutionTolerance));
    session.setMaxIteration(numberOfIterations);
    session.setInteriorPointMethod(JOptimizer.PRIMAL_DUAL_METHOD);

    JOptimizer opt = new JOptimizer();
    opt.setOptimizationRequest(session);
    try {
        opt.optimize();
    } catch (final Exception e) {
        System.err.println("UncollateralizedReturnMaximizer: optimization failed.");
        System.err.flush();
        return null;
    }

    List<Double> solution = ArrayUtils.toArrayList(opt.getOptimizationResponse().getSolution());
    for (int i = 0; i < numAssetOptions; ++i) {
        solution.set(i, solution.get(i) * scaleUp);
    }
    for (int j = 0; j < numLiabilityOptions; ++j) {
        final int index = j + numAssetOptions;
        solution.set(index, solution.get(index) * scaleUp);
    }
    return solution;
}

From source file:ca.travelagency.invoice.Invoice.java

public void addInvoiceItem(InvoiceItem invoiceItem) {
    Validate.notNull(invoiceItem);/*  ww w. j a  v a 2  s  .c o  m*/
    invoiceItem.setInvoice(this);
    List<InvoiceItem> items = getInvoiceItems();
    if (invoiceItem.getIndex() == null) {
        invoiceItem.setIndex(items.size());
        items.add(invoiceItem);
    } else {
        items.set(invoiceItem.getIndex(), invoiceItem);
    }
    calculateTotalDue();
}

From source file:com.opengamma.analytics.math.surface.InterpolatedSurfaceMultiplicativeShiftFunction.java

/**
 * {@inheritDoc}/*  w w  w  . jav  a 2s  .c  om*/
 */
@Override
public InterpolatedDoublesSurface evaluate(final InterpolatedDoublesSurface surface, final double[] xShift,
        final double[] yShift, final double[] percentage, final String newName) {
    Validate.notNull(surface, "surface");
    Validate.notNull(xShift, "x shift");
    Validate.notNull(yShift, "y shift");
    Validate.notNull(percentage, "shifts");
    final int n = xShift.length;
    if (n == 0) {
        return InterpolatedDoublesSurface.from(surface.getXDataAsPrimitive(), surface.getYDataAsPrimitive(),
                surface.getZDataAsPrimitive(), surface.getInterpolator(), newName);
    }
    Validate.isTrue(n == yShift.length && n == percentage.length);
    final Double[] x = surface.getXData();
    final Double[] y = surface.getYData();
    final Double[] z = surface.getZData();
    final List<Double> newX = new ArrayList<>(Arrays.asList(x));
    final List<Double> newY = new ArrayList<>(Arrays.asList(y));
    final List<Double> newZ = new ArrayList<>(Arrays.asList(z));
    final int size = surface.size();
    for (int i = 0; i < n; i++) {
        boolean foundValue = false;
        for (int j = 0; j < size; j++) {
            if (Double.doubleToLongBits(x[j]) == Double.doubleToLongBits(xShift[i])
                    && Double.doubleToLongBits(y[j]) == Double.doubleToLongBits(yShift[i])) {
                newZ.set(j, z[j] * (1 + percentage[i]));
                foundValue = true;
            }
        }
        if (!foundValue) {
            newX.add(xShift[i]);
            newY.add(yShift[i]);
            newZ.add(surface.getZValue(xShift[i], yShift[i]) * (1 + percentage[i]));
        }
    }
    return InterpolatedDoublesSurface.from(newX, newY, newZ, surface.getInterpolator(), newName);
}

From source file:com.opengamma.analytics.math.surface.InterpolatedSurfaceAdditiveShiftFunction.java

/**
 * {@inheritDoc}// w  ww. j  ava  2s.  c o  m
 */
@Override
public InterpolatedDoublesSurface evaluate(final InterpolatedDoublesSurface surface, final double[] xShift,
        final double[] yShift, final double[] shift, final String newName) {
    Validate.notNull(surface, "surface");
    Validate.notNull(xShift, "x shift");
    Validate.notNull(yShift, "y shift");
    Validate.notNull(shift, "shifts");
    final int n = xShift.length;
    if (n == 0) {
        return InterpolatedDoublesSurface.from(surface.getXDataAsPrimitive(), surface.getYDataAsPrimitive(),
                surface.getZDataAsPrimitive(), surface.getInterpolator(), newName);
    }
    Validate.isTrue(n == yShift.length && n == shift.length);
    final Double[] x = surface.getXData();
    final Double[] y = surface.getYData();
    final Double[] z = surface.getZData();
    final List<Double> newX = new ArrayList<Double>(Arrays.asList(x));
    final List<Double> newY = new ArrayList<Double>(Arrays.asList(y));
    final List<Double> newZ = new ArrayList<Double>(Arrays.asList(z));
    final int size = surface.size();
    for (int i = 0; i < n; i++) {
        boolean foundValue = false;
        for (int j = 0; j < size; j++) {
            if (Double.doubleToLongBits(x[j]) == Double.doubleToLongBits(xShift[i])
                    && Double.doubleToLongBits(y[j]) == Double.doubleToLongBits(yShift[i])) {
                newZ.set(j, z[j] + shift[i]);
                foundValue = true;
            }
        }
        if (!foundValue) {
            newX.add(xShift[i]);
            newY.add(yShift[i]);
            newZ.add(surface.getZValue(xShift[i], yShift[i]) + shift[i]);
        }
    }
    return InterpolatedDoublesSurface.from(newX, newY, newZ, surface.getInterpolator(), newName);
}

From source file:org.onebusaway.webapp.actions.admin.console.ServiceAlertAffectsAction.java

public String update() throws IOException, JSONException {

    if (_id == null || _index == -1) {
        return INPUT;
    }/*from www  . j av a 2 s  .  c o m*/

    ServiceAlertBean alert = _transitDataService.getServiceAlertForId(_id);
    if (alert == null) {
        return INPUT;
    }

    List<SituationAffectsBean> allAffects = alert.getAllAffects();
    if (allAffects == null || _index >= allAffects.size()) {
        return INPUT;
    }

    _model.setAgencyId(string(_model.getAgencyId()));
    _model.setRouteId(string(_model.getRouteId()));
    _model.setDirectionId(string(_model.getDirectionId()));
    _model.setTripId(string(_model.getTripId()));
    _model.setStopId(string(_model.getStopId()));

    allAffects.set(_index, _model);
    _transitDataService.updateServiceAlert(alert);

    return SUCCESS;
}

From source file:griffon.javafx.editors.ColorPropertyEditor.java

private void handleAsList(List list) {
    List values = new ArrayList();
    values.addAll(list);//from   w w  w  .  j a  v  a 2s . co  m
    switch (list.size()) {
    case 3:
        values.add(1d);
        break;
    case 4:
        // ok
        break;
    default:
        throw illegalValue(list, Color.class);
    }
    for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
        Object val = values.get(i);
        if (val instanceof Number) {
            values.set(i, parse((Number) val));
        } else if (val instanceof CharSequence) {
            values.set(i, parse(String.valueOf(val)));
        }
    }
    super.setValueInternal(new Color((Double) values.get(0), (Double) values.get(1), (Double) values.get(2),
            (Double) values.get(3)));
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static boolean writeElementToListAt(int index, List<Object> container, Object newValue) {
    if (container == null) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("index", index);
            replace.put("value", newValue);
            L.debug(Strings.substitute(R.getString("D-UTIL#0007"), replace));
        }/*from w w  w . j  a v a  2s  .co  m*/
        return false;
    }
    if (container.size() <= index) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("index", index);
            replace.put("value", newValue);
            replace.put("size", container.size());
            L.debug(Strings.substitute(R.getString("D-UTIL#0008"), replace));
        }
        return false;
    }
    container.set(index, newValue);
    return true;
}

From source file:griffon.swing.editors.ColorPropertyEditor.java

private void handleAsList(List list) {
    List values = new ArrayList();
    values.addAll(list);// w ww. j  a v  a  2s. c o m
    switch (list.size()) {
    case 3:
        values.add(255);
        break;
    case 4:
        // ok
        break;
    default:
        throw illegalValue(list, Color.class);
    }
    for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
        Object val = values.get(i);
        if (val instanceof Number) {
            values.set(i, parse((Number) val));
        } else if (val instanceof CharSequence) {
            values.set(i, parse(String.valueOf(val)));
        }
    }
    super.setValueInternal(new Color((Integer) values.get(0), (Integer) values.get(1), (Integer) values.get(2),
            (Integer) values.get(3)));
}

From source file:fr.eolya.utils.http.HttpUtils.java

public static List<String> extractAbsoluteLinks(String rawPage, String urlPage, int depth) throws IOException {

    List<String> links = extractLinks(rawPage, depth);
    String baseHref = null;/*  ww w. j  a v  a 2  s.  c o m*/

    for (int i = 0; i < links.size(); i++) {
        try {
            String url = null;
            if (baseHref == null && isRelativeURL(links.get(i).trim()))
                baseHref = getBaseHref(rawPage);
            if (baseHref != null && isRelativeURL(links.get(i).trim())) {
                url = urlGetAbsoluteURL(baseHref, links.get(i).trim());
            } else {
                url = urlGetAbsoluteURL(urlPage, links.get(i).trim());
            }
            links.set(i, url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return links;
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration.java

/**
 * Updates an existing endpoint (or adds if missing).
 *
 * @param endpoint the endpoint to update.
 *///  w  w  w.ja  va  2  s.  c o  m
public synchronized void updateEndpoint(@NonNull AbstractBitbucketEndpoint endpoint) {
    List<AbstractBitbucketEndpoint> endpoints = new ArrayList<>(getEndpoints());
    boolean found = false;
    for (int i = 0; i < endpoints.size(); i++) {
        AbstractBitbucketEndpoint ep = endpoints.get(i);
        if (ep.getServerUrl().equals(endpoint.getServerUrl())) {
            endpoints.set(i, endpoint);
            found = true;
            break;
        }
    }
    if (!found) {
        endpoints.add(endpoint);
    }
    setEndpoints(endpoints);
}