java - 需要有关如何实现遗传算法类型应用程序的建议

标签 java algorithm

我是 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;
}
}

使用它,您只需为您拥有的每个数据 block 创建一个 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 类,并添加了另一个类来跟踪股票:

公共(public)类 ShareBean { 私有(private)字符串代码; 私有(private)股;

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;
}
}

另一个编辑:

把这个 main 方法放在某个地方......这并不重要。

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()));
}

关于java - 需要有关如何实现遗传算法类型应用程序的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6443671/

相关文章:

java - 退出后避免返回页面渲染

java - 如何改进 : Given two integers, 返回它们共享的位数

c - 如果程序在 2 秒内执行 n=10,执行 n=100 需要多少时间?

algorithm - 如何让波形渲染更有趣?

java - 网页响应时间较长,第二次尝试后恢复正常

java - 检查文件结尾未按预期工作

java - ScrollView - 如何在 ScrollView 中启用水平滚动

java - 构建 A* 算法的问题

java - 使用插入排序、双向链表对字符串数组索引进行排序

java - 将数组的所有元素向右移动