1

ユーザーがアプリケーション (Android Studio) に入力するArrayList文字列要素があります。MainActivityといいArrayList<String> serviceNamesます。サービス名は、Facebook、Fitness App、Clock App などのようなものにすることができます。Swarm Particle アルゴリズムを使用するフィットネス関数に取り組んでいるため、サービスのこれらのユーザー入力のそれぞれに整数値を割り当てたいと考えています。

public class CustomServiceSelection implements Goodness {

    public static final int numOfServices = MainActivity.servicenames.size();
    public static final int NUM_DIMENSIONS = 2 + numOfServices;
    public static final int DATA = 0;
    public static final int WLAN = 1; //get the names of the services from the arraylist
    //then convert them into an integer for the bits/no_dimensions


    boolean alwaysOn = MainActivity.costUTILITY.contains(100.0);


    ArrayList<String> serviceNames = MainActivity.servicenames;
    ArrayList<Double> costData = MainActivity.costDATA;
    ArrayList<Double> costWlan = MainActivity.costWLAN;
    ArrayList<Double> costUtilities = MainActivity.costUTILITY;
    private double batteryCost;
    //ArrayList<Double> battery = MainActivity.costBattery;

    public CustomServiceSelection(double batteryCost, ArrayList<Double> costData, ArrayList<Double> costWlan,
                                  ArrayList<Double> costUtilities) {
        if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
            throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
        }
        this.batteryCost = batteryCost; //make sure you add battery field to UI, user enters battery level
        this.costData = costData;
        this.costWlan = costWlan;
        this.costUtilities = costUtilities;
        //this.services = services;
    }

    public double getGoodness(boolean[] bits) {
        double utility = 0.0;
        double rcost = 0.0;
        ArrayList<Double> resourceCost = new ArrayList<Double>();

        for(Double i : costData){

        }

        for (int x = 0; x <= NUM_DIMENSIONS; x++) { //for each resource that is a bit
            if (bits[x] == alwaysOn) {  //if a utility cost of a service is 100, return -2000.0
                return -2000;
            }

            if (bits[DATA] && bits[WLAN]) {
                return -500;
            }
            if (!bits[DATA] || bits[WLAN]) {
                return -450; //particle goodness always returns this??
            }
            if (bits[DATA]) {
                resourceCost = costData;
            } else if (bits[WLAN]) {
                resourceCost = costWlan;
            }

            for (int i = 0; i <= NUM_DIMENSIONS; i++) { //for each service the user enters
                if (bits[i]) {
                    utility += costUtilities.get(i);
                    rcost += resourceCost.get(i);
                }
            }
            if (rcost < batteryCost) {
                return utility;
            }


        }
        return utility * 0.50;
    }
    }

num_dimensions を 2 (WLAN と DATA) に加えて、ユーザーがメイン アクティビティで入力するサービスの数にしたいと考えています。したがって、4 つのサービスを入力すると、合計で 6 つのディメンションになります。次に、これらのサービスに「int」値を次のように割り当てたいと思います...

   public static final int Facebook = 2;
   public static final int Twitter = 3;
   public static final int ClockApp = 4;

それが完了したら、これらの値を次元数のビットとして受け取る関数に渡したいgetGoodness()ので、Facebook は次元の 3 番目のビットになります。どんな助けやアドバイスも素晴らしいでしょう、私はこれをプロジェクトとして引き受けている大学の学生です。

編集: getGoodness() 関数にハッシュマップを実装しました。私のコードは次のようになります...

    public double getGoodness(boolean[] bits) {
    double utility = 0.0;
    double rcost = 0.0;
    ArrayList<Double> resourceCost = new ArrayList<Double>();

    for(int i = 2; i <= NUM_DIMENSIONS; i++) { //hashmap that stores the   service names with an int value
        for(int k = 0; k <= numOfServices; k++) {
            serviceMap.put(i, serviceNames.get(k));
        }

NUM_DIMENSIONS の最初の 2 ビットを WLAN と DATA にする必要があります。したがって、ユーザーが入力するサービスごとに、それらに割り当てられる整数値は 2 から 1 ずつ増加します。つまり、(2, Facebook)、(3, Twitter)、(4, Uber) などです。次の問題は、これらを getGoodness() 関数に渡します。各サービスの 0 ~ 100 のユーティリティ (優先度) コストを取得するメイン UI アクティビティから配列リストがあります。これは costUtilities と呼ばれ、80.0 ~ 100.0 (double) のユーティリティを含むビットが有効でない場合は -2000 の値を返すようにします。これについてご協力いただければ幸いです。そうでない場合は、これまでのご支援に感謝し、PSO に関する最終年度の個人プロジェクトでの幸運を祈っています :)

4

2 に答える 2