package cn.ict.zyq.bestConf.COMT2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import weka.classifiers.trees.m5.PreConstructedLinearModel;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;

import com.google.common.collect.Range;

/**
 * Branch2
 *
 */

public class Branch2 {
	
	private HashMap<Attribute,Range<Double>> rangeMap;		//ranges of the attributes in the M5P tree 属?区间
	private ArrayList<PreConstructedLinearModel> linearModelList; 		//linearModels of one leaf 线?模型
	
	public Branch2() {
		rangeMap = new HashMap<Attribute,Range<Double>>();	
	}

	public Branch2(HashMap<Attribute, Range<Double>> rangeMap, PreConstructedLinearModel linearModel) {
		this.rangeMap = rangeMap;
		this.linearModelList = new ArrayList<PreConstructedLinearModel>();
		this.linearModelList.add(linearModel);
	}
	
	public Branch2(HashMap<Attribute, Range<Double>> rangeMap, ArrayList<PreConstructedLinearModel> linearModels) {
		this.rangeMap = rangeMap;
		this.linearModelList = linearModels;
	}
	
	public Instance maxPoint(Instances dataset) throws Exception{
		Instance max = new DenseInstance(dataset.numAttributes());
		max.setDataset(dataset);
		
		double[] combinedCoefs = null;
		int len=0;
		for(PreConstructedLinearModel model : linearModelList){
			//initialization
			if(combinedCoefs==null){
				len = model.coefficients().length;
				combinedCoefs = new double[len];
				for(int i=0;i<len;i++)
					combinedCoefs[i]=0;
			}
			
			for(int i=0;i<len;i++)
				combinedCoefs[i]+=model.coefficients()[i];
		}
		
		//the max value is obtained at ends of a range
		for(Map.Entry<Attribute, Range<Double>> ent: rangeMap.entrySet()){
			int attIdx = ent.getKey().index();
			if(combinedCoefs[attIdx]>0){
				//use the upper bound
				if(ent.getValue().hasUpperBound())
					max.setValue(attIdx, ent.getValue().upperEndpoint());
			}else if(combinedCoefs[attIdx]<0){
				//use the lower bound
				if(ent.getValue().hasLowerBound())
					max.setValue(attIdx, ent.getValue().lowerEndpoint());
			}
		}
		
		//now we set the predicted values
		double y = 0;
		for(PreConstructedLinearModel model : linearModelList){
			y += model.classifyInstance(max);
		}
		y /= linearModelList.size();
		max.setClassValue(y);
		
		return max;
	}

	public HashMap<Attribute, Range<Double>> getRangeMap() {
		return rangeMap;
	}

	public void setRangeMap(HashMap<Attribute, Range<Double>> rangeMap) {
		this.rangeMap = rangeMap;
	}

	public ArrayList<PreConstructedLinearModel> getLinearModels() {
		return linearModelList;
	}

	public void setLinearModels(ArrayList<PreConstructedLinearModel> linearModel) {
		this.linearModelList = linearModel;
	}

}

@@ -1,8 +1,816 @@
+/**%0D%0A * Copyright (c) 2017 Institute of Computing Technology, Chinese Academy of Sciences, 2017 %0D%0A * Institute of Computing Technology, Chinese Academy of Sciences contributors. All rights reserved.%0D%0A * %0D%0A * Licensed under the Apache License, Version 2.0 (the %22License%22); you%0D%0A * may not use this file except in compliance with the License. You%0D%0A * may obtain a copy of the License at%0D%0A * %0D%0A * http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or%0D%0A * implied. See the License for the specific language governing%0D%0A * permissions and limitations under the License. See accompanying%0D%0A * LICENSE file.%0D%0A */%0D%0A
 package 
@@ -1300,12 +1300,19 @@
 ree 
-%E5%B1%9E?%E5%8C%BA%E9%97%B4
+%C3%A5%C2%B1%C5%BE%C3%A6?%C3%A5%C5%92%C2%BA%C3%A9%E2%80%94%C2%B4
 %0D%0A%09p
@@ -1405,12 +1405,19 @@
 eaf 
-%E7%BA%BF?%E6%A8%A1%E5%9E%8B
+%C3%A7%C2%BA%C2%BF%C3%A6?%C3%A6%C2%A8%C2%A1%C3%A5%C5%BE%E2%80%B9
 %0D%0A%09%0D

Update Branch2.java