Ai Dreams Forum

Artificial Intelligence => General AI Discussion => Topic started by: yotamarker on December 23, 2017, 08:35:45 pm

Title: algorithms involving unknowns
Post by: yotamarker on December 23, 2017, 08:35:45 pm
the following is supposed to pick the closest elevator to person x going up or down from floor y

do you agree with the alg or do you have a better solution :
Code
Public Class Form1
    Dim topFloor As Integer = 31
    Dim btmFlr As Integer = 0
    Dim EF() As Integer = {1, 6, 12, 18, 24, 31, 20} ' elevator at floor
    Dim ar() As Integer = {1, 2, 0, 1, 2, 0} ' elevator direction
    '1 up 2 down 0 non moving
    Private Sub inputReqBtn_Click(sender As Object, e As EventArgs) Handles inputReqBtn.Click
        Dim Rqflr As Integer = RqflrTxt.Text
        Dim Rqdir As Integer = RqDirTxt.Text

        MsgBox(nearElevator(Rqflr, Rqdir, EF, ar))
    End Sub
    Function elevatorDistance(ByVal xF As Integer, ByVal ef As Integer, ByVal xdir As Integer, ByVal edir As Integer) As Integer
        Dim distance As Integer = 0
        If xF > ef Then
            Select Case xdir
                Case 1 'up
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + xF
                        Case 0
                            distance = topFloor - ef + topFloor - xF
                    End Select
                Case 2
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + topFloor - btmFlr
                        Case 0
                            distance = topFloor - ef + topFloor - xF
                    End Select

            End Select
        Else
            Select Case xdir
                Case 1
                    Select Case edir
                        Case 1
                            distance = topFloor - ef + topFloor - xF
                        Case 2
                            distance = ef - btmFlr + xF - btmFlr
                        Case 0
                            distance = ef - btmFlr + xF - btmFlr
                    End Select
                Case 2
                    Select Case edir
                        Case 1
                            distance = 2 * topFloor - xF - ef
                        Case 2
                            distance = ef - xF
                        Case 0
                            distance = ef - btmFlr + xF - btmFlr
                    End Select

            End Select
        End If
        Return distance
    End Function
    Function nearElevator(ByVal xF As Integer, ByVal xdir As Integer, ByVal ef() As Integer, ByVal edir() As Integer) As Integer
        Dim min As Integer = 2 * topFloor
        Dim i As Integer
        For index = 0 To edir.Length - 1
            If min > elevatorDistance(xF, ef(index), xdir, edir(index)) Then
                min = elevatorDistance(xF, ef(index), xdir, edir(index))
                i = index
            End If
        Next
        Return i
    End Function
End Class
Title: Re: algorithms involving unknowns
Post by: ranch vermin on December 23, 2017, 09:16:23 pm
I reckon its got a chance of working from a glance, but i call these methods, not algorythms.  to me an algorythm is a supervision function involving a score.
Title: Re: algorithms involving unknowns
Post by: yotamarker on January 05, 2018, 04:36:16 pm
this thread is about automatic programming. we are living in the age of low tech programming wise. when a programmer receives a task he has to develop an algorithm and then translate it into code with many in between steps. ignoring the aspect of human unemployment that is completely out of the scope of what this thread "cares" about, obviously said task should be delegated to the A.I which, in a technologically advanced reality, return a ready made alg + code/s.

having said that, the basic outline of my "plan" is to refer to algorithms as spells from a grimoire.
what do you think ?
Title: Re: algorithms involving unknowns
Post by: yotamarker on January 09, 2018, 03:02:26 pm
     *
    ***
   *****
  *******
 *********
***********
Code
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {l1 = l1 + "*";}
System.out.println(l1);
l1 = "";
}

}
}
     *
    * *
   * * *
  * * * *
 * * * * *
Code
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}

}
}
     *
    * *
   * * *
  * * * *
 * * * * *
 * * * * *
  * * * *
   * * *
    * *
     *
Code
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}

//part 2 lower triangle :
for (int i=x;i>=1;i--){
//replica
for(int j=0;j<(x-i);j++) {l1 = l1 + " ";}
for(int j=0;j<(i*2-1);j++) {if(j%2==0) {l1 = l1 + " ";}else{l1 = l1 + "*";}}
System.out.println(l1);
l1 = "";
}
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//j = 0 evolve to j = 1
for(int j=1;j<i;j++) {l1 += j +" ";}
System.out.println(l1);
l1 = "";
}

}
}
       1
      2 1 2
    3 2 1 2 3
  4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Code
package PL;

import java.util.Scanner;
import static java.lang.System.out;

public class test2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String l1 = "";
Scanner s1 = new Scanner(System.in);
int x = s1.nextInt();
for (int i=1;i<=x;i++){
//mirror next for loop in reverse 3 of the for conditions
// evolve j=i to j = i-1
for(int j=0;j<(x-i);j++) {l1 = l1 + "  ";} // final adds this spacer, evolve " " to "  "
for(int j=i-1;j>1;j--) {l1 += j + " ";}
for(int j=1;j<i;j++) {l1 += j +" ";}
System.out.println(l1);
l1 = "";
}

}
}
(https://i.imgflip.com/22dl0b.jpg) (https://imgflip.com/i/22dl0b) (https://imgflip.com/memegenerator)
Title: Re: algorithms involving unknowns
Post by: yotamarker on January 28, 2018, 08:30:38 pm
test of an example object class 8)
car class :

Code
public class Car {
private int plateNumber, DailyCost;
private int rented=0;
private String type ; // the class variable will have private access modifiers
//to prevent code injection vulnerability
public int getPlateNumber() {
return plateNumber;
}
public void setPlateNumber(int plateNumber) {
this.plateNumber = plateNumber;
}
public int getDailyCost() {
return DailyCost;
}
public void setDailyCost(int dailyCost) {
DailyCost = dailyCost;
}
public int getRented() {
return rented;
}
public void setRented(int rented) {
this.rented = rented;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car() {} // empty constructor
public Car(int platenumber, String type, int DailyCost) {
this.plateNumber = platenumber;
this.type = type;
this.DailyCost = DailyCost;
}
public Car(int platenumber) {
this.plateNumber = platenumber;
}
public int bill(int Days) {
rented+=Days;
return DailyCost*Days;
}
public int totalBill() {
return DailyCost*rented;
}
public String toString() {

return this.plateNumber +"\nDaily cost : " + this.DailyCost +
"\ntype  : "+ this.type +"\nrented for: " + this.rented + " days\n\n\n";
}
}
MAIN:
Code
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Car delorean = new Car(123, "DMC-12", 300);
Car peugeot = new Car();
Car mitsubishi = new Car(455);
System.out.println(delorean.toString());
System.out.println(peugeot.toString());
System.out.println(mitsubishi.toString());
System.out.println("bill "+delorean.bill(5));
System.out.println("bill "+delorean.bill(10));
System.out.println("total bill " + delorean.totalBill());
}

}
Title: Re: algorithms involving unknowns
Post by: keghn on February 01, 2018, 03:03:14 pm

Dynamic Programming: 

https://www.youtube.com/watch?v=DiAtV7SneRE
Title: Re: algorithms involving unknowns
Post by: yotamarker on February 10, 2018, 01:45:37 am
https://yotamarker.justforum.net/t145-java-class-with-learnability-exampled-with-simple-car-class