交换数组元素不起作用?

时间:2017-03-07 12:23:47

标签: javascript arrays reactjs redux

我是javascript的新手,我对交换感到困惑。实际上,我的交换功能无法正常工作。

我的数据结构

let WholeSeq = [] //variable name for image content

WholeSeq

例如: - 工作条件

let SwapThis = this.swapArrayElements([1,2,3],0,1) //Working

不工作

 let SwapThis = this.swapArrayElements(WholeSeq[0].Sequence,0,1) // Not Working

我的交换数组函数

swapArrayElements(a, x, y){
        if (a.length === 1) return a;
        a.splice(y, 1, a.splice(x, 1, a[y])[0]);
        return a;
    }

整体模拟算法

import React from 'react';
import {render} from 'react-dom';
import _ from 'lodash';
import lupus from 'lupus';

let newRandomPop = [];

let howManyMachines = 2;
let njobs = 6;


let time = [5,3,1,2,4,6];



class SA extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            njobs:njobs,
            time:time,
            howManyMachines:howManyMachines,
            bestValue:0
        }
    }

    componentDidMount(){
        this.generatePopulation();
    }
    swapArrayElements(a, x, y){
        if (a.length === 1) return a;
        a.splice(y, 1, a.splice(x, 1, a[y])[0]);
        return a;
    }
    generateRandomeNumberPaticularSequence(maximum,minimum){
        return Math.round( Math.random() * (maximum - minimum) + minimum);
    }
    SwapThis(WholeSeq){

        let newSeq = [];

        console.log(WholeSeq[0].Sequence);



         //This Swaping is not working
        console.log(this.swapArrayElements(WholeSeq[0].Sequence,0,1))
    }

    takeDecision(bestValue){
        if(this.state.bestValue < bestValue){
            console.log('Negative Value')

        }else{
            this.setState({
                bestValue

            })
        }

    }

    AllotFitnessRate(newMachine){
        let NewMachineArrayWithFitnessRate = [];
        let TotalMaxValue = newMachine.reduce((sum,MaxValue) => {
            return sum + MaxValue.MaxValue
        },0);
        let AvgValue = TotalMaxValue/newMachine.length
        newMachine.forEach(data => {
            let newArrayWithFitness ={

                fitness:(data.MaxValue/AvgValue),
                MaxValue:data.MaxValue,
                MachineArr:data.MachineArr,
                Sequence:data.Sequence
            }
            NewMachineArrayWithFitnessRate.push(newArrayWithFitness)
        });
     this.takeDecision(NewMachineArrayWithFitnessRate[0].MaxValue)
     this.SwapThis(NewMachineArrayWithFitnessRate)
    }

    allortAllSequenceForTesting(allSequence){
        console.log('From Se',allSequence)
        const NewMachine = [];
        for(let i=0;i<allSequence.length;i++){
            let machines = Array(this.state.howManyMachines).fill().map((m, i) => {
                return {
                    id: i,
                    value: 0,
                    jobs: [],
                    name:"M"+(i+1)

                } });
            allSequence[i].forEach(job => {
                // console.log(allSequence[i])
                let minMachine = machines
                    .slice(1)
                    .reduce((res, cur) =>
                        res.value <= cur.value ? res : cur, machines[0]);
                minMachine.jobs.push(job);
                minMachine.value += job.timeToFinish;

            });
            let HigestValueArray = _.map(machines, function (o) {
                return o.value;
            });
            let subArrayMaxValue = _.max(HigestValueArray);
            // console.log("Befor",subArrayMaxValue);
            // console.log(machines);
            const ArrayShouldBePushed = {
                MaxValue:subArrayMaxValue,
                MachineArr:machines,
                Sequence:allSequence[i]
            }

            NewMachine.push(ArrayShouldBePushed);
        }

        let sortThis = _.sortBy(NewMachine,'MaxValue');
        // sortThis.map(data => {
        //
        //     console.log("===============",data.MaxValue)
        //     console.log(data.Sequence.map(data => {
        //         return data.jobId
        //     }))
        //    data.MachineArr.map(data => {
        //        console.log("Machine Name",data.name)
        //        data.jobs.map(data => {
        //
        //            console.log(data.jobId," ",data.timeToFinish)
        //
        //        })
        //    })
        //
        // })
        this.AllotFitnessRate(sortThis)
    }
    generatePopulation(){
        let allJobs =  Array(this.state.njobs).fill().map((m,i) => {
            return {
                jobId:"j"+(i+1),
                timeToFinish:this.state.time[i]
            }
        });
        for(let i =0;i<2;i++){
            let pushtoNewArray = _.shuffle(allJobs);
            newRandomPop.push(pushtoNewArray)

        }
        this.allortAllSequenceForTesting(newRandomPop);
    }
    render(){
        return(
            <div>
                Hellp
            </div>
        )
    }
}

render(<SA/>,document.getElementById('app'));

会有很多功能,但主要功能是

SwapThis(WholeSeq){
            let newSeq = [];

            console.log(WholeSeq[0].Sequence);

             //This Swaping is not working
            console.log(this.swapArrayElements(WholeSeq[0].Sequence,0,1))
        }

0 个答案:

没有答案