• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

A bug in a simple code/where is it ?

max222

Lurker
I cannot find a bug in the following application for Android Studio.
We have a set of players
{1,...,n} and a valuation from the set of all subsets to
{0,1}. The apllication is supposed to read numbers labeled with binary strings of length n
corresponding to coalitions S (1 indicates that this player is in S).
And then it should compute how many S have the property that v(S)=1 and v(S-{i})=0 for all
players i in {1,...,n}. I have this code but it has a bug in it.


00=v({0,0})=0
01=v({0,1})=0
10=v({1,0})=0
11=v({1,1})=1

yields 0 instead of 1 (corresponding to {1,1}). The number of all such S for all 's togeather is called "swingove" in the code.
It should be swingove=1, but the app gives swingove=0. Below I attach MainActivity.java i aktivyty_Main.xml.

To make clear the flow of the application, button4 increases by 1 the number of coalitions (subsets of {1,...,n})
button2 generates the editText areas shap is the result for the shapley value (not ineteresting now)
and banzhaf which should after "K" print the number swingove.

I will explain any part of the code if necessary.


Code:
package com.example.tablelayout6;

import androidx.appcompat.app.AppCompatActivity;

import android.icu.text.MeasureFormat;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TableRow;
import android.widget.TabWidget;

import android.widget.TextView;
import android.widget.TableLayout;

import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import java.lang.StringBuilder;

public class MainActivity extends AppCompatActivity {
public Integer N=0;
public String a="2";
private EditText editText1;
private EditText linear;
private Integer[] hodnoty =new Integer[10];
private String[] koal=new String[10];
private float[] shap=new float[10];

   [USER=1021285]@override[/USER]
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);


   }

   public boolean iswinning(Integer J,int ii) {
return true;

   }

   public Integer fact (Integer f) {
       if (f==0) return 1;
else return f*fact((Integer) (f-1));
   }

   //T pocetHracu


   public Integer[] stringToArray(String binary) {
       Integer[] arrayOfPLayers=new Integer[binary.length()];
        for(int i=0;i<binary.length();i++) {
            arrayOfPLayers=Integer.parseInt("" + binary.charAt(i));

        }
        return arrayOfPLayers;
   }

   //N je pocet koalici
   public void click5(View view) {
       int pocetHracu=Integer.toBinaryString(N-1).length();
       Integer[][] koalice=new Integer[N][pocetHracu];
       Integer[][] koalice2=new Integer[N][pocetHracu];
       Integer[][] koalice3=new Integer[N][pocetHracu];

       TextView tv6 = (TextView) findViewById(R.id.textView6);

       for (Integer ith=0;ith<N;ith++) {
           for(Integer T=0;T<pocetHracu;T++) {
             String StringPadded=stf(Integer.toBinaryString(ith),pocetHracu-Integer.toBinaryString(ith).length());
             koalice[ith][T]=stringToArray(StringPadded)[T];
             koalice2[ith][T]=stringToArray(StringPadded)[T];
             koalice3[ith][T]=stringToArray(StringPadded)[T];
           }
       }
       float[] shap=new float[pocetHracu];
       float[] vyhryKoalici = new float[N];
       Integer[] isSwing=new Integer[pocetHracu];

       for (int i=0; i<N;i++) {
           editText1 = (EditText) findViewById(330+i);
           String a=editText1.getText().toString();
           float f1 = Float.parseFloat(a);
        vyhryKoalici=f1;
       }

       Integer[] kolikJeSwingu=  new Integer[pocetHracu];




String S="";
       Integer jth;
       for (int p = 0; p < pocetHracu; p++) {
           isSwing[p] =0;
           kolikJeSwingu[p]=0;
       }

       for(Integer T=0;T<pocetHracu;T++) {
         for (Integer ith = 0; ith < N; ith++) {

             int bool = 1;

//vyhryKoalici[ith];
//musis najit jth<N pro ktere koalice3[jth]=S-ith
             if (koalice[ith][T] == 1 && vyhryKoalici[ith]==1) {
                 koalice3[ith][T] = 0;
                 for (jth = 0; jth < N; jth++) {

                     for (int k = 0; k < pocetHracu; k++) {
                         if (k != T && koalice[ith][k] != koalice3[jth][k]) {
                             bool = 0;
                         }
                     }

                     if (vyhryKoalici[jth] == 0 && bool == 1) {
                         isSwing[T]++;
                     }
                 }
                 koalice3[ith][T]=1;
             }

         }
       }

//kolikJeSwingu(Integer TmaSwing, Integer pocetHracu, Integer pocetKoalici,
//                                   Integer[][] koal, Integer[] koalicePriKtereJeSwing, float[] vyhryKoal)

           for (Integer ith = 0; ith < N; ith++) {
               for(Integer T=0;T<pocetHracu;T++) {
//isSwingBool(Integer kteraKoalice, Integer TmaSwing, Integer pocetHracu, Integer pocetKoalici,
//                               Integer[][] koal, Integer[] koalicePriKtereJeSwing, float[] vyhryKoal) {

                //   kolikJeSwingu[T]+=kolikJeSwingu(T, pocetHracu, N,
                 //          koalice, koalice[ith],vyhryKoalici);


                   if (koalice[ith][T] == 1) {
                       koalice2[ith][T] = 0;
                       for (jth = 0; jth < N; jth++) {
                           int bool = 1;
                           for (int k = 0; k < pocetHracu; k++) {
                               if (k != T && koalice[ith][k] != koalice2[jth][k]) {
                                   bool = 0;
                               }
                           }

                           int bool2=0;
                           koalice2[ith][T] = 1;

                           if (vyhryKoalici[ith] == 1 && vyhryKoalici[jth] == 0) {
                               for (int k = 0; k < pocetHracu; k++) {
                                   if (koalice[ith][k] != koalice2[jth][k]) {
                                       bool2++;
                                   }
                               }

                           //    if(bool2==1) {
                            //       isSwing[T]++;
                            //   }

                           }
                           koalice2[ith][T] = 1;
                           Integer t = 0;
                           for (int p = 0; p < pocetHracu; p++) {
                               t += koalice[ith][p];
                           }
                           // Integer[] isSwing=new Integer[pocetHracu];

                           if (bool == 1) {
                               shap[T] += (float) ((float) fact(t - 1) * fact(pocetHracu - t) / (float) (fact(pocetHracu)) * (float) (vyhryKoalici[ith] - vyhryKoalici[jth]));
                           }
                       }
                   }

           }
       }


S+="here";
       String T2="";
       T2+="banzhaf";

       Integer swingove=0;

for(int p=0;p<pocetHracu;p++) {
   swingove+=isSwing[p];

}

       String strPokus="banzhaf ";

float[] banzh=new float[pocetHracu];
for(int p=0;p<pocetHracu;p++)
{
   banzh[p]=(float) isSwing[p]/(float) swingove;

}


        String strPokus2="shap ";

       for(int i=0;i<pocetHracu;i++) {
        //   S+=(" "+String.valueOf(shap));
           strPokus2+=String.format("%.2f ",shap);
           strPokus+=String.format("%.2f ",banzh);
       }


//      tv6.setText(strPokus2+"\n"+strPokus+"\nswing"+isSwing[0]+"swingPrvni:"+isSwing[1]+"\nH"+swingove);

       tv6.setText(strPokus2+"\n"+strPokus+"K"+String.valueOf(swingove));
   }

   public void click4(View view) {
   //
       linear = (EditText) findViewById(R.id.simpleEditText);
if(N==0) {
   linear.setText("0");
}

   String     a = linear.getText().toString();
      N = Integer.parseInt(a);
      N++;
    linear.setText(N.toString());


   }

   public  String stf(String a,Integer l) {
       if(l==0) { return a; }
       l--;
       return stf("0"+a,l);

   }
   public void click2(View view) {
       String col1;
       String col2;
     //  String playerChanged;

       TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);

       EditText editText = (EditText)findViewById(R.id.simpleEditText);


       TableRow row = new TableRow(this);
       TextView tv = new TextView(this);
       TextView c = new TextView(this);

       //EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
  //  a = editText.getText().toString();

    tv.setId(202);




       tv.setText("This is text");
//
       tl.addView(row);

       row.addView(tv);

int sf=Integer.toBinaryString(N-1).length();
       for (int x = 0; x < N; x++) {
           //String.format("%010d",(
           String jl=Integer.toBinaryString(x);
           String jl2=stf(jl,sf-jl.length());
koal[x]=jl2;
           col1 = "(" + x + ")"+jl2;
          // col1 = "(" + x + ")"+Integer.toBinaryString(x);
           col2 = "1";

           //col3 = "(" + x + ") Column 3";
           //col4 = "(" + x + ") Column 4";

           TableRow newRow = new TableRow(this);

           TextView column1 = new TextView(this);
           TextView column2 = new TextView(this);
           EditText editText1 = new EditText(this);

          editText1.setId(330+x);
          // String stringAnswer = editText1.getText().toString();

           TextView column3 = new TextView(this);
          // TextView column4 = new TextView(this);
           editText1.setText("0        ");
           column1.setText(col1);
           column1.setText(col1);
           column2.setText(col2);
           //column3.setText(col3);
           //column4.setText(col4);

          // column1.setText(stringAnswer);

           newRow.addView(column1);
           newRow.addView(editText1);





            newRow.addView(column3);
          // newRow.addView(column4);

           tl.addView(newRow, new TableLayout.LayoutParams());
       }




   }


   }

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:android2="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   android:layout_columnSpan="12"
   android:layout_columnWeight="12"
   android:accessibilityLiveRegion="none"
   android:eek:rientation="vertical">

   <TableLayout
       android:id="@+id/tableLayout1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"></TableLayout>

   <Button
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:eek:nClick="click2"
       android:text="Button2" />


   <EditText
       android:id="@+id/simpleEditText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:digits="10"
       android:hint="Enter Your Name Here"
       android:inputType="text" />

   <Button
       android:id="@+id/button4"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:eek:nClick="click4"
       android:text="Button4" />

   <Button
       android:id="@+id/button5"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:eek:nClick="click5"
       android:text="Button5" />

   <TextView
       android:id="@+id/textView6"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Shapley1" />


</LinearLayout>
 
Last edited by a moderator:
I moved this to the development area and added code tags for readability. I hope you get this sorted. Cheers!! :)
 
Back
Top Bottom