@Override
	public void train(List<Instance> instances) {
		// calculate the Gram Matrix.
		// java List maintains iterator order.
		this.instances = instances; 
		int index1 = 0;
		int index2 = 0;
		labels = new double[instances.size()];
		dualParam = new double[instances.size()];
		gramMatrix = new double[instances.size()][instances.size()];
		for (Instance i1 : instances) {
			labels[index1] = 2 * (Double.valueOf(i1.getLabel().toString()) - 0.5); // label values
			dualParam[index1] = 0; // alpha values
			index2 = 0;
			for (Instance i2 : instances) {
//				String key = String.valueOf(index1) + '_' + String.valueOf(index2);
//				gramMatrix.put(key, kernel.value(i1, i2));
				gramMatrix[index1][index2] = kernel.value(i1, i2);
				index2 ++;
			}
			index1 ++;
		}
		// start tuning param
		for (int iter = 0 ; iter < iters ; iter++) {
			for (int i = 0 ; i < instances.size() ; i++) {
				double yE = 0.0;
				for (int j = 0 ; j < instances.size() ; j++) {
					yE += dualParam[j] * labels[j] * gramMatrix[i][j];
				}
				if (yE * labels[i] < 1) {
					dualParam[i] += 1;
				}
			}
		}
	}