Analytic Hierarchy process (AHP) Android implementation Tutorial

in #utopian-io6 years ago (edited)

Repository

https://github.com/Godwyyn

What Will I Learn?

  • How to implement Saaty AHP in android
  • Computing vector criteria weight
  • Matrix Computation
  • Ranking of the options

Requirements

For this tutorial, you will need the following:

  • A laptop with any operating system such as Windows OS, Mac OSX and Linux
  • Android studio
  • Must have read about Saaty Analytic Hierarchy Process(AHP) for decision making
  • Prior knowledge on Vector and Matrix Calculation
  • Knowledge of Java, XML

Difficulty

  • Advanced

Tutorial Contents

Saaty's Analytic Hierarchy Process (AHP) is a structured technique for organizing and analyzing complex decisions, based on mathematics and psychology. It aids the decision maker to set priorities on different factors and make the best decision. By reducing complex decisions to a series of pairwise comparisons, and then synthesizing the result, AHP helps to capture both subjective and objective aspects of a decision.

Screenshot_20180705-012808.png

Step 1: Develop a model for the decision

In AHP we break down the decision into a hierarchy of goals, Criteria and alternatives.
First of all we need an interface where the user can input factors or criteria being considered. So we create a dashboard to hold the Adapters , AlternativeAdapter and CriterionAdapter which will considt of the list of alternatives and criteria under consideration. We give the user the option to add or delete an alternative/criterion to fit his/her study.

DashboardFragment

Screenshot_20180705-012819.png


    private AhpDashboardContract.Presenter mPresenter;
    private AhpDashboardFragBinding mAhpDashboardFragBinding;
    private AhpDashboardAlternativeAdapter mAhpDashboardAlternativeAdapter;
    private AhpDashboardCriterionAdapter mAhpDashboardCriterionAdapter;
    private Dialog addCriterionDialog;
    private Dialog addAlternativeDialog;
    private AhpDashboardAddCriterionDialogBinding mAhpDashboardAddCriterionDialogBinding;
    private AhpDashboardAddAlternativeDialogBinding mAhpDashboardAddAlternativeDialogBinding;
    private AhpResultsDialog mAhpResultsDialog;
    public AhpDashboardFragment() {}

    public static AhpDashboardFragment newInstance() {
        return new AhpDashboardFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        List<String> criterionList = new ArrayList<>();
        List<String> alternativeList= new ArrayList<>();
        for (int x=0; x<4;x++){
            String criterion = "Criterion "+x;
            String alternative = "Alternative "+x;
            criterionList.add(criterion);
            alternativeList.add(alternative);
        }

        mAhpDashboardAlternativeAdapter = new AhpDashboardAlternativeAdapter( new ArrayList<String>(0),getContext(),this);
        mAhpDashboardCriterionAdapter = new AhpDashboardCriterionAdapter( new ArrayList<String>(0),getContext(),this);

        mAhpDashboardAlternativeAdapter.replaceData(alternativeList);
        mAhpDashboardCriterionAdapter.replaceData(criterionList);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode == RESULT_OK) {

            float [][] preferenceMatrix = (float[][]) data.getExtras().getSerializable("preferenceMatrix");
            float [] averagePriorityMatrix = (float[]) data.getExtras().getSerializable("averagePriorityMatrix");
            mAhpResultsDialog.showResults(averagePriorityMatrix,preferenceMatrix,mAhpDashboardAlternativeAdapter.getAlternatives(),mAhpDashboardCriterionAdapter.getCriterions());

            System.out.println("Matrix of preference");
            imprimeMatriz(preferenceMatrix);
        }
    }

    private void imprimeMatriz(float [][] criterion1matrix3){
        for(float[] c : criterion1matrix3){

            for(float elemento : c)
                System.out.printf(" %.2f ", elemento);
            System.out.printf("\n");
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mAhpDashboardFragBinding = DataBindingUtil.inflate(inflater, R.layout.ahp_dashboard_frag,container,false);
        mAhpDashboardFragBinding.setHandler(this);
        mAhpResultsDialog = new AhpResultsDialog(getContext(),this);

        if (mAhpDashboardFragBinding.ahpDashboardToolbar != null){
            ((AppCompatActivity) getActivity()).setSupportActionBar(mAhpDashboardFragBinding.ahpDashboardToolbar);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
            mAhpDashboardFragBinding.ahpDashboardToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_left));
            mAhpDashboardFragBinding.ahpDashboardToolbar.setNavigationOnClickListener(View ->{
                getActivity().onBackPressed();
            });
        }

        RecyclerView.LayoutManager layout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        RecyclerView.LayoutManager layout2 = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        mAhpDashboardFragBinding.ahpDashboardAlternativesListRecyclerView.setLayoutManager(layout);
        mAhpDashboardFragBinding.ahpDashboardAlternativesListRecyclerView.setNestedScrollingEnabled(false);
        mAhpDashboardFragBinding.ahpDashboardAlternativesListRecyclerView.setFocusable(false);
        mAhpDashboardFragBinding.ahpDashboardAlternativesListRecyclerView.setAdapter(mAhpDashboardAlternativeAdapter);

        mAhpDashboardFragBinding.ahpDashboardCriterionsListRecyclerView.setLayoutManager(layout2);
        mAhpDashboardFragBinding.ahpDashboardCriterionsListRecyclerView.setNestedScrollingEnabled(false);
        mAhpDashboardFragBinding.ahpDashboardCriterionsListRecyclerView.setFocusable(false);
        mAhpDashboardFragBinding.ahpDashboardCriterionsListRecyclerView.setAdapter(mAhpDashboardCriterionAdapter);


        checkListsContent();
        configDialogs();

        setHasOptionsMenu(true);
        return mAhpDashboardFragBinding.getRoot();
    }

    @Override
    public void checkListsContent(){
        if(mAhpDashboardAlternativeAdapter.getItemCount() > 0){
            mAhpDashboardFragBinding.ahpDashboardNoAlternativesTextView.setVisibility(View.GONE);
        }

        if(mAhpDashboardCriterionAdapter.getItemCount() > 0){
            mAhpDashboardFragBinding.ahpDashboardNoCriterionsTextView.setVisibility(View.GONE);
        }
    }

    @Override
    public void startNew(View view) {
        if(mAhpDashboardAlternativeAdapter.getItemCount() == 4 && mAhpDashboardCriterionAdapter.getItemCount() == 4){
            Intent intent = new Intent(getContext(), AhpStepperActivity.class);
            AhpMethod ahpMethod = new AhpMethod(mAhpDashboardCriterionAdapter.getCriterions(),mAhpDashboardAlternativeAdapter.getAlternatives());
            intent.putExtra("ahpBundle", (Serializable) ahpMethod );
            startActivityForResult(intent, 1);
        }else{
            Toast.makeText(getContext(), "Continue and add 4 criterion and 4 alternative", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void addCriterion(View view) {
        String criterionTitle = mAhpDashboardAddCriterionDialogBinding.ahpDashboardCriterionValueEditText.getText().toString();
        mAhpDashboardAddCriterionDialogBinding.ahpDashboardCriterionValueEditText.setText("");
        if(!criterionTitle.equals(""))
            mAhpDashboardCriterionAdapter.addItem(criterionTitle);

        addCriterionDialog.cancel();
    }

    @Override
    public void addAlternative(View view) {
        String alternativeTitle = mAhpDashboardAddAlternativeDialogBinding.ahpDashboardAlternativeValueEditText.getText().toString();
        mAhpDashboardAddAlternativeDialogBinding.ahpDashboardAlternativeValueEditText.setText("");
        if(!alternativeTitle.equals(""))
            mAhpDashboardAlternativeAdapter.addItem(alternativeTitle);

        addAlternativeDialog.cancel();
    }

    @Override
    public void openAddCriterionDialog(View view) {
        if(mAhpDashboardCriterionAdapter.getItemCount() == 4)
            Toast.makeText(getContext(), "Can only add maximum of 4 criteria .", Toast.LENGTH_SHORT).show();
        else
            addCriterionDialog.show();
    }

    @Override
    public void closeAddCriterionDialog(View view) {
        addCriterionDialog.cancel();
    }

    @Override
    public void openAddAlternativeDialog(View view) {
        if(mAhpDashboardAlternativeAdapter.getItemCount() == 4)
            Toast.makeText(getContext(), "Can only add maximum of 4 alternative.", Toast.LENGTH_SHORT).show();
        else
            addAlternativeDialog.show();
    }

    @Override
    public void closeAddAlternativeDialog(View view) {
        addAlternativeDialog.cancel();
    }

    @Override
    public void setPresenter(AhpDashboardContract.Presenter presenter) {}

    public void configDialogs(){

        addAlternativeDialog = new Dialog(getContext(), R.style.DialogTheme);
        addAlternativeDialog.setTitle("ADD Alternative");
        mAhpDashboardAddAlternativeDialogBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.ahp_dashboard_add_alternative_dialog, null, false);
        mAhpDashboardAddAlternativeDialogBinding.setHandler(this);
        addAlternativeDialog.setContentView(mAhpDashboardAddAlternativeDialogBinding.getRoot());

        addCriterionDialog = new Dialog(getContext(), R.style.DialogTheme);
        addCriterionDialog.setTitle("ADD CRITERION");
        mAhpDashboardAddCriterionDialogBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.ahp_dashboard_add_criterion_dialog, null, false);
        mAhpDashboardAddCriterionDialogBinding.setHandler(this);
        addCriterionDialog.setContentView(mAhpDashboardAddCriterionDialogBinding.getRoot());
    }

AlternativeAdapter


    private List<String> mAlternatives;
    private Context mContext;
    private AhpDashboardContract.View mAhpDashboardContract;
    private ItemViewHolder mViewHolder;
    private void setList(List<String> list) {
        mAlternatives = list;
    }

    public AhpDashboardAlternativeAdapter(List<String> alternatives, Context context, AhpDashboardContract.View ahpDashboardContract) {
        this.mAlternatives = alternatives;
        this.mContext = context;
        this.mAhpDashboardContract = ahpDashboardContract;
    }

    public List<String> getAlternatives(){
        return mAlternatives;
    }

    public void replaceData(List<String> itemsMethod) {
        setList(itemsMethod);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mAlternatives.size();
    }

    public void addItem(String alternativeTitle){
        String alternative = alternativeTitle;
        mAlternatives.add(alternative);
        notifyDataSetChanged();
        mAhpDashboardContract.checkListsContent();
    }
    public void removeItem(int position){
        mAlternatives.remove(position);
        notifyDataSetChanged();
        mAhpDashboardContract.checkListsContent();
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.ahp_dashboard_alternative_item_swipe_layout;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AhpDashboardAlternativeItemBinding ahpDashboardAlternativeItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                        R.layout.ahp_dashboard_alternative_item,
                        parent, false);
        return new ItemViewHolder(ahpDashboardAlternativeItemBinding);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        mViewHolder = holder;

        String alternative = mAlternatives.get(position);
        holder.mAhpDashboardAlternativeItemBinding.setAlternative(alternative);
        holder.mAhpDashboardAlternativeItemBinding.executePendingBindings();
       holder.mAhpDashboardAlternativeItemBinding.ahpDashboardRemoveAlternativeLinearLayout.setOnClickListener(v -> {
            removeItem(position);
        });
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{

        private AhpDashboardAlternativeItemBinding mAhpDashboardAlternativeItemBinding;

        public ItemViewHolder(AhpDashboardAlternativeItemBinding ahpDashboardAlternativeItemBinding) {
            super(ahpDashboardAlternativeItemBinding.getRoot());

            this.mAhpDashboardAlternativeItemBinding = ahpDashboardAlternativeItemBinding;

        }

CriteriaAdapter


    private List<String> mCriterions;
    private Context mContext;
    private AhpDashboardContract.View mAhpDashboardContract;
    private AhpDashboardCriterionAdapter.ItemViewHolder mViewHolder;

    private void setList(List<String> list) {
        mCriterions = list;
    }

    public AhpDashboardCriterionAdapter(List<String> criterions, Context context, AhpDashboardContract.View ahpDashboardContract) {
        this.mCriterions = criterions;
        this.mContext = context;
        this.mAhpDashboardContract = ahpDashboardContract;
    }

    public List<String> getCriterions(){
        return mCriterions;
    }

    public void replaceData(List<String> criterions) {
        setList(criterions);
        notifyDataSetChanged();
    }

    public void addItem(String criterionTitle){
        String criterion = criterionTitle;
        mCriterions.add(criterion);
        notifyDataSetChanged();
        mAhpDashboardContract.checkListsContent();
    }
    public void removeItem(int position){
        mCriterions.remove(position);
        notifyDataSetChanged();
        mAhpDashboardContract.checkListsContent();
    }

    @Override
    public int getItemCount() {
        return mCriterions.size();
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.ahp_dashboard_criterion_item_swipe_layout;
    }

    @Override
    public AhpDashboardCriterionAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AhpDashboardCriterionItemBinding ahpDashboardCriterionItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                        R.layout.ahp_dashboard_criterion_item,
                        parent, false);

        return new AhpDashboardCriterionAdapter.ItemViewHolder(ahpDashboardCriterionItemBinding);
    }

    @Override
    public void onBindViewHolder(AhpDashboardCriterionAdapter.ItemViewHolder holder, int position) {

        mViewHolder = holder;

        String criterion = mCriterions.get(position);

        holder.mAhpDashboardCriterionItemBinding.setCriterion(criterion);

        holder.mAhpDashboardCriterionItemBinding.ahpDashboardRemoveCriterionLinearLayout.setOnClickListener(v -> {
            removeItem(position);
        });
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{

        private AhpDashboardCriterionItemBinding mAhpDashboardCriterionItemBinding;

        public ItemViewHolder(AhpDashboardCriterionItemBinding ahpDashboardCriterionItemBinding) {
            super(ahpDashboardCriterionItemBinding.getRoot());

            this.mAhpDashboardCriterionItemBinding = ahpDashboardCriterionItemBinding;
        }
    }

Step 2: Derive priorities for the criteria

The importance of criteria are compared pairwise with respect to the desired goal to derive their weights. The comparison is done using Saaty's scale of 1-9 based on the level of important, with 9 being absolutely more important and 1 being slightly important.

ComparisonItemAdapter

This is where the user will select a scale of importance for each pair of comparison.

Screenshot_20180705-012828.png


    private List<ComparisonItem> mComparisonItems;
    private Context mContext;
    private ItemViewHolder mViewHolder;
    private ComparisonItem mComparisonItem;
    private int itemPosition;

    public AhpComparionItemAdapter(Context context,
                                              List<ComparisonItem> comparisonItems){
        this.mContext         = context;
        this.mComparisonItems = comparisonItems;
    }


    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AhpMethodComparisonItemBinding ahpMethodComparisonItemBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.ahp_method_comparison_item,
                parent,
                false);

        ahpMethodComparisonItemBinding.setHandler(this);
        final ItemViewHolder vh = new ItemViewHolder(ahpMethodComparisonItemBinding);
        return vh;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        mViewHolder = holder;
        itemPosition = position;
        ComparisonItem comparisonItem = mComparisonItems.get(position);
        mViewHolder.mAhpMethodComparisonItemBinding.setComparisonItem(comparisonItem);
        mViewHolder.mAhpMethodComparisonItemBinding.ahpMethodComparisonItemDefinitionComparisonTextView.setText(mContext.getString(getComparisonDefinitionTextId(comparisonItem.getDefinitionText())));

        mViewHolder.itemView.setOnClickListener(view -> {
            ComparisonItem ComparisonItem = (ComparisonItem) view.getTag();
//            mDashboardViewContract.showProblemIdentifiedDetail(ComparisonItem);
        });

        mViewHolder.mAhpMethodComparisonItemBinding.ahpMethodCriterionOneStepFirstSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                ComparisonItem comparisonItem = mComparisonItems.get(position);
                comparisonItem = updateSeekBarRealValues(comparisonItem,seekBar.getProgress());
                mComparisonItems.set(position,comparisonItem);
                mViewHolder = holder;
                mViewHolder.mAhpMethodComparisonItemBinding.setComparisonItem(comparisonItem);
                mViewHolder.mAhpMethodComparisonItemBinding.ahpMethodComparisonItemDefinitionComparisonTextView.setText(mContext.getString(getComparisonDefinitionTextId(comparisonItem.getDefinitionText())));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mViewHolder.mAhpMethodComparisonItemBinding.executePendingBindings();
    }

    public void replaceData(List list) {
        mComparisonItems = list;
        notifyDataSetChanged();
    }

    private ComparisonItem updateSeekBarRealValues(ComparisonItem comparisonItem,int progress) {

        switch (progress) {

            case 0:
                comparisonItem.setFirstAlternativeValue("1/9");
                comparisonItem.setSecondAlternativeValue("9");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.ABSOLUTE.getValue());
                break;
            case 1:
                comparisonItem.setFirstAlternativeValue("1/7");
                comparisonItem.setSecondAlternativeValue("7");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.VERY_STRONG.getValue());
                break;
            case 2:
                comparisonItem.setFirstAlternativeValue("1/5");
                comparisonItem.setSecondAlternativeValue("5");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.STRONG.getValue());
                break;
            case 3:
                comparisonItem.setFirstAlternativeValue("1/3");
                comparisonItem.setSecondAlternativeValue("3");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.TWEAK.getValue());
                break;
            case 4:
                comparisonItem.setFirstAlternativeValue("1");
                comparisonItem.setSecondAlternativeValue("1");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.EQUAL.getValue());
                break;
            case 5:
                comparisonItem.setFirstAlternativeValue("3");
                comparisonItem.setSecondAlternativeValue("1/3");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.TWEAK.getValue());
                break;
            case 6:
                comparisonItem.setFirstAlternativeValue("5");
                comparisonItem.setSecondAlternativeValue("1/5");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.STRONG.getValue());
                break;
            case 7:
                comparisonItem.setFirstAlternativeValue("7");
                comparisonItem.setSecondAlternativeValue("1/7");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.VERY_STRONG.getValue());
                break;
            case 8:
                comparisonItem.setFirstAlternativeValue("9");
                comparisonItem.setSecondAlternativeValue("1/9");
                comparisonItem.setDefinitionText(ComparisonDefinitionEnum.ABSOLUTE.getValue());
                break;
        }

        return comparisonItem;
    }

    private int getComparisonDefinitionTextId(int enumSituationValue) {

        Map<Integer, Integer> comparisonDefinitionText = new HashMap<Integer, Integer>();
        comparisonDefinitionText.put(ComparisonDefinitionEnum.EQUAL.getValue(), R.string.ahp_method_comparison_definitions_equal_importance);
        comparisonDefinitionText.put(ComparisonDefinitionEnum.TWEAK.getValue(), R.string.ahp_method_comparison_definitions_tweak_importance);
        comparisonDefinitionText.put(ComparisonDefinitionEnum.STRONG.getValue(), R.string.ahp_method_comparison_definitions_strong_importance);
        comparisonDefinitionText.put(ComparisonDefinitionEnum.VERY_STRONG.getValue(), R.string.ahp_method_comparison_definitions_very_strong_importance);
        comparisonDefinitionText.put(ComparisonDefinitionEnum.ABSOLUTE.getValue(), R.string.ahp_method_comparison_definitions_absolute_importance);
        comparisonDefinitionText.put(ComparisonDefinitionEnum.INTERMEDIARY.getValue(), R.string.ahp_method_comparison_definitions_intermediary_values);
        int comparisonDefinitionTextId = comparisonDefinitionText.get(enumSituationValue);

        return comparisonDefinitionTextId;
    }

    @Override
    public int getItemCount() {
        return mComparisonItems.size();
    }

    public List<ComparisonItem> getComparisonItems() {
        return mComparisonItems;
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder{

        private AhpMethodComparisonItemBinding mAhpMethodComparisonItemBinding;

        public ItemViewHolder(AhpMethodComparisonItemBinding ahpMethodComparisonItemBinding) {
            super(ahpMethodComparisonItemBinding.getRoot());
            this.mAhpMethodComparisonItemBinding = ahpMethodComparisonItemBinding;
        }
    } 

ComparisonStep

This is the final phase before the result, after analyzing and comparing the alternative. Each criterion is being compared with the other since AHP is all about pairwise comparison.


    AhpMethodComparisonStepBinding mBinding;
    private AhpComparionItemAdapter mAhpComparionItemAdapter;
    private float[][] comparisonMatrix;
    private List<String> mComparisonItems;
    private String mComparisonTitle;
    private int mStepIndex;

    @SuppressLint("ValidFragment")
    public ComparisonStep(List<String> comparisonItems, String comparisonTitle, int stepIndex){
        this.mComparisonItems = comparisonItems;
        this.mComparisonTitle = comparisonTitle;
        this.mStepIndex = stepIndex;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAhpComparionItemAdapter = new AhpComparionItemAdapter(getContext(), new ArrayList<>());

        System.out.println(name());

        List<ComparisonItem> comparisonItemList = new ArrayList<>();
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(0), mComparisonItems.get(1)));
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(0), mComparisonItems.get(2)));
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(0), mComparisonItems.get(3)));
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(1), mComparisonItems.get(2)));
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(1), mComparisonItems.get(3)));
        comparisonItemList.add(new ComparisonItem(mComparisonItems.get(2), mComparisonItems.get(3)));

        mAhpComparionItemAdapter.replaceData(comparisonItemList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.ahp_method_comparison_step,container,false);
        mBinding.setComparisonTitle(mComparisonTitle);

        mBinding.ahpMethodCriterionComparisonListRecyclerView.setNestedScrollingEnabled(false);
        mBinding.ahpMethodCriterionComparisonListRecyclerView.setFocusable(false);
        RecyclerView.LayoutManager layout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mBinding.ahpMethodCriterionComparisonListRecyclerView.setLayoutManager(layout);
        mBinding.ahpMethodCriterionComparisonListRecyclerView.setAdapter(mAhpComparionItemAdapter);

        return mBinding.getRoot();
    }

    private void generateComparisonMatrice(){

        List<ComparisonItem> comparisonItems = mAhpComparionItemAdapter.getComparisonItems();
        ComparisonItem comparisonItem0 = comparisonItems.get(0);
        ComparisonItem comparisonItem1 = comparisonItems.get(1);
        ComparisonItem comparisonItem2 = comparisonItems.get(2);
        ComparisonItem comparisonItem3 = comparisonItems.get(3);
        ComparisonItem comparisonItem4 = comparisonItems.get(4);
        ComparisonItem comparisonItem5 = comparisonItems.get(5);

        float[][] matrix ={
                {1, comparisonItem0.getSecondAlternativeValue(), comparisonItem1.getSecondAlternativeValue(), comparisonItem2.getSecondAlternativeValue()},
                {comparisonItem0.getFirstAlternativeValue(), 1, comparisonItem3.getSecondAlternativeValue(), comparisonItem4.getSecondAlternativeValue()},
                {comparisonItem1.getFirstAlternativeValue(), comparisonItem3.getFirstAlternativeValue(), 1, comparisonItem5.getSecondAlternativeValue()},
                {comparisonItem2.getFirstAlternativeValue(), comparisonItem4.getFirstAlternativeValue(), comparisonItem5.getFirstAlternativeValue(), 1}
        };

        comparisonMatrix = matrix;
    }

    @Override
    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);
    }

    @Override
    public String name() {
        return "Tab_"+mStepIndex;
    }

    @Override
    public boolean isOptional() {
        return true;
    }

    @Override
    public void onStepVisible() {
        System.out.println(name());
    }

    @Override
    public void onNext() {
        generateComparisonMatrice();

        imprimeMatriz(comparisonMatrix);
        mStepper.getExtras().putSerializable(name(), comparisonMatrix);

        System.out.println("onNext ");
    }

    @Override
    public void onPrevious() {
        System.out.println("onPrevious");
    }

    @Override
    public String optional() {
        return "You can skip";
    }

    @Override
    public String error() {
        return "<b>You must click!</b> <small>this is the condition!</small>";
    }

    private void imprimeMatriz(float [][] criterion1matrix3){
        for(float[] c : criterion1matrix3){

            for(float elemento : c)
                System.out.printf(" %.2f ", elemento);
            System.out.printf("\n");
        }

    } 

Result

Based on the synthesis results and sensitivity analysis, a decision can be made. The criteria with the most dominant or highest value is accepted.

ResultDialog

A dialog or and Activity is created to show the result of the analysis.


    private final Context mContext;
    private Dialog mAhpResultsDialog;
    private Fragment mFragment;
    private AhpResultsDialogBinding mAhpResultsDialogBinding;

    public AhpResultsDialog(Context context, Fragment fragment){
        mContext  = context;
        mFragment = fragment;

        ahpResultsDialogConfiguration();
    }

    @Override
    public void ahpResultsDialogConfiguration() {
        mAhpResultsDialog = new Dialog(mContext,DialogTheme);
        mAhpResultsDialogBinding =
                DataBindingUtil.inflate(LayoutInflater.from(mContext),
                        R.layout.ahp_results_dialog,
                        null,
                        false);
        mAhpResultsDialog.setContentView(mAhpResultsDialogBinding.getRoot());
        mAhpResultsDialogBinding.setHandler(this);
        mAhpResultsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mAhpResultsDialog.setCanceledOnTouchOutside(false);
        mAhpResultsDialog.setCancelable(false);
    }

    @Override
    public void show() {
        mAhpResultsDialog.show();
    }

    @Override
    public void close(View view) {
        mAhpResultsDialog.cancel();
    }

    @Override
    public void showResults(float[] averagePriorityMatrix, float[][] preferenceMatrix, List<String> alternatives, List<String> criterions) {

        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit0.setText(criterions.get(0));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit1.setText(criterions.get(1));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit2.setText(criterions.get(2));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit3.setText(criterions.get(3));

        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit0Value.setText(String.valueOf(averagePriorityMatrix[0]));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit1Value.setText(String.valueOf(averagePriorityMatrix[1]));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit2Value.setText(String.valueOf(averagePriorityMatrix[2]));
        mAhpResultsDialogBinding.ahpResultsPriorityMatrixInclude.ahpResultPriorityMatrixCrit3Value.setText(String.valueOf(averagePriorityMatrix[3]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt0.setText(alternatives.get(0));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt1.setText(alternatives.get(1));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt2.setText(alternatives.get(2));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt3.setText(alternatives.get(3));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixCrit0.setText(criterions.get(0));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixCrit1.setText(criterions.get(1));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixCrit2.setText(criterions.get(2));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixCrit3.setText(criterions.get(3));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt0Crit0.setText(String.valueOf(preferenceMatrix[0][0]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt0Crit1.setText(String.valueOf(preferenceMatrix[0][1]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt0Crit2.setText(String.valueOf(preferenceMatrix[0][2]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt0Crit3.setText(String.valueOf(preferenceMatrix[0][3]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt1Crit0.setText(String.valueOf(preferenceMatrix[1][0]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt1Crit1.setText(String.valueOf(preferenceMatrix[1][1]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt1Crit2.setText(String.valueOf(preferenceMatrix[1][2]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt1Crit3.setText(String.valueOf(preferenceMatrix[1][3]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt2Crit0.setText(String.valueOf(preferenceMatrix[2][0]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt2Crit1.setText(String.valueOf(preferenceMatrix[2][1]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt2Crit2.setText(String.valueOf(preferenceMatrix[2][2]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt2Crit3.setText(String.valueOf(preferenceMatrix[2][3]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt3Crit0.setText(String.valueOf(preferenceMatrix[3][0]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt3Crit1.setText(String.valueOf(preferenceMatrix[3][1]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt3Crit2.setText(String.valueOf(preferenceMatrix[3][2]));
        mAhpResultsDialogBinding.ahpResultsPreferenceMatrixInclude.ahpResultPreferenceMatrixAlt3Crit3.setText(String.valueOf(preferenceMatrix[3][3]));
        show();
    }

    @Override
    public void setPresenter(AhpResultsDialogContract.Presenter presenter) {

    }

Screenshot_20180705-035911.png

Proof of Work Done

The full code of the tutorial is in the githup link below
https://github.com/Godwyyn/AHPProject

Sort:  

Thank you for your contribution.
This moderation might not be considered due to the below:

  • The tutorial has a lot of code that isn't explained.
  • Further detail your tutorial, the more information the easier it'll be for the user to understand your code.
  • Put some comments on your code, plus the explanation you've already made.

Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Can I edit this one and post again? Or make another post on the same topic?

Sorry no, you have to make a new contribution but with a new subject.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

@resteemator is a new bot casting votes for its followers. Follow @resteemator and vote this comment to increase your chance to be voted in the future!