Skip to content

Commit

Permalink
app v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nutelabezgmo222 committed Apr 29, 2021
1 parent 4708664 commit e99d601
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
7 changes: 3 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ function App() {
const simplex = new Simplex(prodObject, prodRestriction);
setResult({
variables: simplex.getResult(),
finalValue: simplex.getGoalValue(),
});
}

return (
<div className="app">
<Header></Header>
Expand Down Expand Up @@ -203,8 +201,9 @@ function App() {
Рохрахувати
</button>
</div>
{result &&
<Result resultSet={result} values={prodObject.values}/>
{
result &&
<Result resultSet={result} values={prodObject.values} attributes={prodObject.attributes}/>
}
</Main>
</div>
Expand Down
39 changes: 35 additions & 4 deletions src/components/Result.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
import React from 'react'

function Result({ resultSet = {}, values = [] }) {
function Result({ resultSet = {}, values = [], attributes=[] }) {
const finalValue = resultSet.variables.reduce((total, variable) => {
if (variable.name < values.length) {
total += values[variable.name].prodValues[0].value * variable.value
}
return total;
}, 0)
const materials = attributes.reduce((arr, attr) => {
if (attr.id === 0) return arr;
const curBalance = resultSet.variables.reduce((total, variable) => {
if (variable.name < values.length) {
total +=
values[variable.name].prodValues.find((prodVal) => prodVal.id === attr.id).value *
variable.value
}
return total;
}, 0)
arr.push({ name: attr.title, balance: attr.restriction - curBalance });
return arr;
}, [])
return (
<div className="result">
<h3>Результат</h3>
<div className="result__body">
Для того щоб отримати максимальный прибуток у вигляді {Math.floor(resultSet.finalValue)} у.о
Для того щоб отримати максимальный прибуток у вигляді
<span style={{fontSize:'1.2em', color: 'darkgreen', fontWeight: 'bold'}}> {finalValue}</span> у.о
<p>Необхідно створити:</p>
<ul className="result__list">
{
resultSet.variables &&
resultSet &&
resultSet.variables.map((variable, i) => {
if (typeof variable.name === 'number') {
return <li key={i}>{values[variable.name].title} : {Math.floor(variable.value)}</li>
if (variable.name < values.length) {
return <li key={i}>{values[variable.name].title} : {variable.value}</li>
}
}
return '';
})
}
</ul>
<p style={{marginTop: '15px'}}>Залишки матеріалів:</p>
<ul className="result__list">
{
materials &&
materials.map((material, i) => {
return <li key={`${material.name}_${i}`}>{material.name} : {material.balance}</li>
})
}
</ul>
</div>

</div>
Expand Down
32 changes: 21 additions & 11 deletions src/features/simplex-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ export default class Simplex extends SimplexData {
leadingRowIndex = 0; // current decision leading row
leadingElement = 0; // current leading element (crossing of leading col and row)
lastCol = [];

itarationCount = 0;
constructor(object, prodRest = null) {
super(object, prodRest);
this.iterationCount = 0;
this.initSimplexTable();
while (!this.isFunctionOptimal()) {
this.findOptimal();
this.iterationCount++;
}
}

Expand Down Expand Up @@ -134,7 +136,7 @@ export default class Simplex extends SimplexData {
findLeadingRow() {
let minIndex = 0;
for (let i = 1; i < this.lastCol.length; i++) {
if (this.lastCol[minIndex] > this.lastCol[i]) {
if (this.lastCol[minIndex] > this.lastCol[i] || this.lastCol[minIndex] === null) {
minIndex = i;
}
}
Expand All @@ -144,7 +146,7 @@ export default class Simplex extends SimplexData {
this.lastCol = [];
for (let i = 0; i < this.basisValues.length; i++) {
let colValue;
if (this.coefMatrix[i][this.leadingColIndex] < 0) {
if (this.coefMatrix[i][this.leadingColIndex] <= 0) {
colValue = null;
} else {
colValue =
Expand Down Expand Up @@ -221,16 +223,24 @@ export default class Simplex extends SimplexData {
this.setLeadingElement();
}
getResult() {
return this.basisValues;
}
getGoalValue() {
return this.currentGoalValue;
const newBasisValues = this.basisValues.map((val) => {
let newValue = val.value.toFixed(3);
if (newValue.toString().slice(-3) === "999") {
newValue = Math.round(newValue);
} else {
newValue = Math.floor(newValue);
}
return {
...val,
value: newValue,
};
});
return newBasisValues;
}
toString() {
console.log('basis: ', this.basisValues);
console.log('Matrix: ', this.coefMatrix);
console.log('goalValue: ', this.currentGoalValue);
console.log('indexRow: ', this.indexRow)
console.log(this.lastCol);
console.log("indexRow:", this.indexRow);
console.log("leadingElement:", this.leadingElement);
}

}

1 comment on commit e99d601

@vercel
Copy link

@vercel vercel bot commented on e99d601 Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.