需要关于如何实现遗传算法类型应用的建议



我是java的新手,似乎无法解决问题。我试图获取两个股票信息数组,并将它们相互比较(只保留两个数组中出现的信息)。我读了一些关于通用算法的文章,如果匹配,我希望能够创建类来为每个数组集分配适合度分数。我的代码实际上不起作用(我可以让它分析数组的每个单独的组件,但不能分析我想要的范围)。为了澄清问题,这里有一个我的数据样本:

ID   date   Ticker  Shares
    1   2011-06-19  goog    0
    1   2011-06-19  ibm 0
    1   2011-06-19  gs  0
    1   2011-06-19  msft    0
    1   2011-06-19  c   5
    2   2011-06-19  goog    0
    2   2011-06-19  ibm 0
    2   2011-06-19  gs  0
    2   2011-06-19  msft    1
    2   2011-06-19  c   4
    3   2011-06-19  goog    0
    3   2011-06-19  ibm 0
    3   2011-06-19  gs  0
    3   2011-06-19  msft    2
    3   2011-06-19  c   3
    4   2011-06-19  goog    0
    4   2011-06-19  ibm 0
    4   2011-06-19  gs  0
    4   2011-06-19  msft    3
    4   2011-06-19  c   2
    5   2011-06-19  goog    0
    5   2011-06-19  ibm 0
    5   2011-06-19  gs  0
    5   2011-06-19  msft    4
    5   2011-06-19  c   1 

依此类推,我有一个这个和另一个的数组,用于前一个日期。我希望能够将两者进行比较(按id分组),并找到完整的匹配项。但以后,我希望能够通过其他课程对成功的比赛进行分析。我认为第一步是确定匹配项。这是我的代码(它只识别一个匹配的股票代码,我不知道如何使它与整个ID集匹配):

public void compare(int firstindex, int lastIndex, Object date1, ArrayList data1id, ArrayList data1ticker, ArrayList data1shares, ArrayList data1price, Object date2,  ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
    ArrayList ticker = new ArrayList(); 
    ArrayList shares = new ArrayList(); 
    ArrayList price = new ArrayList(); 
    while (firstindex < lastIndex) {
        //System.out.print("date is " + date1);
        ticker.add(data1ticker.get(firstindex));
        shares.add(data1shares.get(firstindex));
        price.add(data1price.get(firstindex));
        firstindex++;
    }
    comparewithsecondarray(ticker, shares, price, date2, data2id, data2ticker, data2shares, data2price);
    //System.out.println("***********");
}
public void comparewithsecondarray(ArrayList tickerarray, ArrayList sharesarray, ArrayList pricearray, Object date2,  ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
//get the total number of values in the array
int totalArrayList = tickerarray.size();
int counter= 0;
        System.out.println("Array has been checked against second array and we're on " + counter);
        System.out.println(tickerarray);
        System.out.println(sharesarray);
        System.out.println("+++++++");
    while (counter < totalArrayList) {
        Object ticker = tickerarray.get(counter);
        Object shares = sharesarray.get(counter);
        Object price = pricearray.get(counter);

        loadSecondArray(ticker, shares, price, date2, data2id, data2ticker, data2shares, data2price);
        counter++;
    }
}
public void loadSecondArray(Object ticker, Object shares, Object price, Object date2,  ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
    //System.out.println("ticker " + ticker);
    //System.out.println("shares " + shares);
    //System.out.println("price " + price);
    //find the last number of the arrray
    if (!data2id.isEmpty()) {
        int counter2 = Integer.parseInt(data2id.get(data2id.size()-1).toString());
        //System.out.println("last element in array2 is " + counter2);
    }        
    //location is the id number we're looking for.
    int location = 1;
    while (location > counter2) {
        boolean blnFound = data2id.contains(location);
        //System.out.println("Does arrayList contain " + location + "? " + blnFound);
        if (blnFound) {
            if(firstindex == -1) {
                //System.out.println("ArrayList does not contain " + location);
            } else {
                //System.out.println("ArrayList contains " + location  + " at index :" + firstindex);
                int firstindex = data2id.indexOf(location);
                int lastIndex = data2id.lastIndexOf(location);
                //send ranges to study
                while (firstindex < lastIndex) {
                    //System.out.print("date is " + date1);
                    Object ticker2 = data2ticker.get(firstindex);
                    Object shares2= data2shares.get(firstindex);
                    Object price2 = data2price.get(firstindex);
                    if (ticker.equals(ticker2) &&  shares.equals(shares2)) {
                        System.out.println("We have a match!");
                        System.out.println(ticker);
                        System.out.println(ticker2);
                        System.out.println(shares);
                        System.out.println(shares2);
                        System.out.println("*****");
                    }
                    //add to the counter
                    firstindex++;
                }
                location++;
            }

        } else {
            break;
        }
    }

提前为代码的质量感到抱歉,我还是个新手,还在学习。我认为第一步是识别匹配,然后有一种方法将这些匹配(我想是数组列表)传递给其他类进行分析。

任何关于如何实现我这个项目目标的建议都会很好(我正在读一本关于遗传算法的书,但有点难理解,所以我开始复习我在互联网上能找到的所有代码,以了解它是如何实现的)。

提前感谢

我想你可能需要这样的东西:

import java.util.Calendar;
//class representing all your data
public class StockData implements Comparable<StockData>{
    private int id;
    private Calendar date;
    private List<ShareBean> shares;
//this will return whichever StockData that has more total shares as being greater
@Override
public int compareTo(StockData arg0) {
    int totalshares = 0;
    int totalshares2 = 0;
    for(ShareBean share: shares)
        totalshares+=share.getShares();
    for(ShareBean share: arg0.getShares())
        totalshares2+=share.getShares();
    return totalshares-totalshares2;
}
    //this method is used to see if another StockData object has the same id
    @Override
    public boolean equals(Object arg0) {
        try {
        StockData arg1 = (StockData) arg0;
        if (id == arg1.id)
            return true;
        } catch (Exception e) {
        return false;
    }
    return false;
    }
    public void setDate(Calendar date) {
        this.date = date;
    }
    public Calendar getDate() {
    return date;
    }
    public void setId(int id) {
    this.id = id;
        }
        public int getId() {
            return id;
    }

    public void setShares(List<ShareBean> shares) {
    this.shares = shares;
    }
    public List<ShareBean> getShares() {
    return shares;
    }
public String toString(){
    String toReturn = "";
    toReturn+="ID: "+id+"n";
    toReturn+="Date: "+date.getTime()+"n";
    for(ShareBean share: shares)
        toReturn+="Shares: "+share.toString()+"n";
    return toReturn;
}
}

使用它,您只需为您拥有的每个数据块创建一个StockData对象,并将其添加到此类对象的数组中。然后,如果您想知道它们是否相同,只需使用StockData的.equals(Object arg0)方法,并将其与另一个StockData对象进行比较。

例如:

//this method compares to Lists of StockData, and returns a list containing all
//the StockData objects that had matches
public List<StockData> comparewithsecondarray(List<StockData> StockData1, List<StockData> StockData2) {
List<StockData> list = new ArrayList<StockData>();
for(StockData sd1: StockData1){
   for(StockData sd2: StockData2){
      if(sd1.equals(sd2)){
         //found a match!  add it to the list
         list.add(sd1);
         //break so we don't add the same object multiple times
         break;
      }
   }
}
return list;
}

看起来你真的让这件事比它需要的要复杂得多。如果你转发你特别想做的事情,这会让回答你的问题变得更容易。

编辑:我修改了我的StockData类,并添加了另一个类来跟踪股票:

公共类ShareBean{私人字符串股票行情;私人int股;

public ShareBean(String ticker, int shares){
    this.ticker = ticker;
    this.shares = shares;
}
public void setTicker(String ticker) {
    this.ticker = ticker;
}
public String getTicker() {
    return ticker;
}
public void setShares(int shares) {
    this.shares = shares;
}
public int getShares() {
    return shares;
}

public String toString(){
    String toReturn = "";
    toReturn+="Ticker: "+ticker+", Shares: "+shares;
    return toReturn;
}
}

另一个编辑:

把这个主要方法放在某个地方。。。这其实并不重要。

public static void main(String[] args) {
    List<StockData> listSD1 = new ArrayList<StockData>();
    List<StockData> listSD2 = new ArrayList<StockData>();
    StockData sd1 = new StockData();
    StockData sd2 = new StockData();
    List<ShareBean> listShares1 = new ArrayList<ShareBean>();
    List<ShareBean> listShares2 = new ArrayList<ShareBean>();
    //create the shares for sd1
    listShares1.add(new ShareBean("goog", 3));
    listShares1.add(new ShareBean("ibm", 5));
    listShares1.add(new ShareBean("gs", 0));
    listShares1.add(new ShareBean("msft", 0));
    listShares1.add(new ShareBean("c", 1));
    //create the shares for sd2
    listShares2.add(new ShareBean("goog", 0));
    listShares2.add(new ShareBean("ibm", 1));
    listShares2.add(new ShareBean("gs", 3));
    listShares2.add(new ShareBean("msft", 0));
    listShares2.add(new ShareBean("c", 5));
    //set their ids
    sd1.setId(1);
    sd2.setId(2);
    //set the dates (using calendars)
    sd1.setDate(Calendar.getInstance());
    sd2.setDate(Calendar.getInstance());
    //and finally set the shares
    sd1.setShares(listShares1);
    sd2.setShares(listShares2);
    //now add each object to each list.  the lists will be exacly the same
    listSD1.add(sd1);
    listSD1.add(sd2);
    listSD2.add(sd1);
    listSD2.add(sd2);
    //now the lists are ready, and we can compare them
    //I put the comparewithsecondarray method in the StockData class, but it could go anywhere
    //I also overrode the "toString" method to make the output more readable (in both StockData and ShareBean)
    System.out.println(Arrays.toString(sd1.comparewithsecondarray(listSD1, listSD2).toArray()));
}

最新更新